Skip to content

Commit 891fde1

Browse files
author
emineoymak
committed
Merge remote-tracking branch 'origin/BAEL-2399' into BAEL-2399
2 parents 0c60584 + 7bfb556 commit 891fde1

3 files changed

Lines changed: 84 additions & 92 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.examples.spring;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.examples.common.PersonDao;
7+
8+
@Component
9+
public class SpringPersonService {
10+
11+
@Autowired
12+
private PersonDao personDao;
13+
14+
public PersonDao getPersonDao() {
15+
return personDao;
16+
}
17+
18+
public void setPersonDao(PersonDao personDao) {
19+
this.personDao = personDao;
20+
}
21+
22+
}

‎guice/src/main/java/com/baeldung/examples/spring/Student.java‎

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,62 @@
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

Comments
 (0)