This seems easy and I don't understand what I'm doing wrong. I'm using the @BeforeAll tag to create a new Sequence object and use that instance in each test method. However, it seems that the Sequence object is null and I don't understand why. I made sure that the @BeforeAll method is static.
public class SequenceTest {
static Sequence seq;
@BeforeAll
public static void createTestSequence() {
seq = new Sequence();
assertEquals(null, seq); // this passes when it shouldn't!
}
@Test
public void test1() {
// do test
// fails because Sequence object is null
}
}
I'm using maven and I included these dependencies in my pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
I think that your block code should work. Maybe your maven fire plugin does not run any test? I've tried with following block code and the test failed:
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SequenceTest {
static Sequence seq;
@BeforeAll
static void createTestSequence() {
seq = new Sequence();
assertEquals(null, seq); // this passes when it shouldn't!
}
@Test
public void test1() {
// do test
// fails because Sequence object is null
}
}
class Sequence{
}
I've got following error:
expected: but was: org.tdd.others.Sequence@9b7af25 org.opentest4j.AssertionFailedError: expected: but was: org.tdd.others.Sequence@9b7af25 at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55) at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177) at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1124) at org.tdd.others.SequenceTest.createTestSeque nce(SequenceTest.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
I was using old dependencies. Changed it to this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<!-- junit 5, unit test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.