简体   繁体   English

使用@BeforeAll 在 JUnit 中实例化一个 object 返回 null

[英]Using @BeforeAll to instantiate an object in JUnit returns null

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.我正在使用@BeforeAll 标签创建一个新的序列 object 并在每个测试方法中使用该实例。 However, it seems that the Sequence object is null and I don't understand why.但是,似乎序列 object 是 null 我不明白为什么。 I made sure that the @BeforeAll method is static.我确保@BeforeAll 方法是 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:我正在使用 maven 并将这些依赖项包含在我的 pom.xml 文件中:

<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?也许您的 maven 消防插件没有运行任何测试? 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 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) nce(SequenceTest.java:16) 在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM