Merge remote-tracking branch 'origin/BAEL-2399' into BAEL-2399

This commit is contained in:
emineoymak 2019-01-01 03:20:10 -05:00
commit 0c6058427c
1 changed files with 77 additions and 72 deletions

View File

@ -1,72 +1,77 @@
package com.baeldung.examples;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.guice.Employee;
import com.baeldung.examples.guice.FooGenerator;
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;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class GuiceUnitTest {
@Test
public void givenAccountFieldInjectedInGuiceUser_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 givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
Injector injector = Guice.createInjector(new GuiceModule());
BookService bookService = injector.getInstance(BookService.class);
assertNotNull(bookService);
}
@Test
public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() {
Injector injector = Guice.createInjector(new GuiceModule());
FooGenerator fooGenerator = injector.getInstance(FooGenerator.class);
assertNotNull(fooGenerator);
}
@Test
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
Injector injector = Guice.createInjector(new GuiceModule());
Person person = injector.getInstance(Person.class);
assertNotNull(person);
}
@Test
public void givenEmployeeConstructorAnnotatedByInject_WhenEmployeeIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() {
Injector injector = Guice.createInjector(new GuiceModule());
Employee employee = injector.getInstance(Employee.class);
assertNotNull(employee);
assertEquals("Default", employee.getLastName());
}
@Test
public void givenAddressAutowiredToGuiceUserBySetterInjection_WhenGuiceUserIsInjected_ThenAddressInitializedByTheSetter() {
Injector injector = Guice.createInjector(new GuiceModule());
GuiceUser guiceUser = injector.getInstance(GuiceUser.class);
assertNotNull(guiceUser);
assertNotNull(guiceUser.getAddress());
assertEquals("Default", guiceUser.getAddress().getCity());
}
}
package com.baeldung.examples;
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.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;
import com.google.inject.Guice;
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 givenPersonDaoInjectedInGuicePersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
Injector injector = Guice.createInjector(new GuiceModule());
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
assertNotNull(personService);
assertNotNull(personService.getPersonDao());
}
@Test
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
Injector injector = Guice.createInjector(new GuiceModule());
GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class);
assertNotNull(guiceUserService.getAccountService());
}
@Test
public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
Injector injector = Guice.createInjector(new GuiceModule());
BookService bookService = injector.getInstance(BookService.class);
assertNotNull(bookService);
}
@Test
public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() {
Injector injector = Guice.createInjector(new GuiceModule());
FooGenerator fooGenerator = injector.getInstance(FooGenerator.class);
assertNotNull(fooGenerator);
}
@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());
}
}