|
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 | | - |
8 | | -import com.baeldung.examples.common.BookService; |
9 | | -import com.baeldung.examples.guice.Employee; |
10 | | -import com.baeldung.examples.guice.FooGenerator; |
11 | | -import com.baeldung.examples.guice.GuiceUser; |
12 | | -import com.baeldung.examples.guice.GuiceUserService; |
13 | | -import com.baeldung.examples.guice.Person; |
14 | | -import com.baeldung.examples.guice.modules.GuiceModule; |
15 | | -import com.google.inject.Guice; |
16 | | -import com.google.inject.Injector; |
17 | | - |
18 | | -public class GuiceUnitTest { |
19 | | - |
20 | | - @Test |
21 | | - public void givenAccountFieldInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { |
22 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
23 | | - GuiceUser guiceUser = injector.getInstance(GuiceUser.class); |
24 | | - assertNotNull(guiceUser.getAccount()); |
25 | | - } |
26 | | - |
27 | | - @Test |
28 | | - public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() { |
29 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
30 | | - GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class); |
31 | | - assertNotNull(guiceUserService.getAccountService()); |
32 | | - } |
33 | | - |
34 | | - @Test |
35 | | - public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() { |
36 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
37 | | - BookService bookService = injector.getInstance(BookService.class); |
38 | | - assertNotNull(bookService); |
39 | | - } |
40 | | - |
41 | | - @Test |
42 | | - public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() { |
43 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
44 | | - FooGenerator fooGenerator = injector.getInstance(FooGenerator.class); |
45 | | - assertNotNull(fooGenerator); |
46 | | - } |
47 | | - |
48 | | - @Test |
49 | | - public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() { |
50 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
51 | | - Person person = injector.getInstance(Person.class); |
52 | | - assertNotNull(person); |
53 | | - } |
54 | | - |
55 | | - @Test |
56 | | - public void givenEmployeeConstructorAnnotatedByInject_WhenEmployeeIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() { |
57 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
58 | | - Employee employee = injector.getInstance(Employee.class); |
59 | | - assertNotNull(employee); |
60 | | - assertEquals("Default", employee.getLastName()); |
61 | | - } |
62 | | - |
63 | | - @Test |
64 | | - public void givenAddressAutowiredToGuiceUserBySetterInjection_WhenGuiceUserIsInjected_ThenAddressInitializedByTheSetter() { |
65 | | - Injector injector = Guice.createInjector(new GuiceModule()); |
66 | | - GuiceUser guiceUser = injector.getInstance(GuiceUser.class); |
67 | | - assertNotNull(guiceUser); |
68 | | - assertNotNull(guiceUser.getAddress()); |
69 | | - assertEquals("Default", guiceUser.getAddress().getCity()); |
70 | | - } |
71 | | - |
72 | | -} |
| 1 | +package com.baeldung.examples; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | + |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import com.baeldung.examples.common.BookService; |
| 8 | +import com.baeldung.examples.guice.FooGenerator; |
| 9 | +import com.baeldung.examples.guice.GuicePersonService; |
| 10 | +import com.baeldung.examples.guice.GuiceUser; |
| 11 | +import com.baeldung.examples.guice.GuiceUserService; |
| 12 | +import com.baeldung.examples.guice.Person; |
| 13 | +import com.baeldung.examples.guice.modules.GuiceModule; |
| 14 | +import com.google.inject.Guice; |
| 15 | +import com.google.inject.Injector; |
| 16 | + |
| 17 | +public class GuiceUnitTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void givenAccountInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() { |
| 21 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 22 | + GuiceUser guiceUser = injector.getInstance(GuiceUser.class); |
| 23 | + assertNotNull(guiceUser.getAccount()); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void givenPersonDaoInjectedInGuicePersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() { |
| 28 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 29 | + GuicePersonService personService = injector.getInstance(GuicePersonService.class); |
| 30 | + assertNotNull(personService); |
| 31 | + assertNotNull(personService.getPersonDao()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() { |
| 36 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 37 | + GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class); |
| 38 | + assertNotNull(guiceUserService.getAccountService()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() { |
| 43 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 44 | + BookService bookService = injector.getInstance(BookService.class); |
| 45 | + assertNotNull(bookService); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() { |
| 50 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 51 | + FooGenerator fooGenerator = injector.getInstance(FooGenerator.class); |
| 52 | + assertNotNull(fooGenerator); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() { |
| 57 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 58 | + Person person = injector.getInstance(Person.class); |
| 59 | + assertNotNull(person); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() { |
| 64 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 65 | + GuicePersonService personService = injector.getInstance(GuicePersonService.class); |
| 66 | + assertNotNull(personService); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenPersonDaoInjectedToGuicePersonServiceBySetterInjection_WhenGuicePersonServiceIsInjected_ThenPersonDaoInitializedByTheSetter() { |
| 71 | + Injector injector = Guice.createInjector(new GuiceModule()); |
| 72 | + GuicePersonService personService = injector.getInstance(GuicePersonService.class); |
| 73 | + assertNotNull(personService); |
| 74 | + assertNotNull(personService.getPersonDao()); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments