Merge remote-tracking branch 'origin/BAEL-2399' into BAEL-2399
This commit is contained in:
commit
891fde1f49
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.examples.spring;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.baeldung.examples.common.PersonDao;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SpringPersonService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PersonDao personDao;
|
||||||
|
|
||||||
|
public PersonDao getPersonDao() {
|
||||||
|
return personDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPersonDao(PersonDao personDao) {
|
||||||
|
this.personDao = personDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,34 +0,0 @@
|
|||||||
package com.baeldung.examples.spring;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class Student {
|
|
||||||
private String firstName;
|
|
||||||
private String lastName;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public Student(@Value("Default") String firstName, @Value("Default") String lastName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.examples;
|
package com.baeldung.examples;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -12,8 +11,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
|
|
||||||
import com.baeldung.examples.common.BookService;
|
import com.baeldung.examples.common.BookService;
|
||||||
import com.baeldung.examples.spring.AppConfig;
|
import com.baeldung.examples.spring.AppConfig;
|
||||||
|
import com.baeldung.examples.spring.SpringPersonService;
|
||||||
import com.baeldung.examples.spring.SpringUser;
|
import com.baeldung.examples.spring.SpringUser;
|
||||||
import com.baeldung.examples.spring.Student;
|
|
||||||
import com.baeldung.examples.spring.UserService;
|
import com.baeldung.examples.spring.UserService;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ -23,13 +22,20 @@ public class SpringUnitTest {
|
|||||||
ApplicationContext context;
|
ApplicationContext context;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccountFieldAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
|
public void givenAccountAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
|
||||||
SpringUser springUser = context.getBean(SpringUser.class);
|
SpringUser springUser = context.getBean(SpringUser.class);
|
||||||
assertNotNull(springUser.getAccount());
|
assertNotNull(springUser.getAccount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccountServiceFieldAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
public void givenPersonDaoAutowiredToSpringPersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
|
||||||
|
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||||
|
assertNotNull(personService);
|
||||||
|
assertNotNull(personService.getPersonDao());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||||
UserService userService = context.getBean(UserService.class);
|
UserService userService = context.getBean(UserService.class);
|
||||||
assertNotNull(userService.getAccountService());
|
assertNotNull(userService.getAccountService());
|
||||||
}
|
}
|
||||||
@ -41,18 +47,16 @@ public class SpringUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStudentConstructorAnnotatedByAutowired_WhenStudentIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() {
|
public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||||
Student student = context.getBean(Student.class);
|
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||||
assertNotNull(student);
|
assertNotNull(personService);
|
||||||
assertEquals("Default", student.getFirstName());
|
|
||||||
assertEquals("Default", student.getLastName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAddressAutowiredToSpringUserBySetterInjection_WhenSpringUserRetrievedFromContext_ThenAddressInitializedByTheSetter() {
|
public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() {
|
||||||
SpringUser springUser = context.getBean(SpringUser.class);
|
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||||
assertNotNull(springUser.getAddress());
|
assertNotNull(personService);
|
||||||
assertEquals("Default", springUser.getAddress().getCity());
|
assertNotNull(personService.getPersonDao());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user