IDEA中利用JUnit進行單元測試

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

 


 

在文本框中輸入Plugin進行插件搜索設置。spring


 

點擊按鈕,從插件資源庫中安裝新的插件。app

 

 

從插件資源庫中搜索JunitGenerator V2.0版本,在插件位置,鼠標右擊工具




 

選擇Download and Install ,在彈出的對話框中選擇yes按鈕,點擊OK以後在須要重啓下工具,選擇Restart按鈕,到此JunitGenerator2.0 插件安裝完畢.單元測試

如今可經過此工具自動完成test類的生成了,在須要進行單元測試的類中Alt+Insert,測試


測試類中使用的相關注解跟代碼以下:spa


 

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中測試的環境*/
@RunWith(SpringJUnit4ClassRunner.class)
/*
用來指定加載的Spring配置文件的位置,會加載默認配置文件
@ContextConfiguration 註解有如下兩個經常使用的屬性:
locations:能夠經過該屬性手工指定 Spring 配置文件所在的位置,能夠指定一個或多個 Spring 配置文件。
inheritLocations:是否要繼承父測試用例類中的 Spring 配置文件,默認爲 true。
*/
@ContextConfiguration(locations = "classpath:test/RXTemplateService/applicationContext.xml")
/*
@TransactionConfiguration是配置事務狀況的註解.
第一個參數transactionManager是你在applicationContext.xml或bean.xml中定義的事務管理器的bean的id;
第二個參數defaultRollback是表示測試完成後事務是否會滾 參數是布爾型的 默認就是true 但強烈建議寫上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...
    }
} 
相關文章
相關標籤/搜索