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

This commit is contained in:
emineoymak 2019-01-01 03:25:55 -05:00
commit 4375c0d450
24 changed files with 180 additions and 321 deletions

View File

@ -37,7 +37,7 @@
</dependencies>
<properties>
<guice.version>4.1.0</guice.version>
<guice.version>4.2.2</guice.version>
<spring.version>5.1.3.RELEASE</spring.version>
<springtest.version>5.1.3.RELEASE</springtest.version>
</properties>

View File

@ -5,23 +5,23 @@ import org.springframework.stereotype.Component;
@Component
public class Account {
private String accountNumber;
private String type;
private String accountNumber;
private String type;
public String getAccountNumber() {
return accountNumber;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getType() {
return type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import java.util.List;
public interface AccountService {
public List<String> listAccountTypes();
}

View File

@ -1,15 +1,8 @@
package com.baeldung.examples.common;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
public List<String> listAccountTypes() {
return Arrays.asList("Checking", "Saving");
}
}

View File

@ -1,35 +0,0 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class Address {
private String city;
private String state;
private String county;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.examples.common;
public interface AudioBookService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.examples.common;
public class AudioBookServiceImpl implements AudioBookService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.examples.common;
public interface AuthorService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.examples.common;
public class AuthorServiceImpl implements AuthorService {
}

View File

@ -1,9 +1,5 @@
package com.baeldung.examples.common;
import java.util.List;
public interface BookService {
public List<String> findBestSellerBooks();
}

View File

@ -1,12 +1,10 @@
package com.baeldung.examples.common;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
public class BookServiceImpl implements BookService {
public List<String> findBestSellerBooks() {
return Arrays.asList("Harry Potter", "Lord of The Rings");
}
@Autowired(required = false)
private AuthorService authorService;
}

View File

@ -1,32 +0,0 @@
package com.baeldung.examples.guice;
import com.google.inject.Inject;
public class Employee {
private String firstName;
private String lastName;
@Inject
public Employee(String firstName) {
this.firstName = firstName;
this.lastName = "Default";
}
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;
}
}

View File

@ -1,5 +1,4 @@
package com.baeldung.examples.guice;
public class Foo {
}

View File

@ -4,8 +4,9 @@ import org.springframework.lang.Nullable;
import com.google.inject.Inject;
public class FooGenerator {
@Inject
public FooGenerator(@Nullable Foo foo) {
}
public class FooProcessor {
@Inject
@Nullable
private Foo foo;
}

View File

@ -1,32 +0,0 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.common.Account;
import com.baeldung.examples.common.Address;
import com.google.inject.Inject;
public class GuiceUser {
@Inject
private Account account;
private Address address;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Address getAddress() {
return address;
}
@Inject
public void setAddress(Address address) {
this.address = address;
address.setCity("Default");
}
}

View File

@ -5,15 +5,15 @@ import com.google.inject.Inject;
public class GuiceUserService {
@Inject
private AccountService accountService;
@Inject
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}

View File

@ -1,39 +1,24 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.common.Address;
import com.google.inject.Inject;
public class Person {
private String firstName;
private String firstName;
private String lastName;
private String lastName;
private Address address;
public String getFirstName() {
return firstName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Address getAddress() {
return address;
}
@Inject
public void setAddress(Address address) {
this.address = address;
address.setCity("Default");
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -17,14 +17,7 @@ public class GuiceModule extends AbstractModule {
@Override
protected void configure() {
try {
bind(PersonDao.class).to(PersonDaoImpl.class);
bind(AccountService.class).to(AccountServiceImpl.class);
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return null;
}
});
bind(Person.class).toConstructor(Person.class.getConstructor());
// bind(Person.class).toProvider(new Provider<Person>() {
// public Person get() {
@ -32,6 +25,13 @@ public class GuiceModule extends AbstractModule {
// return p;
// }
// });
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return null;
}
});
bind(PersonDao.class).to(PersonDaoImpl.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();

View File

@ -0,0 +1,17 @@
package com.baeldung.examples.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.AudioBookServiceImpl;
@Configuration
public class SpringBeansConfig {
@Bean
public AudioBookService audioBookServiceGenerator() {
return new AudioBookServiceImpl();
}
}

View File

@ -3,17 +3,19 @@ package com.baeldung.examples.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl;
@Configuration
@Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.examples")
public class AppConfig {
public class SpringMainConfig {
@Bean
public BookService bookServiceGenerator() {
return new BookServiceImpl();
}
@Bean
public BookService bookServiceGenerator() {
return new BookServiceImpl();
}
}

View File

@ -1,35 +0,0 @@
package com.baeldung.examples.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baeldung.examples.common.Account;
import com.baeldung.examples.common.Address;
@Component
public class SpringUser {
@Autowired
private Account account;
private Address address;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Address getAddress() {
return address;
}
@Autowired
public void setAddress(Address address) {
this.address = address;
address.setCity("Default");
}
}

View File

@ -8,15 +8,15 @@ import com.baeldung.examples.common.AccountService;
@Component
public class UserService {
@Autowired
private AccountService accountService;
@Autowired
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}