[英]Junit5 Parameterized Test LifeCycle. why @BeforeAll and static block run after all the tests
I am posting a simple code below, why does @BeforeAll annotated method and static block run after the Parameterized tests?我在下面发布了一个简单的代码,为什么 @BeforeAll 注释方法和静态块在参数化测试之后运行? How do I register a common object or Database connection in this case before loading the Parameterized test in Junit5 or leverage the functionality of @BeforeAll or static block.
在这种情况下,如何在 Junit5 中加载参数化测试或利用 @BeforeAll 或静态块的功能之前注册公共对象或数据库连接。 (FYI: The equivalent code for Parameterized test in Junit4 runs static block before all the tests. But not the @BeforeClass annotated method.)
(仅供参考:Junit4 中参数化测试的等效代码在所有测试之前运行静态块。但不是 @BeforeClass 注释方法。)
package com.something;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Example {
MathUtils utils;
static{
System.out.println("Static Block------");
}
@BeforeAll
static void beforeAllInit(){
System.out.println("This will run Before all tests");
}
@BeforeEach //fresh instance before each test method
void Init(){
utils = new MathUtils();
System.out.println("utils init()");
}
@AfterEach
void cleanup(){
System.out.println("Cleaning Up...");
}
//@Test
@ParameterizedTest
@CsvSource(value={"1,2,3","10,10,20","5,9,14"})
void testAdd(int num1, int num2, int exp){
//MathUtils utils = new MathUtils();
//int exp = 3;
int actual = utils.add(num1,num2);
assertEquals(exp,actual,"Adding two numbers");
}
@ParameterizedTest
@MethodSource("createDataCollection")
void testMethod(ReadJson rj) {
assertNotNull(rj);
}
public Stream<ReadJson> createDataCollection() {
//beforeAllInit();
final List<ReadJson> testInputs = new ArrayList<>();
testInputs.add(new ReadJson("one","something","Miami","ABC"));
testInputs.add(new ReadJson("two","something","New York","ABC"));
testInputs.add(new ReadJson("three","something","Redlands","ABC"));
return testInputs.stream();
}
}
utils init()实用程序初始化()
Cleaning Up...打扫干净...
utils init()实用程序初始化()
Cleaning Up...打扫干净...
utils init()实用程序初始化()
Cleaning Up...打扫干净...
Static Block------静态块-----
This will run Before all tests这将在所有测试之前运行
Process finished with exit code 0进程以退出代码 0 结束
Cannot reproduce.无法重现。 The following test file:
以下测试文件:
package com.example;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JUnitTests {
static {
System.out.println("Static initializer...");
}
@BeforeAll
static void beforeAll() {
System.out.println("Before all...");
}
@BeforeEach
void beforeEach() {
System.out.println("Before each...");
}
@ParameterizedTest
@CsvSource(value = {"5,5,10", "2,3,5"})
void parameterizedTest(int x, int y, int r) {
System.out.printf("Parameterized test (%d, %d, %d)...%n", x, y, r);
assertEquals(r, x + y, () -> String.format("%d + %d expected to equal %d", x, y, r));
}
@AfterEach
void afterEach() {
System.out.println("After each...");
}
@AfterAll
static void afterAll() {
System.out.println("After all...");
}
}
Compiled and executed with the following Gradle build script:使用以下 Gradle 构建脚本编译并执行:
plugins {
java
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
}
tasks.test {
useJUnitPlatform()
testLogging {
showStandardStreams = true
}
}
Gives the following output:给出以下输出:
> Task :test
JUnitTests STANDARD_OUT
Static initializer...
Before all...
JUnitTests > parameterizedTest(int, int, int) > com.example.JUnitTests.parameterizedTest(int, int, int)[1] STANDARD_OUT
Before each...
Parameterized test (5, 5, 10)...
After each...
JUnitTests > parameterizedTest(int, int, int) > com.example.JUnitTests.parameterizedTest(int, int, int)[2] STANDARD_OUT
Before each...
Parameterized test (2, 3, 5)...
After each...
JUnitTests STANDARD_OUT
After all...
BUILD SUCCESSFUL in 3s
3 actionable tasks: 3 executed
As you can see, everything is executed in the expected order.如您所见,一切都按预期的顺序执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.