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