首页 > 编程语言 > 详细

基于spring的单元测试

时间:2020-07-24 00:32:22      阅读:89      评论:0      收藏:0      [点我收藏+]

1.需要引入的依赖

     <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>

2.单元测试的代码

import com.woniu.erp.Mapper.GoodsManagerMapper;
import com.woniu.erp.entity.Good;
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 java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)//固定的内容
@ContextConfiguration(locations = "classpath:config/SpringContextConfiguration.xml")//这个引用spring的主配值文件,注意路径要正确
public class Test {

    @Autowired
    GoodsManagerMapper goodsManagerMapper ;//依赖注入

    @org.junit.Test
    public void test(){
        List<Good> goodsList = goodsManagerMapper.getGoodsList(); //调用方法测试
        System.out.println(goodsList);
    }
}

技术分享图片

总结:1.需要引入spring单元测试的依赖

   2.需要在类上面加@RunWith注解和@ContextConfiguration注解

   3.对属性进行依赖注入

   4.在test中调用方法测试

基于spring的单元测试

原文:https://www.cnblogs.com/yyk520/p/13369380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!