Merge remote-tracking branch 'origin/BAEL-2399' into BAEL-2399
This commit is contained in:
@@ -5,9 +5,8 @@ import static org.junit.Assert.assertNotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.examples.common.BookService;
|
||||
import com.baeldung.examples.guice.FooGenerator;
|
||||
import com.baeldung.examples.guice.FooProcessor;
|
||||
import com.baeldung.examples.guice.GuicePersonService;
|
||||
import com.baeldung.examples.guice.GuiceUser;
|
||||
import com.baeldung.examples.guice.GuiceUserService;
|
||||
import com.baeldung.examples.guice.Person;
|
||||
import com.baeldung.examples.guice.modules.GuiceModule;
|
||||
@@ -16,62 +15,47 @@ import com.google.inject.Injector;
|
||||
|
||||
public class GuiceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAccountInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuiceUser guiceUser = injector.getInstance(GuiceUser.class);
|
||||
assertNotNull(guiceUser.getAccount());
|
||||
}
|
||||
@Test
|
||||
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class);
|
||||
assertNotNull(guiceUserService.getAccountService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonDaoInjectedInGuicePersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
@Test
|
||||
public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
BookService bookService = injector.getInstance(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class);
|
||||
assertNotNull(guiceUserService.getAccountService());
|
||||
}
|
||||
@Test
|
||||
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
Person person = injector.getInstance(Person.class);
|
||||
assertNotNull(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
BookService bookService = injector.getInstance(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
}
|
||||
@Test
|
||||
public void givenFooInjectedToFooProcessorAsOptionalDependency_WhenFooProcessorIsRetrievedFromContext_ThenCreationExceptionIsNotThrown() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
FooProcessor fooProcessor = injector.getInstance(FooProcessor.class);
|
||||
assertNotNull(fooProcessor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
FooGenerator fooGenerator = injector.getInstance(FooGenerator.class);
|
||||
assertNotNull(fooGenerator);
|
||||
}
|
||||
@Test
|
||||
public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
Person person = injector.getInstance(Person.class);
|
||||
assertNotNull(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonDaoInjectedToGuicePersonServiceBySetterInjection_WhenGuicePersonServiceIsInjected_ThenPersonDaoInitializedByTheSetter() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
@Test
|
||||
public void givenPersonDaoInjectedToGuicePersonServiceBySetterInjection_WhenGuicePersonServiceIsInjected_ThenPersonDaoInitializedByTheSetter() {
|
||||
Injector injector = Guice.createInjector(new GuiceModule());
|
||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,54 +9,55 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.examples.common.AudioBookService;
|
||||
import com.baeldung.examples.common.BookService;
|
||||
import com.baeldung.examples.spring.AppConfig;
|
||||
import com.baeldung.examples.spring.SpringMainConfig;
|
||||
import com.baeldung.examples.spring.SpringPersonService;
|
||||
import com.baeldung.examples.spring.SpringUser;
|
||||
import com.baeldung.examples.spring.UserService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { AppConfig.class })
|
||||
@ContextConfiguration(classes = { SpringMainConfig.class })
|
||||
public class SpringUnitTest {
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void givenAccountAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
|
||||
SpringUser springUser = context.getBean(SpringUser.class);
|
||||
assertNotNull(springUser.getAccount());
|
||||
}
|
||||
@Test
|
||||
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||
UserService userService = context.getBean(UserService.class);
|
||||
assertNotNull(userService.getAccountService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonDaoAutowiredToSpringPersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
|
||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
@Test
|
||||
public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() {
|
||||
BookService bookService = context.getBean(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||
UserService userService = context.getBean(UserService.class);
|
||||
assertNotNull(userService.getAccountService());
|
||||
}
|
||||
@Test
|
||||
public void givenBookServiceIsRegisteredAsBeanInContextByOverridingAudioBookService_WhenAudioBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsThrown() {
|
||||
BookService bookService = context.getBean(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
AudioBookService audioBookService = context.getBean(AudioBookService.class);
|
||||
assertNotNull(audioBookService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() {
|
||||
BookService bookService = context.getBean(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
}
|
||||
@Test
|
||||
public void givenAuthorServiceAutowiredToBookServiceAsOptionalDependency_WhenBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsNotThrown() {
|
||||
BookService bookService = context.getBean(BookService.class);
|
||||
assertNotNull(bookService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||
assertNotNull(personService);
|
||||
}
|
||||
@Test
|
||||
public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||
assertNotNull(personService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() {
|
||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
@Test
|
||||
public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() {
|
||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||
assertNotNull(personService);
|
||||
assertNotNull(personService.getPersonDao());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user