@After

If you allocate external resources in a Before method you need to release them after the test runs.Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or
Test method throws an exception. The @After methods declared in superclasses will be run after those of the current class.

如果在@Before注解方法中分配了额外的资源,那么在测试执行完后,需要释放分配的资源。

使用@After注解一个public void方法会使该方法在@Test注解方法执行后被执行

即使在@Before注解方法、@Test注解方法中抛出了异常,所有的@After注解方法依然会被执行,见示例

父类中的@After注解方法会在子类@After注解方法执行后被执行

public class MathTest {
@Before
public void setUp() throws Exception {
throw new Exception();
} @Test
public void testAdd() {
Math m = new Math();
assertTrue(m.add(1, 1) == 2);
} @After
public void tearDown() throws Exception {
System.out.println("after");
}
}
after

@AfterClass

If you allocate expensive external resources in a Before Class method you need to release them after all the tests in the class have run. Annotating a public static void method with @AfterClass causes that method to be run after all the tests in the class
have been run. All @AfterClass methods are guaranteed to run even if a Before Class method throws an exception.The @AfterClass methods declared in superclasses will be run after those of thecurrent class.

如果在@BeforeClass注解方法中分配了代价高昂的额外的资源,那么在测试类中的所有测试方法执行完后,需要释放分配的资源。

使用@AfterClass注解一个public static void方法会使该方法在测试类中的所有测试方法执行完后被执行

即使在@BeforeClass注解方法中抛出了异常,所有的@AfterClass注解方法依然会被执行

父类中的@AfterClass注解方法会在子类@AfterClass注解方法执行后被执行

@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. No other ordering is defined.

当编写测试方法时,经常会发现一些方法在执行前需要创建相同的对象

使用@Before注解一个public void 方法会使该方法在@Test注解方法被执行前执行(那么就可以在该方法中创建相同的对象)

父类的@Before注解方法会在子类的@Before注解方法执行前执行

@BeforeClass

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization.Annotating a public static void no-arg method with @BeforeClass
causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

有些时候,一些测试需要共享代价高昂的步骤(如数据库登录),这会破坏测试独立性,通常是需要优化的

使用@BeforeClass注解一个public static void 方法,并且该方法不带任何参数,会使该方法在所有测试方法被执行前执行一次,并且只执行一次

父类的@BeforeClass注解方法会在子类的@BeforeClass注解方法执行前执行

@Ignore

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing
tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.

对包含测试类的类或@Test注解方法使用@Ignore注解将使被注解的类或方法不会被当做测试执行

JUnit执行结果中会报告被忽略的测试数

public class MathTest {
@Ignore("do not test")
@Test
public void testAdd() {
Math m = new Math();
assertTrue(m.add(1, 1) == 2);
}
}
@Ignore
public class MathTest {
@Test
public void testAdd() {
Math m = new Math();
assertTrue(m.add(1, 1) == 2);
}
}

执行结果相同:

@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.

The Test annotation supports two optional parameters.

The first, expected,declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails.

The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).

@Test注解的public void方法将会被当做测试用例

JUnit每次都会创建一个新的测试实例,然后调用@Test注解方法

任何异常的抛出都会认为测试失败

@Test注解提供2个参数:

1,“expected”,定义测试方法应该抛出的异常,如果测试方法没有抛出异常或者抛出了一个不同的异常,测试失败

2,“timeout”,如果测试运行时间长于该定义时间,测试失败(单位为毫秒)

public class MathTest {
@Test(expected=Exception.class)
public void testAdd() throws Exception{
throw new Exception();
}
}

public class MathTest {
@Test(timeout=5000)
public void testAdd() {
for(;;){ }
}
}

