|
1 | | -package com.baeldung.examples; |
2 | | - |
3 | | -import static org.junit.Assert.assertEquals; |
4 | | -import static org.junit.Assert.assertNotNull; |
5 | | - |
6 | | -import org.junit.Test; |
7 | | -import org.junit.runner.RunWith; |
8 | | -import org.springframework.beans.factory.annotation.Autowired; |
9 | | -import org.springframework.context.ApplicationContext; |
10 | | -import org.springframework.test.context.ContextConfiguration; |
11 | | -import org.springframework.test.context.junit4.SpringRunner; |
12 | | - |
13 | | -import com.baeldung.examples.common.BookService; |
14 | | -import com.baeldung.examples.spring.AppConfig; |
15 | | -import com.baeldung.examples.spring.SpringUser; |
16 | | -import com.baeldung.examples.spring.Student; |
17 | | -import com.baeldung.examples.spring.UserService; |
18 | | - |
19 | | -@RunWith(SpringRunner.class) |
20 | | -@ContextConfiguration(classes = { AppConfig.class }) |
21 | | -public class SpringUnitTest { |
22 | | - @Autowired |
23 | | - ApplicationContext context; |
24 | | - |
25 | | - @Test |
26 | | - public void givenAccountFieldAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { |
27 | | - SpringUser springUser = context.getBean(SpringUser.class); |
28 | | - assertNotNull(springUser.getAccount()); |
29 | | - } |
30 | | - |
31 | | - @Test |
32 | | - public void givenAccountServiceFieldAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() { |
33 | | - UserService userService = context.getBean(UserService.class); |
34 | | - assertNotNull(userService.getAccountService()); |
35 | | - } |
36 | | - |
37 | | - @Test |
38 | | - public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() { |
39 | | - BookService bookService = context.getBean(BookService.class); |
40 | | - assertNotNull(bookService); |
41 | | - } |
42 | | - |
43 | | - @Test |
44 | | - public void givenStudentConstructorAnnotatedByAutowired_WhenStudentIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() { |
45 | | - Student student = context.getBean(Student.class); |
46 | | - assertNotNull(student); |
47 | | - assertEquals("Default", student.getFirstName()); |
48 | | - assertEquals("Default", student.getLastName()); |
49 | | - } |
50 | | - |
51 | | - @Test |
52 | | - public void givenAddressAutowiredToSpringUserBySetterInjection_WhenSpringUserRetrievedFromContext_ThenAddressInitializedByTheSetter() { |
53 | | - SpringUser springUser = context.getBean(SpringUser.class); |
54 | | - assertNotNull(springUser.getAddress()); |
55 | | - assertEquals("Default", springUser.getAddress().getCity()); |
56 | | - } |
57 | | - |
58 | | -} |
| 1 | +package com.baeldung.examples; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.ApplicationContext; |
| 9 | +import org.springframework.test.context.ContextConfiguration; |
| 10 | +import org.springframework.test.context.junit4.SpringRunner; |
| 11 | + |
| 12 | +import com.baeldung.examples.common.BookService; |
| 13 | +import com.baeldung.examples.spring.AppConfig; |
| 14 | +import com.baeldung.examples.spring.SpringPersonService; |
| 15 | +import com.baeldung.examples.spring.SpringUser; |
| 16 | +import com.baeldung.examples.spring.UserService; |
| 17 | + |
| 18 | +@RunWith(SpringRunner.class) |
| 19 | +@ContextConfiguration(classes = { AppConfig.class }) |
| 20 | +public class SpringUnitTest { |
| 21 | + @Autowired |
| 22 | + ApplicationContext context; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void givenAccountAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { |
| 26 | + SpringUser springUser = context.getBean(SpringUser.class); |
| 27 | + assertNotNull(springUser.getAccount()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenPersonDaoAutowiredToSpringPersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() { |
| 32 | + SpringPersonService personService = context.getBean(SpringPersonService.class); |
| 33 | + assertNotNull(personService); |
| 34 | + assertNotNull(personService.getPersonDao()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() { |
| 39 | + UserService userService = context.getBean(UserService.class); |
| 40 | + assertNotNull(userService.getAccountService()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() { |
| 45 | + BookService bookService = context.getBean(BookService.class); |
| 46 | + assertNotNull(bookService); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() { |
| 51 | + SpringPersonService personService = context.getBean(SpringPersonService.class); |
| 52 | + assertNotNull(personService); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() { |
| 57 | + SpringPersonService personService = context.getBean(SpringPersonService.class); |
| 58 | + assertNotNull(personService); |
| 59 | + assertNotNull(personService.getPersonDao()); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments