您的位置:首页 > 其它

JUnit中的注解(Annotation)

2017-12-17 15:14 330 查看
新建待测试类:

package com.sunft.junit4;

/**
* 待测试的类
* @author sunft
*
*/
public class T {

public int add(int x, int y) {
return x + y;
}

public int divide(int x, int y) {
return x / y;
}

public static void main(String[] args) {
int z = new T().add(3, 5);
System.out.println(z);
}
}

1、@Test: 测试方法

      a)(expected=XXException.class)
/**
* 运行下面的方法,抛出指定的异常,运行会通过,
* 因为这里期望抛异常
*/
@Test(expected=java.lang.ArithmeticException.class)
public void testDivide() {
int z = new T().divide(8, 0);
}
b)(timeout=xxx)
/**
* 运行下面的方法,抛出指定的异常,运行会通过,
* 因为这里期望抛异常,期望该方法在100毫秒内结束
*/
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z = new T().divide(8, 0);
}

2、@Ignore: 被忽略的测试方法
package com.sunft.junit4.test;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import org.junit.Ignore;
import org.junit.Test;

import com.sunft.junit4.T;

public class TTest {

@Test
public void testAdd() {
int z = new T().add(5, 3);
//这里使用的是hamcrest的匹配模式,is等同于Matcers.is
assertThat(z, is(8));
//演示allOf的使用
assertThat(z, allOf(greaterThan(5), lessThan(10)));
}

/**
* 运行下面的方法,抛出指定的异常,运行会通过,
* 因为这里期望抛异常,期望该方法在100毫秒内结束
* @Ignore用于忽略当前测试
*/
@Ignore
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z = new T().divide(8, 0);
}

}


3、@Before和@After注解
@Before: 每一个测试方法之前运行
@After: 每一个测试方法之后运行

演示@Before和@After注解的使用:
package com.sunft.junit4.test;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.sunft.junit4.T;

public class TTest {

@Before
public void before() {
System.out.println("before method");
}

@Test
public void testAdd() {
int z = new T().add(5, 3);
//这里使用的是hamcrest的匹配模式,is等同于Matcers.is
assertThat(z, is(8));
//演示allOf的使用
assertThat(z, allOf(greaterThan(5), lessThan(10)));
System.out.println("testAdd method");
}

/**
* 运行下面的方法,抛出指定的异常,运行会通过,
* 因为这里期望抛异常,期望该方法在100毫秒内结束
*/
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
System.out.println("testDivide method");
int z = new T().divide(8, 0);
}

@After
public void after() {
System.out.println("after() method");
}

}


运行所有方法,得到下面的结果:
before method
testAdd method
after() method
before method
testDivide method
after() method


可以看出,添加@Before注解的方法会在所有被测试的方法之前执行,添加了@After注解的方法会在所有被测试的方法之后执行。

4、@BeforeClass和@AfterClass
@BeforeClass: 所有测试开始之前运行
@AfterClass: 所有测试结束之后运行

package com.sunft.junit4.test;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sunft.junit4.T;

public class TTest {

/**
* 该方法只能是静态方法
*/
@BeforeClass
public static void beforeClass() {
System.out.println("beforeClass method");
}

@Before
public void before() {
System.out.println("before method");
}

@Test
public void testAdd() {
int z = new T().add(5, 3);
//这里使用的是hamcrest的匹配模式,is等同于Matcers.is
assertThat(z, is(8));
//演示allOf的使用
assertThat(z, allOf(greaterThan(5), lessThan(10)));
System.out.println("testAdd method");
}

/**
* 运行下面的方法,抛出指定的异常,运行会通过,
* 因为这里期望抛异常,期望该方法在100毫秒内结束
*/
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
System.out.println("testDivide method");
int z = new T().divide(8, 0);
}

@After
public void after() {
System.out.println("after() method");
}

/**
* 该方法只能是静态方法
*/
@AfterClass
public static void afterClass() {
System.out.println("afterClass method");
}

}


控制台打印的结果:

beforeClass method
before method
testAdd method
after() method
before method
testDivide method
after() method
afterClass method


由此可以得出结论,添加@BeforeClass注解的方法会在所有的方法执行之前执行,添加@AfterClass注解的方法会在所有的方法执行之后执行。注意添加@BeforeClass和@AfterClass注解的方法必须是静态的,否则会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息