ssm项目使用junit进行单元测试


 

1、在maven中导入

junit-4.12.jar;
spring-test.jar;(版本号要与项目spring版本一致)
hamcrest-core-1.3.jar;(这个包是配合junit使用,不然在导入SpringJUnit4ClassRunner.class时里面继承的BlockJUnit4ClassRunner无法引用)


2、代码实例
  
package cn.ljs.util.InterfaceDemo;

import cn.ljs.entity.PurchaseBidNotice;
import cn.ljs.service.FBYQNoticeService;
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 java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-tx.xml"})
public class TestDemo {

@Autowired
FBYQNoticeService fbyqNoticeService;

@Test
public void test(){
PurchaseBidNotice fb = new PurchaseBidNotice();
List<Map<String,Object>> list = fbyqNoticeService.findAll(fb);
System.out.println(list);
}

}


总结:在学习用spring注入完成junit单元测试时我遇到了以下几个错误
    1、spring-test.jar是项目spring版本不一致;
    2、junit-4.12.jar与
hamcrest-core-1.3.jar是相辅相成的,想要正常使用就必须两个都引用,我用idea开发junit要到用4.12版本才能用,4.11都不行;
     3、spring-tx.xml、spring-mybatis.xml、spring-mvc.xml用<import/>引入相互之间的调用,不要容易出现xx无法引用;
    4、如果在spring配置文件里使用了
<mvc:resources/>标签进行静态资源的调用,会报错影响junit运行,建议在做单元时注释;
  
 
 
 
 

					


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM