怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
来源:互联网 发布:药监局数据库 编辑:程序博客网 时间:2024/06/05 18:25
转载自:http://www.cnblogs.com/libingbin/p/6007203.html
因为项目的需要,所以研究了一下自动生成测试代码。将经验记录下来,总会有用的。我个人认为,好记性不如多做笔记多反思总结。
1. 前提条件
- 开发环境已正确配置
- 工程已解决JUnit依赖关系(pom.xml)
- 我用的是4.12版本:
1 <dependency>2 <groupId>junit</groupId>3 <artifactId>junit</artifactId>4 <version>4.12</version>5 </dependency>
2. IDEA中JUnit配置
打开Settings窗口搜索junit,如图(两个插件都勾选添加):
JUnitGenerator V2.0插件,可以帮助我们自动生成测试代码。如果搜索junit没有JUnitGenerator V2.0时,如下图操作(下载添加):
调用模板的方法(Alt+Insert)默认测试所有所有方法。若想要动态个性化生成,可以在所要测试的类页面上,使用该快捷操作Ctrl + Shift + T,如下图个性化设置:
现在可以通过右键菜单在这个类上运行'***测试类名**'来进行测试,或通过Run → Edit Configurations来进行。
3. JUnit常用断言及注解
JUnit为我们提供了一些辅助函数,他们用来帮助我们确定被测试的方法是否按照预期的效果正常工作,通常,把这些辅助函数称为断言。
断言核心方法
注解
一个测试类单元测试的执行顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:
@Before –> @Test –> @After
代码示例:
1 public class JunitFlowTest { 2 /* * 3 *1. @BeforeClass修饰的方法会在所有方法被调用前被执行, 4 * 而且该方法是静态的,所以当测试类被加载后接着就会运行它, 5 * 而且在内存中它只会存在一份实例,它比较适合加载配置文件。 6 * 2. @AfterClass所修饰的方法通常用来对资源的清理,如关闭数据库的连接 7 * 3. @Before和@After会在每个测试方法的前后各执行一次。 8 * */ 9 @BeforeClass 10 public static void setUpBeforeClass() throws Exception { 11 System.out.println("this is beforeClass..."); 12 } 13 @AfterClass 14 public static void tearDownAfterClass() throws Exception { 15 System.out.println("this is afterClass...");16 } 17 @Before 18 public void setUp() throws Exception { 19 System.out.println("this is before..."); 20 } 21 22 /**23 * 即使在@Before注解方法、@Test注解方法中抛出了异常,24 * 所有的@After注解方法依然会被执行25 */26 @After 27 public void tearDown() throws Exception { 28 System.out.println("this is after"); 29 } 30 31 /* * 32 * 33 * 1. Failure一般由单元测试使用的断言方法判断失败所引起的,34 * 这经表示测试点发现了问题 35 * ,就是说程序输出的结果和我们预期的不一样。 36 * 2. error是由代码异常引起的,它可以产生于测试代码本身的错误,37 * 也可以是被测试代码中的 38 * 一个隐藏的bug 39 * 3.测试用例不是用来证明你是对的,而是用来证明你没有错。 40 */ 41 @Test 42 public void testAdd() { 43 assertEquals(5, new Calculate().add(3,3)); 44 } 45 @Test 46 public void testDivide() { 47 assertEquals(3, new Calculate().divide(6, 0));48 }49 50 }
阅读全文
0 0
- 怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
- 在Idea中使用Junit4生成测试类
- 在IDEA中使用MyBatis Generator自动生成代码
- JUnit4及其在IDEA中的使用
- 【随记】在idea下使用jUnit4
- IntelliJ IDEA中如何使用JUnit4
- IntelliJ IDEA中如何使用JUnit4
- 在Maven2中加入Junit4测试
- idea中自动生成serialVersionUID
- Intellij IDEA中使用MyBatis-generator 自动生成MyBatis代码
- Intellij IDEA中使用MyBatis-generator 自动生成MyBatis代码
- Intellij IDEA中使用Mybatis-generator自动生成Mtbatis代码
- Intellij IDEA 中使用MyBatis-generator 自动生成MyBatis代码
- Intellij IDEA 中使用 MyBatis-generator 自动生成 MyBatis 代码
- Intellij IDEA 中使用 MyBatis-generator 自动生成 MyBatis 代码
- Intellij IDEA 中使用 MyBatis-generator 自动生成 MyBatis 代码
- Intellij IDEA中使用MyBatis-generator 自动生成MyBatis代码
- idea自动生成junit测试代码配置
- ubuntu 端口映射
- MyBatis框架
- TimeUnit用法,Date,MessageFormat 联合使用
- c++ stl sort
- BufferedWriter缓存流笔记与例题
- 怎样在IDEA中使用JUnit4和JUnitGenerator V2.0自动生成测试模块
- 单例模式在数据库中的应用
- hello java
- 【乱搞】51Nod 1622 集合对
- iOS开发-关于allocWithZone/copyWithZone/NSZone
- 如何实现一个可以用 await 异步等待的 Awaiter
- C++实现两个有序链表合并(17)---《那些奇怪的算法》
- poj 1753 枚举+dfs
- AWT&Swing 各种组件关系