一个标准的Junit 4的运行流程,大致如下:
测试类实例化 -> @BeforeClass -> @Before -> @Test -> @After -> @AfterClass

下面的代码输出明确表明了其运行流程:

package com.junit.tutorial; 

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*; /**
* @author mkyong
*
*/
public class BasicAnnotation { private Collection collection; @BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
} @AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown");
} @Before
public void setUp() {
collection = new ArrayList();
System.out.println("@Before - setUp");
} @After
public void tearDown() {
collection.clear();
System.out.println("@After - tearDown");
} @Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
System.out.println("@Test - testEmptyCollection");
} @Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
System.out.println("@Test - testOneItemCollection");
}
}

结果:
@BeforeClass - oneTimeSetUp
@Before - setUp
@Test - testOneItemCollection
@After - tearDown
@Before - setUp
@Test - testEmptyCollection
@After - tearDown
@AfterClass - oneTimeTearDown

需要注意的是,我们需要申明 @BeforeClass 和 @AfterClass 为静态方法。

JUnit basic annotation的更多相关文章

  1. Java Basic - Annotation

    使用注解最主要的部分在于对注解的处理,那么就会涉及到注解处理器.      从原理上讲,注解处理器就是通过反射机制获取被检查方法上的注解信息,然后根据注解元素的值进行特定的处理.   注解处理器类库( ...

  2. JAXB - Annotations, The Annotation XmlElement

    The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...

  3. Spring框架第四篇之基于注解的DI注入

    一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...

  4. Maven学习笔记(三) :Maven使用入门

    编写POM:      Maven项目的核心是pom.xml.POM(Project Object Model,项目对象模型)定义了项目的基本信息,用于描写叙述项目怎样构建,声明项目依赖,等等.   ...

  5. Spring3+Hibernate4连接Oracle11g数据库参数配置

    应用场合:使用SSH框架开发一套应用系统,因为不同的SSH版本+系统架构会导致各种的错误,总结测试了下,成功测试得出本文配置 软件版本:Sping3+Hibernate4+Maven3 主要配置文件内 ...

  6. JUnit5 快速指南

    JUnit5 快速指南 version: junit5 1. 安装 2. JUnit 注解 3. 编写单元测试 3.1. 基本的单元测试类和方法 3.2. 定制测试类和方法的显示名称 3.3. 断言( ...

  7. hibernate 中文文档

    转载:http://blog.csdn.net/kevon_sun/article/details/42850387 Hibernate Annotations 参考文档 3.2.0 CR1 目录 前 ...

  8. Spring 3 Java Based Configuration with @Value

    Springsource has released the Javaconfig Framework as a core component of Spring 3.0. There is a tre ...

  9. Pyplot tutorial,Pyplot官方教程自翻译

      matplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB ...

随机推荐

  1. Android开发--二维码开发应用(转载!)

    android项目开发 二维码扫描   基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤   首先需要用到google提供的zxin ...

  2. VS2015 Cordova Ionic移动开发(二)

    一.创建空白Cordova应用 打开VS,选择[新建项目],选择其它语言JavaScript或者TypeScript,语言的话就按个人喜好,喜欢JS就用JS,喜欢TS就用TS,推荐使用TS书写,代码结 ...

  3. jQuery AJAX实现调用页面后台方法

    1.新建demo.aspx页面.2.首先在该页面的后台文件demos.aspx.cs中添加引用. using System.Web.Services; 3.无参数的方法调用. 大家注意了,这个版本不能 ...

  4. 在C语言中使用scanf语句时遇到的问题总结

    在使用visual studio2013编写c语言代码时,遇到了这样的几个小问题,进行如下的总结. 1, 关于使用scanf语句报错的解决方案1 #include <stdio.h> in ...

  5. iOS中webView加载URL需要处理特殊字符

    今天在项目中遇到webView加载URL时,因为URL中有特殊字符,导致页面无法加载,而且在- (BOOL)webView:(UIWebView )webView shouldStartLoadWit ...

  6. asp.net中后台javaScrip的使用

    ClientScriptManager csm = Page.ClientScript;        //Script标记靠近<form>标签        //csm.Register ...

  7. actionscript sendToURL请求url,传递http_referer分浏览器统计

    IE全版本都不传递referer,但会在header中传递X_FLASH_VERSION,例如:"HTTP_X_FLASH_VERSION":"13,0,0,182&qu ...

  8. AspNet WebApi : MessageHandler(消息处理器 )

    1. Http Message Handler WebApi中的MessageHandler类似MVC中的filter,可用于请求/响应到达真正目标前对请求或者响应进行修改,比如:用户身份验证,请求头 ...

  9. YII 集成jquery

  10. python学习第五天 List和tuple类型介绍及其List切片

    List 和tuple: python提供一种类似C语言数组的类型,但是使用起来确是相当的简洁.那就讲讲这神奇的python中list 和tuple吧. List类型: 1.直接贴代码: L = [' ...