spring junit整合单元测试

1. 前言:

    我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在程序中只用到该函数的一小部分功能,并且经过调试可以确定,这一小部分功能是正确的。但是,我们同时应该确保每一个函数都完全正确,因为如果我们今后如果对程序进行扩展,用到了某个函数的其他功能,而这个功能有bug的话,那绝对是一件非常郁闷的事情。所以说,每编写完一个函数之后,都应该对这个函数的方方面面进行测试,这样的测试我们称之为单元测试。传统的编程方式,进行单元测试是一件很麻烦的事情,你要重新写另外一个程序,在该程序中调用你需要测试的方法,并且仔细观察运行结果,看看是否有错。正因为如此麻烦,所以程序员们编写单元测试的热情不是很高。于是有一个牛人推出了单元测试包,大大简化了进行单元测试所要做的工作,这就是JUnit4。本文简要介绍一下Spring 整合 JUnit4进行单元测试的方法。

配置成功后,可以单个函数测试,也可以maven install 的时候所有测试单元都跑一遍,避免每次不小心修改了错误代码而不知道。

2. 配置:

<plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.4.2</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>

      </plugin>

 

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

 

2. 代码:

(1) 测试基类

/**
 * Project Name:transengine-service
 * File Name:BaseSpringTest.java
 * Package Name:com.jlpay.test.base
 * Date:2018330日上午9:14:07
 * Copyright (c) 2018, lishaofeng  [email protected] All rights reserved.
 *
 */

package com.jlpay.test.common;
import org.junit.Ignore;
import org.junit.runner.RunWith;
/**
 * ClassName: BaseSpringTest <br/>
 * Function: TODO 类的作用:. <br/>
 * @author lishaofeng  [email protected]
 * @date: 2018330 上午9:14:07 <br/>
 * @version
 * @since JDK 1.8
 */
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@Ignore
@ContextConfiguration(locations = { "classpath*:applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseSpringTest extends AbstractJUnit4SpringContextTests {
}

(2) 具体测试类继承基类:


import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.jlpay.online.route.rpc.model.request.RouteRequestModel;
import com.jlpay.online.route.rpc.model.response.RouteResponseModel;
import com.jlpay.online.route.rpc.service.RouteRpcService;
import com.jlpay.test.common.BaseSpringTest;

/**
 *
 * ClassName: TestPayTradeOrderRpcService <br/>
 * Function: TODO 类的作用:. <br/>
 *
 * @author lishaofeng [email protected]
 * @date: 20171024 上午11:12:31 <br/>
 * @version
 * @since JDK 1.8
 */
public class TestRouteRpcService extends BaseSpringTest{
    //    private static final Logger logger = Logger.getLogger(TestRouteRpcService.class);

    @Autowired
    RouteRpcService service ;
    @Test
    public  void test() {

        RouteRequestModel request = new RouteRequestModel();

        request.setAmount(300000);
        request.setBizSubType("9303");
        request.setBizType("9003");
        request.setMerNo("test123");
        request.setTradeTime("20171103175000");
        request.setCardType("0");
        System.out.println(JSON.toJSONString(request));
        RouteResponseModel response = null;

        try {
            response = (RouteResponseModel) service.execute(request);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Assert.assertTrue(true);
        }
        System.out.println(JSON.toJSONString(response));
        Assert.assertTrue("00".equals(response.getRetCode()));

        System.out.println("------------------------------end");


    }

}

3. 运行方式:

(1) 单个类运行

 

(2) 所有测试类运行

 

4. 常见错误:

(1) java.lang.Exception: No runnable methods?不需要测试的类加注解@Ignore

(2)NoClassDefFoundError: org/junit/runners/model/MultipleFailureException junit版本太低 4.12版本


注意:不想打包的时候自动测试则加  <skipTests>true</skipTests>

             <plugin >
                <groupId>org.apache.maven.plugins</groupId >
                <artifactId>maven-surefire-plugin</artifactId >
                   <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
            </plugin >

 

猜你喜欢

转载自blog.csdn.net/qq_15288023/article/details/79815274