您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
IDEA中利用JUnit進(jìn)行單元測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/8/1 17:19:41 ] 推薦標(biāo)簽:單元測試

 打開IntelliJ IDEA工具,Alt+Ctrl+S,彈出窗口如下:

在文本框中輸入Plugin進(jìn)行插件搜索設(shè)置。

點(diǎn)擊按鈕,從插件資源庫中安裝新的插件。
從插件資源庫中搜索JunitGenerator V2.0版本,在插件位置,鼠標(biāo)右擊



選擇Download and Install ,在彈出的對話框中選擇yes按鈕,點(diǎn)擊OK之后在需要重啟下工具,選擇Restart按鈕,到此JunitGenerator2.0 插件安裝完畢.
現(xiàn)在可通過此工具自動完成test類的生成了,在需要進(jìn)行單元測試的類中Alt+Insert,

測試類中使用的相關(guān)注解跟代碼如下:
 

打開IntelliJ IDEA工具,Alt+Ctrl+S,彈出窗口如下:


在文本框中輸入Plugin進(jìn)行插件搜索設(shè)置。

 

點(diǎn)擊按鈕,從插件資源庫中安裝新的插件。

從插件資源庫中搜索JunitGenerator V2.0版本,在插件位置,鼠標(biāo)右擊

 

 

 

選擇Download and Install ,在彈出的對話框中選擇yes按鈕,點(diǎn)擊OK之后在需要重啟下工具,選擇Restart按鈕,到此JunitGenerator2.0 插件安裝完畢.

現(xiàn)在可通過此工具自動完成test類的生成了,在需要進(jìn)行單元測試的類中Alt+Insert,

 


測試類中使用的相關(guān)注解跟代碼如下:

 


[java] view plain copy
package test.RXTemplateService; 
 
import RXTemplateService.YhService; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.test.annotation.Rollback; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.transaction.TransactionConfiguration; 
import org.springframework.transaction.annotation.Transactional; 
 
import javax.annotation.Resource; 
 
/**
 * YhService Tester.
 *
 * @author <Authors name>
 * @version 1.0
 * @since <pre>���� 19, 2013</pre>
 */ 
 
/*用于配置spring中測試的環(huán)境*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
/*
用來指定加載的Spring配置文件的位置,會加載默認(rèn)配置文件
@ContextConfiguration 注解有以下兩個(gè)常用的屬性:
locations:可以通過該屬性手工指定 Spring 配置文件所在的位置,可以指定一個(gè)或多個(gè) Spring 配置文件。
inheritLocations:是否要繼承父測試用例類中的 Spring 配置文件,默認(rèn)為 true。
*/ 
@ContextConfiguration(locations = "classpath:test/RXTemplateService/applicationContext.xml") 
/*
@TransactionConfiguration是配置事務(wù)情況的注解.
第一個(gè)參數(shù)transactionManager是你在applicationContext.xml或bean.xml中定義的事務(wù)管理器的bean的id;
第二個(gè)參數(shù)defaultRollback是表示測試完成后事務(wù)是否會滾 參數(shù)是布爾型的 默認(rèn)是true 但強(qiáng)烈建議寫上true
*/ 
@TransactionConfiguration(defaultRollback = true) 
@Transactional 
public class YhServiceTest { 
    @Resource 
    private YhService yhService; 
 
    @Before 
    public void before() throws Exception { 
    } 
 
    @After 
    public void after() throws Exception { 
    } 
 
    /**
     * Method: checkDlzhAndDlmm(String dlzh, String dlmm)
     */ 
    @Test 
    public void testCheckDlzhAndDlmm() throws Exception { 
        assert true : yhService.checkDlzhAndDlmm("wbb", "wbb"); 
    } 
 
    /**
     * Method: resetMm(String xmm, Integer id)
     */ 
    @Test 
    public void testResetMm() throws Exception { 
        yhService.resetMm("admin", 1); 
    } 
 
    /**
     * Method: yhSave(T_XT_YH yh)
     */ 
    @Test 
    @Rollback(false) 
    public void testYhSave() throws Exception { 
//TODO: Test goes here... 
    } 
 
    /**
     * Method: yhDelete(String ids)
     */ 
    @Test 
    public void testYhDelete() throws Exception { 
//TODO: Test goes here... 
    } 
 
    /**
     * Method: checkDlzh(String dlzh, Integer id)
     */ 
    @Test 
    public void testCheckDlzh() throws Exception { 
//TODO: Test goes here... 
    } 
 
    /**
     * Method: findYhById(Integer id)
     */ 
    @Test 
    public void testFindYhById() throws Exception { 
//TODO: Test goes here... 
    } 
 
    /**
     * Method: getYhList(int pageNo, int pageSize, Integer ssjgId)
     */ 
    @Test 
    public void testGetYhList() throws Exception { 
//TODO: Test goes here... 
    } 
}  

關(guān)鍵詞閱讀
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd