IDEA中利用JUnit进行单元测试

转载来源http://blog.csdn.net/w_basketboy24/article/details/10078589

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


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


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

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




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

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



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


  1. package test.RXTemplateService;  
  2.   
  3. import RXTemplateService.YhService;  
  4. import org.junit.After;  
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7. import org.junit.runner.RunWith;  
  8. import org.springframework.test.annotation.Rollback;  
  9. import org.springframework.test.context.ContextConfiguration;  
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  11. import org.springframework.test.context.transaction.TransactionConfiguration;  
  12. import org.springframework.transaction.annotation.Transactional;  
  13.   
  14. import javax.annotation.Resource;  
  15.   
  16. /** 
  17.  * YhService Tester. 
  18.  * 
  19.  * @author <Authors name> 
  20.  * @version 1.0 
  21.  * @since <pre>���� 19, 2013</pre> 
  22.  */  
  23.   
  24. /*用于配置spring中测试的环境*/  
  25. @RunWith(SpringJUnit4ClassRunner.class)  
  26. /* 
  27. 用来指定加载的Spring配置文件的位置,会加载默认配置文件 
  28. @ContextConfiguration 注解有以下两个常用的属性: 
  29. locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。 
  30. inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。 
  31. */  
  32. @ContextConfiguration(locations = "classpath:test/RXTemplateService/applicationContext.xml")  
  33. /* 
  34. @TransactionConfiguration是配置事务情况的注解. 
  35. 第一个参数transactionManager是你在applicationContext.xml或bean.xml中定义的事务管理器的bean的id; 
  36. 第二个参数defaultRollback是表示测试完成后事务是否会滚 参数是布尔型的 默认就是true 但强烈建议写上true 
  37. */  
  38. @TransactionConfiguration(defaultRollback = true)  
  39. @Transactional  
  40. public class YhServiceTest {  
  41.     @Resource  
  42.     private YhService yhService;  
  43.   
  44.     @Before  
  45.     public void before() throws Exception {  
  46.     }  
  47.   
  48.     @After  
  49.     public void after() throws Exception {  
  50.     }  
  51.   
  52.     /** 
  53.      * Method: checkDlzhAndDlmm(String dlzh, String dlmm) 
  54.      */  
  55.     @Test  
  56.     public void testCheckDlzhAndDlmm() throws Exception {  
  57.         assert true : yhService.checkDlzhAndDlmm("wbb""wbb");  
  58.     }  
  59.   
  60.     /** 
  61.      * Method: resetMm(String xmm, Integer id) 
  62.      */  
  63.     @Test  
  64.     public void testResetMm() throws Exception {  
  65.         yhService.resetMm("admin"1);  
  66.     }  
  67.   
  68.     /** 
  69.      * Method: yhSave(T_XT_YH yh) 
  70.      */  
  71.     @Test  
  72.     @Rollback(false)  
  73.     public void testYhSave() throws Exception {  
  74. //TODO: Test goes here...  
  75.     }  
  76.   
  77.     /** 
  78.      * Method: yhDelete(String ids) 
  79.      */  
  80.     @Test  
  81.     public void testYhDelete() throws Exception {  
  82. //TODO: Test goes here...  
  83.     }  
  84.   
  85.     /** 
  86.      * Method: checkDlzh(String dlzh, Integer id) 
  87.      */  
  88.     @Test  
  89.     public void testCheckDlzh() throws Exception {  
  90. //TODO: Test goes here...  
  91.     }  
  92.   
  93.     /** 
  94.      * Method: findYhById(Integer id) 
  95.      */  
  96.     @Test  
  97.     public void testFindYhById() throws Exception {  
  98. //TODO: Test goes here...  
  99.     }  
  100.   
  101.     /** 
  102.      * Method: getYhList(int pageNo, int pageSize, Integer ssjgId) 
  103.      */  
  104.     @Test  
  105.     public void testGetYhList() throws Exception {  
  106. //TODO: Test goes here...  
  107.     }  
  108. }   




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值