IDEA中利用JUnit进行单元测试

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


在文本框中输入Plugin进行插件搜索设置。


点击按钮,从插件资源库中安装新的插件。

从插件资源库中搜索JunitGenerator V2.0版本,在插件位置,鼠标右击




选择Download and Install ,在弹出的对话框中选择yes按钮,点击OK之后在需要重启下工具,选择Restart按钮,到此JunitGenerator2.0 插件安装完毕.

现在可通过此工具自动完成test类的生成了,在需要进行单元测试的类中Alt+Insert,


测试类中使用的相关注解跟代码如下:


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...
    }
} 






原文地址:https://www.cnblogs.com/riskyer/p/3268829.html