JUnit--BeforeClass、AfterClass、Before、After示例

本文介绍了JUnit测试框架中关键注解的使用方法,包括@BeforeClass、@AfterClass、@Before、@After及@Test,并通过示例代码展示了它们的执行顺序。

最近老大提了个想法,以后每半个月他会出个题目,这些题目是让我们了解掌握一些市面好比较好用的类包和工具。

嗯,这样方式挺好,总比自己无头苍蝇的学习。所以以后除了分享前端的学习,也会时不时的分享下学习这些类包和工具的心得。

这周老大出了题目是研究EasyMcok.

EasyMcok具体的介绍下次再说。这次先介绍下学习这个工具时掌握的JUnit的心得

之前测试基本我只用JUnit的@Test,但其实它还有@BeforeClass、@AfterClass、@Before、@After

而他们的区别就是加载调用的顺序不同。

举个例子

public class TestIt {  

private TestJUnit tju = null;



@BeforeClass

public static void enter() {

System.out.println("进来了!");

}



@Before

public void init() {

System.out.println("正在初始化。。");

tju = new TestJUnit();

System.out.println("初始化完毕!");

}



@Test

public void testit() {

tju.run();

}



@After

public void destroy() {

System.out.println("销毁对象。。。");

tju = null;

System.out.println("销毁完毕!");

}



@AfterClass

public static void leave() {

System.out.println("离开了!");

}

}

执行结果是

进来了!
正在初始化。。
初始化完毕!
Hello,JUnit销毁对象。。。
销毁完毕!
离开了!

 

转载于:https://www.cnblogs.com/yuxiaorong/archive/2011/11/22/2258783.html

<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-params</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-commons</artifactId> <version>1.6.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-engine --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-engine</artifactId> <version>1.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> </dependencies>这是我的maven依赖,怎么将junit4 变成junit5
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值