I have a JUnit 5 test class with a non static String attribute (value
).
The test class has 2 test methods, which are executed in a defined order (for question order does not matter).
The first executed test method sets the String attribute to "Test".
But when second test method is executed the String attribute is null
instead of "Test".
Why is it like this in JUnit 5?
Beside making the String attribute static, can I handle this in another way?
Here is some sample code:
@TestMethodOrder(OrderAnnotation.class)
class Junit5Test {
private String value;
@Test
@Order(1)
void setValueTest() {
this.value = "Test";
assertNotNull(this.value);
}
@Test
@Order(2)
void readValueTest() {
// This test fails, because value is null. Why?
assertNotNull(this.value);
}
}