Merge branch 'master' into BAEL-2527

This commit is contained in:
pcoates 2019-02-06 20:06:42 +00:00
commit 27e8c7f0f2
68 changed files with 539 additions and 9 deletions

View File

@ -15,3 +15,5 @@
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)

View File

@ -1,3 +1,4 @@
### Relevant articles
- [Introduction to Apache Spark](http://www.baeldung.com/apache-spark)
- [Building a Data Pipeline with Kafka, Spark Streaming and Cassandra](https://www.baeldung.com/kafka-spark-data-pipeline)

View File

@ -3,3 +3,4 @@
- [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code)
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)

View File

@ -26,3 +26,4 @@
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)

View File

@ -14,3 +14,4 @@
- [Array Operations in Java](http://www.baeldung.com/java-common-array-operations)
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
- [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array)

View File

@ -31,3 +31,4 @@
- [A Guide to EnumMap](https://www.baeldung.com/java-enum-map)
- [A Guide to Iterator in Java](http://www.baeldung.com/java-iterator)
- [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences)
- [Java ArrayList vs Vector](https://www.baeldung.com/java-arraylist-vs-vector)

View File

@ -38,3 +38,5 @@
- [How to Get the File Extension of a File in Java](http://www.baeldung.com/java-file-extension)
- [Getting a Files Mime Type in Java](http://www.baeldung.com/java-file-mime-type)
- [Create a Directory in Java](https://www.baeldung.com/java-create-directory)
- [How to Write to a CSV File in Java](https://www.baeldung.com/java-csv)
- [List Files in a Directory in Java](https://www.baeldung.com/java-list-directory-files)

View File

@ -16,4 +16,5 @@
- [Quick Guide to java.lang.System](http://www.baeldung.com/java-lang-system)
- [Java Switch Statement](https://www.baeldung.com/java-switch)
- [The Modulo Operator in Java](https://www.baeldung.com/modulo-java)
- [Ternary Operator In Java](https://www.baeldung.com/java-ternary-operator)
- [Ternary Operator In Java](https://www.baeldung.com/java-ternary-operator)
- [Java instanceof Operator](https://www.baeldung.com/java-instanceof)

View File

@ -34,4 +34,10 @@
- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws)
- [Retrieving a Class Name in Java](https://www.baeldung.com/java-class-name)
- [Java Compound Operators](https://www.baeldung.com/java-compound-operators)
- [Guide to Java Packages](https://www.baeldung.com/java-packages)
- [Guide to Java Packages](https://www.baeldung.com/java-packages)
- [The Java Native Keyword and Methods](https://www.baeldung.com/java-native)
- [If-Else Statement in Java](https://www.baeldung.com/java-if-else)
- [Control Structures in Java](https://www.baeldung.com/java-control-structures)
- [Java Interfaces](https://www.baeldung.com/java-interfaces)
- [Attaching Values to Java Enum](https://www.baeldung.com/java-enum-values)
- [Variable Scope in Java](https://www.baeldung.com/java-variable-scope)

View File

@ -5,4 +5,5 @@
- [Different Ways to Capture Java Heap Dumps](https://www.baeldung.com/java-heap-dump-capture)
- [Understanding Memory Leaks in Java](https://www.baeldung.com/java-memory-leaks)
- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded)
- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions)
- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions)
- [Monitoring Java Applications with Flight Recorder](https://www.baeldung.com/java-flight-recorder-monitoring)

View File

@ -8,4 +8,5 @@
- [Encrypting and Decrypting Files in Java](http://www.baeldung.com/java-cipher-input-output-stream)
- [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing)
- [SSL Handshake Failures](https://www.baeldung.com/java-ssl-handshake-failures)
- [SHA-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java)
- [SHA-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java)
- [Enabling TLS v1.2 in Java 7](https://www.baeldung.com/java-7-tls-v12)

View File

@ -47,3 +47,7 @@
- [Console I/O in Java](http://www.baeldung.com/java-console-input-output)
- [Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf)
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
- [Introduction to Basic Syntax in Java](https://www.baeldung.com/java-syntax)
- [Using Curl in Java](https://www.baeldung.com/java-curl)
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
- [Java Bitwise Operators](https://www.baeldung.com/java-bitwise-operators)

View File

@ -51,3 +51,4 @@
- [Operator Overloading in Kotlin](https://www.baeldung.com/kotlin-operator-overloading)
- [Inline Classes in Kotlin](https://www.baeldung.com/kotlin-inline-classes)
- [Creating Java static final Equivalents in Kotlin](https://www.baeldung.com/kotlin-java-static-final)
- [Nested forEach in Kotlin](https://www.baeldung.com/kotlin-nested-foreach)

View File

@ -26,4 +26,4 @@
<guice.version>4.1.0</guice.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,24 @@
package com.baeldung.examples.common;
public class Account {
private String accountNumber;
private String type;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

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

View File

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

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

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

View File

@ -0,0 +1,7 @@
package com.baeldung.examples.common;
public class BookServiceImpl implements BookService {
private AuthorService authorService;
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,9 @@
package com.baeldung.examples.guice;
import com.google.inject.Inject;
public class FooProcessor {
@Inject
private Foo foo;
}

View File

@ -0,0 +1,19 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.common.PersonDao;
import com.google.inject.Inject;
public class GuicePersonService {
@Inject
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.examples.guice;
import com.baeldung.examples.common.AccountService;
import com.google.inject.Inject;
public class GuiceUserService {
@Inject
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.examples.guice;
public class Person {
private String firstName;
private String lastName;
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

@ -0,0 +1,50 @@
package com.baeldung.examples.guice.modules;
import com.baeldung.examples.common.AccountService;
import com.baeldung.examples.common.AccountServiceImpl;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl;
import com.baeldung.examples.common.PersonDao;
import com.baeldung.examples.common.PersonDaoImpl;
import com.baeldung.examples.guice.Foo;
import com.baeldung.examples.guice.Person;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.Provides;
public class GuiceModule extends AbstractModule {
@Override
protected void configure() {
try {
bind(AccountService.class).to(AccountServiceImpl.class);
bind(Person.class).toConstructor(Person.class.getConstructor());
// bind(Person.class).toProvider(new Provider<Person>() {
// public Person get() {
// Person p = new Person();
// return p;
// }
// });
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return new Foo();
}
});
bind(PersonDao.class).to(PersonDaoImpl.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Provides
public BookService bookServiceGenerator() {
return new BookServiceImpl();
}
}

View File

@ -0,0 +1,61 @@
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.FooProcessor;
import com.baeldung.examples.guice.GuicePersonService;
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 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 givenMultipleBindingsForPerson_WhenPersonIsInjected_ThenTestFailsByProvisionException() {
Injector injector = Guice.createInjector(new GuiceModule());
Person person = injector.getInstance(Person.class);
assertNotNull(person);
}
@Test
public void givenFooInjectedToFooProcessorAsOptionalDependency_WhenFooProcessorIsRetrievedFromContext_ThenCreationExceptionIsNotThrown() {
Injector injector = Guice.createInjector(new GuiceModule());
FooProcessor fooProcessor = injector.getInstance(FooProcessor.class);
assertNotNull(fooProcessor);
}
@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());
}
}

View File

@ -37,3 +37,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Serialize Only Fields that meet a Custom Criteria with Jackson](http://www.baeldung.com/jackson-serialize-field-custom-criteria)
- [Mapping Nested Values with Jackson](http://www.baeldung.com/jackson-nested-values)
- [Convert XML to JSON Using Jackson](https://www.baeldung.com/jackson-convert-xml-json)
- [Deserialize Immutable Objects with Jackson](https://www.baeldung.com/jackson-deserialize-immutable-objects)

View File

@ -19,3 +19,4 @@
- [How to Check If a Key Exists in a Map](https://www.baeldung.com/java-map-key-exists)
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
- [Immutable Map Implementations in Java](https://www.baeldung.com/java-immutable-maps)
- [Map to String Conversion in Java](https://www.baeldung.com/java-map-to-string-conversion)

View File

@ -27,4 +27,5 @@
- [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp)
- [Convert between String and Timestamp](https://www.baeldung.com/java-string-to-timestamp)
- [A Guide to SimpleDateFormat](https://www.baeldung.com/java-simple-date-format)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)

View File

@ -16,3 +16,4 @@
- [Introduction to Protonpack](https://www.baeldung.com/java-protonpack)
- [Java Stream Filter with Lambda Expression](https://www.baeldung.com/java-stream-filter-lambda)
- [Counting Matches on a Stream Filter](https://www.baeldung.com/java-stream-filter-count)
- [Java 8 Streams peek() API](https://www.baeldung.com/java-streams-peek-api)

View File

@ -50,3 +50,6 @@
- [Remove Leading and Trailing Characters from a String](https://www.baeldung.com/java-remove-trailing-characters)
- [Concatenating Strings In Java](https://www.baeldung.com/java-strings-concatenation)
- [Java toString() Method](https://www.baeldung.com/java-tostring)
- [Java String Interview Questions and Answers](https://www.baeldung.com/java-string-interview-questions)
- [Check if a String is a Pangram in Java](https://www.baeldung.com/java-string-pangram)
- [Check If a String Contains Multiple Keywords](https://www.baeldung.com/string-contains-multiple-words)

View File

@ -9,3 +9,4 @@
- [Working with Dates in Kotlin](https://www.baeldung.com/kotlin-dates)
- [Introduction to Arrow in Kotlin](https://www.baeldung.com/kotlin-arrow)
- [Kotlin with Ktor](https://www.baeldung.com/kotlin-ktor)
- [REST API With Kotlin and Kovert](https://www.baeldung.com/kotlin-kovert)

View File

@ -7,3 +7,4 @@
- [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic)
- [Testing Netty with EmbeddedChannel](http://www.baeldung.com/testing-netty-embedded-channel)
- [MQTT Client in Java](https://www.baeldung.com/java-mqtt-client)
- [Guide to XMPP Smack Client](https://www.baeldung.com/xmpp-smack-chat-client)

View File

@ -64,7 +64,8 @@
- [Exactly Once Processing in Kafka](https://www.baeldung.com/kafka-exactly-once)
- [An Introduction to SuanShu](https://www.baeldung.com/suanshu)
- [Implementing a FTP-Client in Java](http://www.baeldung.com/java-ftp-client)
- [Introduction to Functional Java](https://www.baeldung.com/java-functional-library
- [Introduction to Functional Java](https://www.baeldung.com/java-functional-library)
- [Intro to Derive4J](https://www.baeldung.com/derive4j)
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.

View File

@ -5,3 +5,4 @@
- [Fixing the JPA error “java.lang.String cannot be cast to Ljava.lang.String;”](https://www.baeldung.com/jpa-error-java-lang-string-cannot-be-cast)
- [JPA Entity Graph](https://www.baeldung.com/jpa-entity-graph)
- [JPA 2.2 Support for Java 8 Date/Time Types](https://www.baeldung.com/jpa-java-time)
- [Converting Between LocalDate and SQL Date](https://www.baeldung.com/java-convert-localdate-sql-date)

View File

@ -6,4 +6,4 @@
- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool)
- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot)
- [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb)
- [Configuring a DataSource Programmatically in Spring Boot](https://www.baeldung.com/spring-boot-configure-data-source-programmatic)

View File

@ -19,6 +19,7 @@
- [Sorting Query Results with Spring Data](https://www.baeldung.com/spring-data-sorting)
- [INSERT Statement in JPA](https://www.baeldung.com/jpa-insert)
- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting)
- [Spring Data JPA Query by Example](https://www.baeldung.com/spring-data-query-by-example)
### Eclipse Config
After importing the project into Eclipse, you may see the following error:

View File

@ -1,3 +1,4 @@
### Relevant Articles:
- [Spring Security OAuth Login with WebFlux](https://www.baeldung.com/spring-oauth-login-webflux)
- [Spring WebClient and OAuth2 Support](https://www.baeldung.com/spring-webclient-oauth2)

View File

@ -5,3 +5,4 @@
- [Deploying a Spring Boot Application to Cloud Foundry](https://www.baeldung.com/spring-boot-app-deploy-to-cloud-foundry)
- [Deploy a Spring Boot Application to Google App Engine](https://www.baeldung.com/spring-boot-google-app-engine)
- [Deploy a Spring Boot Application to OpenShift](https://www.baeldung.com/spring-boot-deploy-openshift)
- [Deploy a Spring Boot Application to AWS Beanstalk](https://www.baeldung.com/spring-boot-deploy-aws-beanstalk)

View File

@ -9,4 +9,5 @@
- [Display RSS Feed with Spring MVC](http://www.baeldung.com/spring-mvc-rss-feed)
- [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao)
- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache)
- [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
- [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
- [Conditionally Enable Scheduled Jobs in Spring](https://www.baeldung.com/spring-scheduled-enabled-conditionally)

View File

@ -22,3 +22,4 @@
- [Spring Application Context Events](https://www.baeldung.com/spring-context-events)
- [Unsatisfied Dependency in Spring](https://www.baeldung.com/spring-unsatisfied-dependency)
- [What is a Spring Bean?](https://www.baeldung.com/spring-bean)
- [Spring PostConstruct and PreDestroy Annotations](https://www.baeldung.com/spring-postconstruct-predestroy)

View File

@ -0,0 +1,27 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class Account {
private String accountNumber;
private String type;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AccountService {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface BookService {
}

View File

@ -0,0 +1,10 @@
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
public class BookServiceImpl implements BookService {
@Autowired(required = false)
private AuthorService authorService;
}

View File

@ -0,0 +1,4 @@
package com.baeldung.di.spring;
public class Foo {
}

View File

@ -0,0 +1,6 @@
package com.baeldung.di.spring;
public class FooProcessor {
private Foo foo;
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface PersonDao {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class PersonDaoImpl implements PersonDao {
}

View File

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

View File

@ -0,0 +1,18 @@
package com.baeldung.di.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;
@Configuration
@Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.di.spring")
public class SpringMainConfig {
@Bean
public BookService bookServiceGenerator() {
return new BookServiceImpl();
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SpringPersonService {
@Autowired
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class UserService {
@Autowired
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.di.spring;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { SpringMainConfig.class })
public class SpringUnitTest {
@Autowired
ApplicationContext context;
@Test
public void givenAccountServiceAutowiredToUserService_WhenGetAccountServiceInvoked_ThenReturnValueIsNotNull() {
UserService userService = context.getBean(UserService.class);
assertNotNull(userService.getAccountService());
}
@Test
public void givenBookServiceIsRegisteredAsBeanInContext_WhenBookServiceIsRetrievedFromContext_ThenReturnValueIsNotNull() {
BookService bookService = context.getBean(BookService.class);
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
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());
}
}

View File

@ -31,6 +31,7 @@ public class SecurityJsonViewControllerAdvice extends AbstractMappingJacksonResp
.collect(Collectors.toList());
if (jsonViews.size() == 1) {
bodyContainer.setSerializationView(jsonViews.get(0));
return;
}
throw new IllegalArgumentException("Ambiguous @JsonView declaration for roles "+ authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(",")));
}

View File

@ -16,3 +16,4 @@
- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java)
- [Testing an Abstract Class With JUnit](https://www.baeldung.com/junit-test-abstract-class)
- [A Quick JUnit vs TestNG Comparison](http://www.baeldung.com/junit-vs-testng)
- [Guide to JUnit 5 Parameterized Tests](https://www.baeldung.com/parameterized-tests-junit-5)