编写JUnit单元测试的时候,会用到 setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()这四个方法,例如用 eclipse新建一个junit test case的时候,就会有如下图1的窗口让你去选择使用哪些方法(也可以不使用):

图1:选择使用哪些方法

上面这四个方法到底有什么用处,以及使用什么修饰符,看下面的这个例子就知道了:

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
 
public class UserEntityTest {
 
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("this is setUpBeforeClass...");
    }
 
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("this is tearDownAfterClass...");
    }
 
    @Before
    public void setUp() throws Exception {
        System.out.println("this is setUp...");
    }
 
    @After
    public void tearDown() throws Exception {
        System.out.println("this is tearDown...");
    }
 
    @Test
    public void testGetUserId() {
        System.out.println("this is testGetUserId...");
    }
 
    @Test
    public void testGetUserName() {
        System.out.println("this is testGetUserName...");
    }
 
}
 

上面这段代码的运行结果如下:

 
this is setUpBeforeClass...
this is setUp...
this is testGetUserName...
this is tearDown...
this is setUp...
this is testGetUserId...
this is tearDown...

this is tearDownAfterClass.

看代码,再看结果,可以很明显的发现:

(1) 使用@BeforeClass修饰的setUpBeforeClass()方法,在类中所有的方法执行之前执行;那么,使用@AfterClass修饰的 tearDownAfterClass()方法则与之完全相反;可以看到这两个方法都被static修饰,在类加载以后,这两个方法就会被加载,并且只会 存在一份。

(2)使用@Before修饰的setUp()方法,在每一个@Test测试方法执行之前执行;那么,使用@After修饰的tearDown()方法则与之完全相反。

如果测试的程序使用jdbc连接数据库,那么setUpBeforeClass()方法中就可以写上初始化数据库连接的一些代码,tearDownAfterClass()方法中就可以写上关闭数据库连接的一些代码

JUnit单元测试中的setUpBeforeClass()、tearDownAfterClass()、setUp()、tearDown()方法小结的更多相关文章

  1. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  2. server.port 在单元测试中,调用的类或者方法这个地方获取到的端口号就会变成-1

    @Value("${server.port}") 本文链接:https://blog.csdn.net/weixin_38342534/article/details/886985 ...

  3. SSM项目集成Lucene+IKAnalyzer在Junit单元测试中执行异常

    个人博客 地址:http://www.wenhaofan.com/article/20181108132519 问题描述 在项目运行以及main方法中能够正常运行,但是使用junit单元测试时却报如下 ...

  4. junit单元测试中私有方法测试

    1.单元测试可以对系统逻辑进行每个单元模块的测试. 2.单元测试也可以作为回归测试的依据,可以避免升级完善功能时引入问题. 3.单元测试要求将代码写的更清晰,更易于测试. 4.有时单元测试需要测试私有 ...

  5. Spring-mvc junit单元测试中 如何回滚?

    @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration(value = "src/main/webapp") @C ...

  6. Junit单元测试中优先使用AssertThat

    主要的优点: 1. 易读性 2. 错误信息更方便 推荐阅读:https://objectpartners.com/2013/09/18/the-benefits-of-using-assertthat ...

  7. JUnit单元测试实践:测试工具类和方法(EmptyUtils)

    以前的时候(读大学时),我认为写单元测试太费事了.现在,我改变看法了. 工作中,为了提高Web开发的质量和效率,近期又为了保证自己的工具类等一系列可复用组件的质量,我煞费苦心地开始认真学习和撰写单元测 ...

  8. 在单元测试中处理Debug.Assert的好方法

    在单元测试项目的配置文件中配置好, <?xml version="1.0" encoding="utf-8"?> <configuration ...

  9. C++ 中dynamic_cast&lt;&gt;的使用方法小结 -判断类型-rtti

    将一个基类对象指针(或引用)cast到继承类指针,dynamic_cast会根据基类指针是否真正指向继承类指针来做相应处理          即会作一定的判断.        对指针进行dynamic ...

随机推荐

  1. iOS crash 崩溃问题的追踪方法

    http://www.cnblogs.com/easonoutlook/archive/2012/12/27/2835884.html iOS crash 崩溃问题的追踪方法 在调试程序的时候,总是碰 ...

  2. AlarmManager定时闹钟

    一.AlarmManager介绍: AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来 ...

  3. 顺序栈操作--数据结构(C++)版

    最近学习数据结构,一开始接触感觉好难,颓废了一段时间,后来又重新翻开学习,突然感觉到很大的兴趣.对这些代码的运用都有了 一些新的认识.下面简单的讲述下最新学到的顺序栈,不知道大家学习的时候会不会有感觉 ...

  4. 网易 监控 openstack

    http://www.360doc.com/content/16/0416/08/13792507_551022987.shtml

  5. 解析Java的volatile关键字

    众所周知,无限制下多线程操作共享变量是危险的,为了保证线程安全语义,一般的建议是在操作共享变量时加锁,比方说在用synchronized关键字修饰的方法内读写共享变量. 但是synchronized开 ...

  6. (1)php开篇常识

    一.php PHP由zend公司开发维护,目前最高版本PHP7.1,官网地址http://php.net/ PHP从5.5开始不支持XP,没有PHP6这个版本 PHP是嵌入到HTML的脚本代码,格式: ...

  7. objective-c 强弱引用、properties的学习

    一.强弱引用 强引用:strong reference 弱引用:weak reference 引用可以理解为指针A指向的对象B.换句话说,拥有指针A的对象是对象B的所有者(ownership). 区别 ...

  8. Find the Duplicate Number -- LeetCode

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  9. zoj Burn the Linked Camp (查分约束)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  10. 三. Java类与对象8.再谈Java包

    在Java中,为了组织代码的方便,可以将功能相似的类放到一个文件夹内,这个文件夹,就叫做包. 包不但可以包含类,还可以包含接口和其他的包. 目录以"\"来表示层级关系,例如 E:\ ...