Merge remote-tracking branch 'origin/BAEL-2399' into BAEL-2399
This commit is contained in:
commit
4375c0d450
|
@ -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>
|
||||||
|
|
|
@ -5,23 +5,23 @@ import org.springframework.stereotype.Component;
|
||||||
@Component
|
@Component
|
||||||
public class Account {
|
public class Account {
|
||||||
|
|
||||||
private String accountNumber;
|
private String accountNumber;
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
public String getAccountNumber() {
|
public String getAccountNumber() {
|
||||||
return accountNumber;
|
return accountNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountNumber(String accountNumber) {
|
public void setAccountNumber(String accountNumber) {
|
||||||
this.accountNumber = accountNumber;
|
this.accountNumber = accountNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(String type) {
|
public void setType(String type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.examples.common;
|
||||||
|
|
||||||
|
public interface AudioBookService {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.examples.common;
|
||||||
|
|
||||||
|
public class AudioBookServiceImpl implements AudioBookService {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.examples.common;
|
||||||
|
|
||||||
|
public interface AuthorService {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.examples.common;
|
||||||
|
|
||||||
|
public class AuthorServiceImpl implements AuthorService {
|
||||||
|
|
||||||
|
}
|
|
@ -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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,5 +1,4 @@
|
||||||
package com.baeldung.examples.guice;
|
package com.baeldung.examples.guice;
|
||||||
|
|
||||||
|
|
||||||
public class Foo {
|
public class Foo {
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
||||||
public FooGenerator(@Nullable Foo foo) {
|
@Inject
|
||||||
}
|
@Nullable
|
||||||
|
private Foo foo;
|
||||||
}
|
}
|
|
@ -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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -5,15 +5,15 @@ import com.google.inject.Inject;
|
||||||
|
|
||||||
public class GuiceUserService {
|
public class GuiceUserService {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private AccountService accountService;
|
private AccountService accountService;
|
||||||
|
|
||||||
public AccountService getAccountService() {
|
public AccountService getAccountService() {
|
||||||
return accountService;
|
return accountService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountService(AccountService accountService) {
|
public void setAccountService(AccountService accountService) {
|
||||||
this.accountService = accountService;
|
this.accountService = accountService;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +1,24 @@
|
||||||
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() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public void setFirstName(String firstName) {
|
||||||
return firstName;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public String getLastName() {
|
||||||
this.firstName = firstName;
|
return lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastName() {
|
public void setLastName(String lastName) {
|
||||||
return lastName;
|
this.lastName = 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,17 +3,19 @@ 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() {
|
||||||
return new BookServiceImpl();
|
return new BookServiceImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -8,15 +8,15 @@ import com.baeldung.examples.common.AccountService;
|
||||||
@Component
|
@Component
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AccountService accountService;
|
private AccountService accountService;
|
||||||
|
|
||||||
public AccountService getAccountService() {
|
public AccountService getAccountService() {
|
||||||
return accountService;
|
return accountService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountService(AccountService accountService) {
|
public void setAccountService(AccountService accountService) {
|
||||||
this.accountService = accountService;
|
this.accountService = accountService;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,62 +15,47 @@ import com.google.inject.Injector;
|
||||||
|
|
||||||
public class GuiceUnitTest {
|
public class GuiceUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccountInjectedInGuiceUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
|
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||||
Injector injector = Guice.createInjector(new GuiceModule());
|
Injector injector = Guice.createInjector(new GuiceModule());
|
||||||
GuiceUser guiceUser = injector.getInstance(GuiceUser.class);
|
GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class);
|
||||||
assertNotNull(guiceUser.getAccount());
|
assertNotNull(guiceUserService.getAccountService());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersonDaoInjectedInGuicePersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
|
public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
|
||||||
Injector injector = Guice.createInjector(new GuiceModule());
|
Injector injector = Guice.createInjector(new GuiceModule());
|
||||||
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
BookService bookService = injector.getInstance(BookService.class);
|
||||||
assertNotNull(personService);
|
assertNotNull(bookService);
|
||||||
assertNotNull(personService.getPersonDao());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccountServiceInjectedInGuiceUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
|
||||||
Injector injector = Guice.createInjector(new GuiceModule());
|
Injector injector = Guice.createInjector(new GuiceModule());
|
||||||
GuiceUserService guiceUserService = injector.getInstance(GuiceUserService.class);
|
Person person = injector.getInstance(Person.class);
|
||||||
assertNotNull(guiceUserService.getAccountService());
|
assertNotNull(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBookServiceIsRegisteredInModule_WhenBookServiceIsInjected_ThenReturnValueIsNotNull() {
|
public void givenFooInjectedToFooProcessorAsOptionalDependency_WhenFooProcessorIsRetrievedFromContext_ThenCreationExceptionIsNotThrown() {
|
||||||
Injector injector = Guice.createInjector(new GuiceModule());
|
Injector injector = Guice.createInjector(new GuiceModule());
|
||||||
BookService bookService = injector.getInstance(BookService.class);
|
FooProcessor fooProcessor = injector.getInstance(FooProcessor.class);
|
||||||
assertNotNull(bookService);
|
assertNotNull(fooProcessor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFooGeneratorConstructorParameterIsNotNullable_WhenFooGeneratorIsInjected_ThenTestFailsByProvisionException() {
|
public void givenGuicePersonServiceConstructorAnnotatedByInject_WhenGuicePersonServiceIsInjected_ThenInstanceWillBeCreatedFromTheConstructor() {
|
||||||
Injector injector = Guice.createInjector(new GuiceModule());
|
Injector injector = Guice.createInjector(new GuiceModule());
|
||||||
FooGenerator fooGenerator = injector.getInstance(FooGenerator.class);
|
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||||
assertNotNull(fooGenerator);
|
assertNotNull(personService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
|
public void givenPersonDaoInjectedToGuicePersonServiceBySetterInjection_WhenGuicePersonServiceIsInjected_ThenPersonDaoInitializedByTheSetter() {
|
||||||
Injector injector = Guice.createInjector(new GuiceModule());
|
Injector injector = Guice.createInjector(new GuiceModule());
|
||||||
Person person = injector.getInstance(Person.class);
|
GuicePersonService personService = injector.getInstance(GuicePersonService.class);
|
||||||
assertNotNull(person);
|
assertNotNull(personService);
|
||||||
}
|
assertNotNull(personService.getPersonDao());
|
||||||
|
}
|
||||||
@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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,54 +9,55 @@ 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
|
@Test
|
||||||
public void givenAccountAutowiredToSpringUser_WhenGetAccountInvoked_ThenReturnValueIsNotNull() {
|
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
||||||
SpringUser springUser = context.getBean(SpringUser.class);
|
UserService userService = context.getBean(UserService.class);
|
||||||
assertNotNull(springUser.getAccount());
|
assertNotNull(userService.getAccountService());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersonDaoAutowiredToSpringPersonService_WhenGetPersonDaoInvoked_ThenReturnValueIsNotNull() {
|
public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() {
|
||||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
BookService bookService = context.getBean(BookService.class);
|
||||||
assertNotNull(personService);
|
assertNotNull(bookService);
|
||||||
assertNotNull(personService.getPersonDao());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
|
public void givenBookServiceIsRegisteredAsBeanInContextByOverridingAudioBookService_WhenAudioBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsThrown() {
|
||||||
UserService userService = context.getBean(UserService.class);
|
BookService bookService = context.getBean(BookService.class);
|
||||||
assertNotNull(userService.getAccountService());
|
assertNotNull(bookService);
|
||||||
}
|
AudioBookService audioBookService = context.getBean(AudioBookService.class);
|
||||||
|
assertNotNull(audioBookService);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() {
|
public void givenAuthorServiceAutowiredToBookServiceAsOptionalDependency_WhenBookServiceIsRetrievedFromContext_ThenNoSuchBeanDefinitionExceptionIsNotThrown() {
|
||||||
BookService bookService = context.getBean(BookService.class);
|
BookService bookService = context.getBean(BookService.class);
|
||||||
assertNotNull(bookService);
|
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);
|
||||||
assertNotNull(personService);
|
assertNotNull(personService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() {
|
public void givenPersonDaoAutowiredToSpringPersonServiceBySetterInjection_WhenSpringPersonServiceRetrievedFromContext_ThenPersonDaoInitializedByTheSetter() {
|
||||||
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
SpringPersonService personService = context.getBean(SpringPersonService.class);
|
||||||
assertNotNull(personService);
|
assertNotNull(personService);
|
||||||
assertNotNull(personService.getPersonDao());
|
assertNotNull(personService.getPersonDao());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue