Junit4 针对Service接口的单元测试

作者:Java兔
参考资料:http://blog.csdn.net/u013041642/article/details/71430293

测试环境:
SpringMvc + Spring + Mybatis+ Maven

测试版本:
Junit : 4.12
Spring + SpringMvc : 4.2.3.RELEASE

测试文件:

package hjp;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSONObject;
import com.wx.app.ygp.service.comm.AutoGenerateCustCodeService;

/**
 * 针对AutoGenerateCustCodeService接口的单元测试
 * @author huangjp
 * 2017年7月24日 下午6:10:38
 * 遇到问题:
 * Error creating bean with name 'loginController'(途中一直以为是拦截器的问题,虽然问题解决了,但仍然不明白其中的原理)
 * 原因:
 * @ContextConfiguration中加入了"classpath:spring-mvc.xml"配置导致
 * 解决方案 : 
 * 1、删除"classpath:spring-mvc.xml"配置
 * 2、spring-mvc.xml文件中删除针对action包的扫描配置(<context:component-scan base-package = "com.wx.app.ygp.action"....>)
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-core.xml","classpath:spring-mybatis.xml",
"classpath:spring-ht.xml","classpath:spring-task.xml"})
public class AutoGenerateCustCodeTest{

    @Autowired
    private AutoGenerateCustCodeService autoGenerateCustCodeService;

    @Test
    public void test() throws Exception { 

        //区域编码
        String regionCode = "3604250013";
        //部门编码
        String deptCode = "000101";
        //需要自动生成的户号的个数
        int length = 2;

        JSONObject result = autoGenerateCustCodeService.generateCustCode(regionCode, deptCode, length);
        System.out.println(result.toJSONString());

    }

}

具体的配置文件的配置可以参考参考资料。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章