junit 4快速入门

JUnit 4全面引入了Annotation来执行我们编写的测试。

  关于JUnit 3的使用可以参见:http://www.cnblogs.com/mengdd/archive/2013/03/26/2983565.html

 

  使用JUnit 4进行测试:

  1.新建项目,引用JUnit 4类库。

  2.新建立source folder,存放测试类代码,但是注意测试类代码和被测试代码的包名一致。

  3.编写被测试类代码。

  前面的都是和JUnit 3类似的,下面就开始不同了:

  4.编写测试类代码:

  JUnit 4并不要求测试类继承TestCase父类。

  测试方法上面需要加上一个Annotation:@Test

  在一个测试类中,所有被@Test注解所修饰的public,void方法都是test case,可以被JUnit所执行。

 

Test文档:

org.junit

Annotation Type Test

--------------------------------------------------------------------------------

@Retention(value=RUNTIME)

@Target(value=METHOD)

public @interface Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method.

Any exceptions thrown by the test will be reported by JUnit as a failure.

If no exceptions are thrown, the test is assumed to have succeeded.

 

最佳实践

  JUnit 4不再要求方法的名字,可以给测试方法任意命名,但是为了可读性,还是建议以test开头命名,如JUnit 3要求的那样。

 

如何实现JUnit 3中的setUp()和tearDown()

  比如测试前面用过的计算的类:

 

复制代码
package com.mengdd.junit4;

public class Calculator
{
    public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) throws Exception { if (0 == b) { throw new Exception("除数不能为0"); } return a / b; } }
复制代码

 

 

  现在要测试这个类中的前三个方法,写出测试类如下:

复制代码
package com.mengdd.junit4;

import org.junit.Test;
import static org.junit.Assert.assertEquals;//静态导入 public class CalculatorTest { @Test public void testAdd() { Calculator cal = new Calculator(); int result = cal.add(3, 5); assertEquals(8, result); } @Test public void testSubstract() { Calculator cal = new Calculator(); int result = cal.subtract(1, 6); assertEquals(-5, result); } @Test public void testMultiply() { Calculator cal = new Calculator(); int result = cal.multiply(5, 9); assertEquals(45, result); } }
复制代码

 

  这样是可以完成测试功能,如何避免重复呢?

  就好像JUnit 3的时候把创建对象的代码放在setUp()中那样。

 

  JUnit 4中还是依靠注解来解决:@Before

  被这个注解标注的方法会在每个测试用例之前被调用。

  相应的,对应于tearDown()方法的是用@After实现的。

   这两个方法会在每一个测试用例的前后被调用,所以可以用来创建对象,对于每个测试用例来说都是一个新的对象。

 

全局的初始化和销毁:@BeforeClass和@AfterClass

  @BeforeClass:在所有的方法调用之前调用的一个方法;用来进行一些开销昂贵的初始化操作,比如连接数据库。

  @AfterClass:在所有的方法调用之后调用的方法。

  注意这两个修饰符修饰的方法必须是public static void的,并且没有参数(no-arg)。

 

  Note:

  即便@BeforeClass修饰的方法抛出异常了,@AfterClass修饰的方法也会照常执行。

  基类的@BeforeClass方法会在子类的@BeforeClass方法之前进行;而基类的@AfterClass方法会在子类的@AfterClass方法之后进行。

 

  修改后的代码如下:

复制代码
package com.mengdd.junit4;

import org.junit.After;
import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.assertEquals;//静态导入 public class CalculatorTest { private Calculator cal = null; @BeforeClass public static void globalInit() { System.out.println("global Init invoked!"); } @AfterClass public static void globalDestroy() { System.out.println("global Destroy invoked!"); } @Before public void init()//setUp()  { cal = new Calculator(); System.out.println("init --> cal: " + cal); } @After public void destroy()//tearDown()  { System.out.println("destroy"); } @Test public void testAdd() { System.out.println("testAdd"); int result = cal.add(3, 5); assertEquals(8, result); } @Test public void testSubtract() { System.out.println("testSubtract"); int result = cal.subtract(1, 6); assertEquals(-5, result); } @Test public void testMultiply() { System.out.println("testMultiply"); int result = cal.multiply(5, 9); assertEquals(45, result); } }
复制代码

 

 

@Ignore

  @Ignore注解可用于修饰测试类与测试方法,被@Ignore所修饰的方法或类中的测试(@Test修饰的测试)将不会被执行。

  @Ignore中可以加上参数值,说明忽略测试的原因。

转载于:https://www.cnblogs.com/com-wushuang/p/5359521.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值