JUnit + Spring integration example

摘要: In this tutorial, we will show you how to test the Spring DI components with JUnit frameworks.

In this tutorial, we will show you how to test the Spring DI components with JUnit frameworks.

Technologies used :

  1. JUnit 4.12
  2. Hamcrest 1.3
  3. Spring 4.3.0.RELEASE
  4. 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

  1. Spring IO – Unit Testing
  2. Spring IO – Integration Testing
  3. TestNG + Spring Integration Example
  4. Spring Batch unit test example

上一篇: Unit Test What is Mocking? and Why?
下一篇: JUnit Categories Test
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号