JUnit + Spring integration example
By:Roy.LiuLast updated:2019-08-17
In this tutorial, we will show you how to test the Spring DI components with JUnit frameworks.
Technologies used :
- JUnit 4.12
- Hamcrest 1.3
- Spring 4.3.0.RELEASE
- Maven
1. Project Dependencies
To integrate Spring with JUnit, you need spring-test.jar
pom.xml
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.0.RELEASE</version> <scope>test</scope> </dependency>
2. Spring Components
A simple Spring components, later for testing.
2.1 An interface.
DataModelService.java
package com.mkyong.examples.spring; public interface DataModelService { boolean isValid(String input);
2.2 Implementation of above interface.
MachineLearningService.java
package com.mkyong.examples.spring; import org.springframework.stereotype.Service; @Service("ml") public class MachineLearningService implements DataModelService { @Override public boolean isValid(String input) { return true;
2.2 A Spring configuration file, component scanning.
AppConfig.java
package com.mkyong.examples.spring; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = {"com.mkyong.examples.spring"}) public class AppConfig {
3. JUnit + Spring Integration Examples
Annotate the JUnit test class with @RunWith(SpringJUnit4ClassRunner.class) and loads the Spring configuration file manually. Refer below :
MachineLearningTest.java
package com.mkyong.spring; import com.mkyong.examples.spring.AppConfig; import com.mkyong.examples.spring.DataModelService; import com.mkyong.examples.spring.MachineLearningService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {AppConfig.class}) public class MachineLearningTest { //DI @Autowired @Qualifier("ml") DataModelService ml; @Test public void test_ml_always_return_true() { //assert correct type/impl assertThat(ml, instanceOf(MachineLearningService.class)); //assert true assertThat(ml.isValid(""), is(true));
Done.
4. FAQs
4.1 For XML, try this :
import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(locations = { "classpath:pathTo/appConfig.xml", "classpath:pathTo/appConfig2.xml"}) public class MachineLearningTest { //...
4.2 For multiple configuration files :
import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(classes = {AppConfig.class, AppConfig2.class}) public class MachineLearningTest { //...
References
- Spring IO – Unit Testing
- Spring IO – Integration Testing
- TestNG + Spring Integration Example
- Spring Batch unit test example
From:一号门
Previous:Unit Test What is Mocking? and Why?
COMMENTS