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注解的方法会在所有的方法执行之前执行,添加@AfterClass注解的方法会在所有的方法执行之后执行。注意添加@BeforeClass和@AfterClass注解的方法必须是静态的,否则会报错。
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注解的方法必须是静态的,否则会报错。
相关文章推荐
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit 4 使用 Java 5 中的注解(annotation) 【转】
- JUnit 4 使用 Java 5 中的注解(annotation)
- Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)
- Junit的annotation(注释、注解)
- JUnit扩展:引入新注解Annotation
- java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
- JUnit软件测试技术(工具)和注解( Annotation )
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit 4 使用 Java 5 中的注解(annotation)
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)基本概念
- Java单元测试工具:JUnit4——JUnit详解之运行流程及常用注解
- 自己动手实现Java注解(Java Annotation in Action)
- 深入理解Java:注解(Annotation)自定义注解入门
- 使用Spring的声明式事务----Annotation注解方式
- 一张图看懂Java注解(Annotation)
- Hibernate Annotation mappedBy注解理解
- Java注解(一)Annotation的概述和语