JUnit4注解基本介绍的更多相关文章

  1. Java基础笔记 – Annotation注解的介绍和使用 自定义注解

    Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 |  被围观 25,969 views+ 1.Anno ...

  2. 第四节 数据格式化和ModelAttribute注解的介绍

    从来都不坦荡,情绪都写在脸上:不开心的时候,不爱说话,笑也勉强. 课堂笔记,如果这么写,不仅仅是手速,还要有语速, 这样不太适合! --胖先生 关于数据传递: 客户端传递数据到服务端: 1.使用普通的 ...

  3. MyBatis注解Annotation介绍及Demo

     MyBatis注解Annotation介绍及Demo 2014-04-21 17:09:55 标签:Mybatis Annotation 注解 ResultMap SqlBuilder 原创作品,允 ...

  4. 数据格式化和ModelAttribute注解的介绍

    关于数据传递: 客户端传递数据到服务端: 1.使用普通的形式 A.传递简单的数据 如果是说你传递的数据的名称跟控制层中的形参的名称不一致的情况下需要使用 注解: @RequestParam()如果存在 ...

  5. 功能:Java注解的介绍和反射使用

    功能:Java注解的介绍和反射使用 一.注解 1.注解介绍 java注解(Annotation),又称为java标注,是jdk5.0引入的一种机制. Java 语言中的类.方法.变量.参数和包等都可以 ...

  6. JUnit4注解

    今天学习了下,mybatis中开发dao的方法,用到了JUnit4进行单元测试, 将JUnit4中的注解总结了下,供大家参考学习: JUnit 4 开始使用 Java 5 中的注解(annotatio ...

  7. Android注解使用之ButterKnife 8.0注解使用介绍

    前言: App项目开发大部分时候还是以UI页面为主,这时我们需要调用大量的findViewById以及setOnClickListener等代码,控件的少的时候我们还能接受,控件多起来有时候就会有一种 ...

  8. Annotation(一)——注解开发介绍

    <p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...

  9. Android学习笔记- ButterKnife 8.0注解使用介绍

    前言: App项目开发大部分时候还是以UI页面为主,这时我们需要调用大量的findViewById以及setOnClickListener等代码,控件的少的时候我们还能接受,控件多起来有时候就会有一种 ...

随机推荐

  1. PHP静态化之真静态化

    参考文献:http://blog.sina.com.cn/s/blog_66aa1142010114lc.html 采用了动态服务器技术生成静态HTML的做法,这样做的好处是:一是能减轻其服务器的负担 ...

  2. SharePoint 2010 部署 WSP 包

    转:http://www.cnblogs.com/myheaven/archive/2011/05/19/2051180.html .net在工程的下面生成了WSP包,在Debug下面.需要放到生产环 ...

  3. zookeeper实现分布锁

    分布式锁服务在大家的项目中或许用的不多,因为大家都把排他放在数据库那一层来挡.当大量的行锁.表锁.事务充斥着数据库的时候.一般web应用很多的瓶颈都在数据库上,这里给大家介绍的是减轻数据库锁负担的一种 ...

  4. [POJ3684]Physics Experiment

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1363   Accepted: 476   Special Judge ...

  5. Java JDK8 安装及环境变量配置

    步骤: 1.下载JDK 2.安装 3.配置环境变量 注意: 1.在选择安装路径时,不要选择C盘或D盘下的[Program Files]目录.此目录名中有空格,会导致配置不成功. 参照链接: http: ...

  6. maven,spring,mybatis集成错误

    maven,spring,mybatis集成的时候单元测试junit测试没问题,但mvn jetty:run 就报错误 错误: org.apache.ibatis.binding.BindingExc ...

  7. java 小结2 多态问题和容器介绍

    面向对象这个东西,其实我们一直是不是都没有感觉到自己在用,以后我一定要用用.以前学c#时候认真的看过一次,最近一直研究java.随便再看看. 多态问题: 在java中多态分为(1)编译时多态和(2)运 ...

  8. 输入n个数组,数组长度不等,每个数组取出一个数进行组合,求出所有的组合。

    转载声明:原文转自http://www.cnblogs.com/xiezie/p/5511707.html 昨天晚上,有个朋友找到我,他在用matlab编程,但是遇到一个问题,解决不了. 问题如下: ...

  9. 利用系统镜像文件安装.Net框架的方式

    最近重装系统之后,在安装部分程序时需要.NET3.5框架,在线安装时间较长,网上搜到了一个很好的解决办法.利用windows系统镜像.首先将镜像加载到驱动中比如L,然后在cmd中输入 dism.exe ...

  10. hdoj 5522 Numbers

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5522 水题:暴力过 #include<stdio.h> #include<strin ...