Juint 4学习笔记1--基本配置

junit 4中采用Annotation方式引入测试。通过对需要测试的方法添加@TEST注释。
  1. //一个简单的例子
  2.  public class Example {
  3.     @Test 
  4.     public void method() {
  5.        org.junit.Assert.assertTrue( new ArrayList().isEmpty() );
  6.     }
  7.  }

@before

@after

@before class

@after class

以上注释实现方式类似。

org.junit
Annotation Type Before 注释类型 Before


@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface Before
 

When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class.

当编写测试的时候,会发现有时候很多方法需要相同的前置条件,在一个PUBLIC VOID 的方法前面加@Before,会使所有的测试方法运行之前,先运行@Before注释的方法。

Here is a simple example:

以下是一个简单的示例
 public class Example {
    List empty;
    @Before public void initialize() {
       empty= new ArrayList();
    }
    @Test public void size() {
       ...
    }
    @Test public void remove() {
       ...
    }
 }
@after跟@Before相对,在每次测试方法以后运行。
@before class是在一个测试类之前,意思也就是一个测试类前只运行一次
@after class与上面的方法相对。

Assert

断言,assertTrue方法,可以通过静态方法引入方式引入。

import static org.junit.Assert.assertEquals;

 

@Test可以有两个参数,第一个参数expected.后面跟期望抛出的对象类型.

 

    @Test(expected=IndexOutOfBoundsException.class)

public void outOfBounds() {
       new ArrayList<Object>().get(1);
    }
简单的性能测试,如 @Test(timeout=100)

    @Test(timeout=100) public void infinity() {
       while(true);
    }
 

感觉JUNIT4这个工具还是挺简单的。但是测试一点都不简单。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值