本文主要是介绍junit5 实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
网上有若干的junit5的教程, 可惜好多的跑不起来, 所以决定自己写一个, 作为junit4的升级版本, 还是有很多的长进的.
项目的junit5的依赖是:
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><junit.version>4.12</junit.version><junit.jupiter.version>5.5.2</junit.jupiter.version><junit.vintage.version>5.5.2</junit.vintage.version></properties><dependencies><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>${junit.jupiter.version}</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>${junit.jupiter.version}</version><scope>test</scope></dependency><dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><version>${junit.vintage.version}</version><scope>test</scope></dependency></dependencies>
- @BeforeAll 只执行一次,执行时机是在所有测试和 @BeforeEach 注解方法之前。
- @BeforeEach 在每个测试执行之前执行。
- @AfterEach 在每个测试执行之后执行。
- @AfterAll只执行一次,执行时机是在所有测试和 @AfterEach 注解方法之后。
因为框架会为每个测试创建一个单独的实例,在 @BeforeAll/@AfterAll 方法执行时尚无任何测试实例诞生。因此,这两个方法必须定义为静态方法。
测试方法:
package com.example.project;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;/*** @author zk* @Description:* @date 2019-10-25 14:16*/
@DisplayName("第一个测试类") //展示的别名
public class MyFirstTest {/*** 每个单元测试前执行一次*/@AfterEachvoid afterEach(){System.out.println("afterEach");}@BeforeEachvoid beforeEach(){System.out.println("beforeEach");}/*** BeforeAll 必须是static 的 一个类的所有单元测试开始前只执行一次*/@BeforeAllstatic void beforeAll(){System.out.println("beforeAll");}/*** AfterAll 必须是static 的 一个类的所有单元测试后只执行一次*/@AfterAllstatic void afterAll(){System.out.println("afterall");}@DisplayName("第一个测试方法")@Testvoid test1(){System.out.println("test1");Assertions.assertEquals(2,1+1);}@DisplayName("第二个测试方法")@Testvoid test2(){System.out.println("test2");Assertions.assertNull(null);}}
第二个测试:
package com.example.project;import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;import java.time.Duration;
import java.util.concurrent.TimeUnit;import static org.junit.jupiter.api.Assertions.*;/*** @author zk* @Description:* @date 2019-10-25 14:25*/
@DisplayName("第二个测试类")
public class MySecondTest {@Testvoid test1(){assertTrue(2 == 2, () -> "Assertion messages can be lazily evaluated -- "+ "to avoid constructing complex messages unnecessarily.");}/*** 多个测试*/@Testvoid test2(){Assertions.assertAll("all",()->Assertions.assertEquals(2,1+1),()->Assertions.assertNotEquals(2,1+3));}/*** 超时的测试*/@Testvoid timeoutNotExceeded() {Assertions.assertTimeout(Duration.ofSeconds(3), () -> {// Perform task that takes less than 3sThread.sleep(3500);});}/*** 测试方法提供参数* @param argument*/@ParameterizedTest@ValueSource(ints = { 1, 2, 3 })void testWithValueSource(int argument) {assertTrue(argument > 0 && argument < 4);}@ParameterizedTest@EnumSource(TimeUnit.class)void testWithEnumSource(TimeUnit timeUnit) {assertNotNull(timeUnit);}/*** 这里测试报错... https://blog.csdn.net/baidu_27222643/article/details/75020104* @param fruit* @param rank*/@ParameterizedTest@CsvSource({"apple, 1","banana, 2","'lemon, lime', 0xF1"})void testWithCsvSource(String fruit, int rank) {assertNotNull(fruit);assertNotEquals(0, rank);}}
好了, junit 的测试就说到这里, 相比于junit4, 功能增加了好多.
这篇关于junit5 实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!