一个标准的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. The performance between the 'normal' operation and the 'shift' operation.

    First, I gonna post my test result with some code: //test the peformance of the <normal operation ...

  2. oracle-同义词,又学到东西了

    select  col1  from tab1@db_link1; create or replace synonym test123 as se,ect col1 from tab1@db_link ...

  3. 谈谈oracle中的临时表

    --------------------创建临时表 临时保存从xml字符串解析来的数据--------------------------- 会话级别临时表SQL> create global ...

  4. iOS9适配中出现的一些常见问题

    本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...

  5. android查看真机中的数据库

    0.在有网的前提下1.安装 Android Studio,Lantern,Chrome浏览器2.在在githab上搜索stetho,打开第一个facebook/stetho3.在Gradle Scri ...

  6. (java)从零开始之--异常处理(以文件拷贝为例)

    开发过程中避免不了对异常的处理,但是异常的处理又不能乱throw 下面是简单的抛异常处理 public static void CopyFile(String souFile,String dirFi ...

  7. latex引用多篇参考文献

    1.如何使连续的参考文献能够中间用破折号连起来?比如[6,7,8,9]变成[6-9]? 方法:在文档开始前加上下面的语句命令 \usepackage[numbers,sort&compress ...

  8. 【POJ1417】【带标记并查集+DP】True Liars

    Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was ...

  9. Android三种菜单简介

    Android的菜单分为三种类型:选项菜单(Option Menu).上下文菜单(Context Menu).子菜单(Sub Menu). 一.选项菜单 用户点击设备上的菜单按钮(Menu),触发事件 ...

  10. 性能测试实践-linux

    需求:线上系统性能优化,查找服务器和线上系统瓶颈 根据线上经验数据及期望值定量 数据   up down 线上数据 50 500 测试数据 100 500~2000+ 测试数据 200 500~200 ...