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> </dependencies>
<properties> <properties>
<guice.version>4.1.0</guice.version> <guice.version>4.2.2</guice.version>
<spring.version>5.1.3.RELEASE</spring.version> <spring.version>5.1.3.RELEASE</spring.version>
<springtest.version>5.1.3.RELEASE</springtest.version> <springtest.version>5.1.3.RELEASE</springtest.version>
</properties> </properties>

View File

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

View File

@ -1,15 +1,8 @@
package com.baeldung.examples.common; package com.baeldung.examples.common;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class AccountServiceImpl implements AccountService { 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; package com.baeldung.examples.common;
import java.util.List;
public interface BookService { public interface BookService {
public List<String> findBestSellerBooks();
} }

View File

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

View File

@ -4,8 +4,9 @@ import org.springframework.lang.Nullable;
import com.google.inject.Inject; import com.google.inject.Inject;
public class FooGenerator { public class FooProcessor {
@Inject @Inject
public FooGenerator(@Nullable Foo foo) { @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

@ -1,15 +1,10 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import com.baeldung.examples.common.Address;
import com.google.inject.Inject;
public class Person { public class Person {
private String firstName; private String firstName;
private String lastName; private String lastName;
private Address address;
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }
@ -26,14 +21,4 @@ public class Person {
this.lastName = lastName; this.lastName = lastName;
} }
public Address getAddress() {
return address;
}
@Inject
public void setAddress(Address address) {
this.address = address;
address.setCity("Default");
}
} }

View File

@ -17,14 +17,7 @@ public class GuiceModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
try { try {
bind(PersonDao.class).to(PersonDaoImpl.class);
bind(AccountService.class).to(AccountServiceImpl.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).toConstructor(Person.class.getConstructor());
// bind(Person.class).toProvider(new Provider<Person>() { // bind(Person.class).toProvider(new Provider<Person>() {
// public Person get() { // public Person get() {
@ -32,6 +25,13 @@ public class GuiceModule extends AbstractModule {
// return p; // return p;
// } // }
// }); // });
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return null;
}
});
bind(PersonDao.class).to(PersonDaoImpl.class);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); 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,13 +3,15 @@ package com.baeldung.examples.spring;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.baeldung.examples.common.BookService; import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl; import com.baeldung.examples.common.BookServiceImpl;
@Configuration @Configuration
@Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.examples") @ComponentScan("com.baeldung.examples")
public class AppConfig { public class SpringMainConfig {
@Bean @Bean
public BookService bookServiceGenerator() { public BookService bookServiceGenerator() {

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

@ -5,9 +5,8 @@ import static org.junit.Assert.assertNotNull;
import org.junit.Test; import org.junit.Test;
import com.baeldung.examples.common.BookService; 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.GuicePersonService;
import com.baeldung.examples.guice.GuiceUser;
import com.baeldung.examples.guice.GuiceUserService; import com.baeldung.examples.guice.GuiceUserService;
import com.baeldung.examples.guice.Person; import com.baeldung.examples.guice.Person;
import com.baeldung.examples.guice.modules.GuiceModule; import com.baeldung.examples.guice.modules.GuiceModule;
@ -16,21 +15,6 @@ import com.google.inject.Injector;
public class GuiceUnitTest { 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 @Test
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() { public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
Injector injector = Guice.createInjector(new GuiceModule()); Injector injector = Guice.createInjector(new GuiceModule());
@ -45,13 +29,6 @@ public class GuiceUnitTest {
assertNotNull(bookService); assertNotNull(bookService);
} }
@Test
public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() {
Injector injector = Guice.createInjector(new GuiceModule());
FooGenerator fooGenerator = injector.getInstance(FooGenerator.class);
assertNotNull(fooGenerator);
}
@Test @Test
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() { public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
Injector injector = Guice.createInjector(new GuiceModule()); Injector injector = Guice.createInjector(new GuiceModule());
@ -59,6 +36,13 @@ public class GuiceUnitTest {
assertNotNull(person); assertNotNull(person);
} }
@Test
public void givenFooInjectedToFooProcessorAsOptionalDependency_WhenFooProcessorIsRetrievedFromContext_ThenCreationExceptionIsNotThrown() {
Injector injector = Guice.createInjector(new GuiceModule());
FooProcessor fooProcessor = injector.getInstance(FooProcessor.class);
assertNotNull(fooProcessor);
}
@Test @Test
public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() { public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() {
Injector injector = Guice.createInjector(new GuiceModule()); Injector injector = Guice.createInjector(new GuiceModule());

View File

@ -9,31 +9,18 @@ import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.BookService; 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.SpringPersonService;
import com.baeldung.examples.spring.SpringUser;
import com.baeldung.examples.spring.UserService; import com.baeldung.examples.spring.UserService;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = { AppConfig.class }) @ContextConfiguration(classes = { SpringMainConfig.class })
public class SpringUnitTest { public class SpringUnitTest {
@Autowired @Autowired
ApplicationContext context; ApplicationContext context;
@Test
public void givenAccountAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
SpringUser springUser = context.getBean(SpringUser.class);
assertNotNull(springUser.getAccount());
}
@Test
public void givenPersonDaoAutowiredToSpringPersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
SpringPersonService personService = context.getBean(SpringPersonService.class);
assertNotNull(personService);
assertNotNull(personService.getPersonDao());
}
@Test @Test
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() { public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
UserService userService = context.getBean(UserService.class); UserService userService = context.getBean(UserService.class);
@ -46,6 +33,20 @@ public class SpringUnitTest {
assertNotNull(bookService); assertNotNull(bookService);
} }
@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 givenAuthorServiceAutowiredToBookServiceAsOptionalDependency_WhenBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsNotThrown() {
BookService bookService = context.getBean(BookService.class);
assertNotNull(bookService);
}
@Test @Test
public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() { public void givenSpringPersonServiceConstructorAnnotatedByAutowired_WhenSpringPersonServiceIsRetrievedFromContext_ThenInstanceWillBeCreatedFromTheConstructor() {
SpringPersonService personService = context.getBean(SpringPersonService.class); SpringPersonService personService = context.getBean(SpringPersonService.class);