使用@BeforeAll在JUnit中实例化一个对象会返回null。

huangapple go评论81阅读模式
英文:

Using @BeforeAll to instantiate an object in JUnit returns null

问题

这似乎很容易,我不明白我做错了什么。我使用 @BeforeAll 标签来创建一个新的 Sequence 对象,并在每个测试方法中使用该实例。然而,Sequence 对象似乎是 null,我不明白为什么。我确保 @BeforeAll 方法是静态的。

public class SequenceTest {
    static Sequence seq;

    @BeforeAll
    public static void createTestSequence() {

        seq =  new Sequence();
        assertEquals(null, seq);  // 这个测试通过了,但实际上不应该通过!
    }

    @Test
    public void test1() {
        // 进行测试
        // 失败,因为 Sequence 对象是 null
    }
}

我正在使用 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>
英文:

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&#39;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:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
    &lt;version&gt;2.16&lt;/version&gt;
&lt;/plugin&gt;

&lt;!-- junit 5, unit test --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
    &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
    &lt;version&gt;5.3.1&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

答案1

得分: 0

我认为你的代码块应该是有效的。也许你的Maven Fire插件没有运行任何测试?我已经尝试了以下代码块,测试失败了:

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{

}

我得到了以下错误:

> 期望值: <null>,但实际值为: <org.tdd.others.Sequence@9b7af25>
> org.opentest4j.AssertionFailedError: 期望值: <null>,但实际值为:
> <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.createTestSequence(SequenceTest.java:16)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
英文:

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&#39;t!
    }

    @Test
    public void test1() {
        // do test
        // fails because Sequence object is null
    }
}

class Sequence{

}

I've got following error:

> expected: <null> but was: <org.tdd.others.Sequence@9b7af25>
> org.opentest4j.AssertionFailedError: expected: <null> 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.createTestSequence(SequenceTest.java:16)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

答案2

得分: 0

我之前在使用旧的依赖项。将其更改为以下内容:

          <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>
英文:

I was using old dependencies. Changed it to this:

          &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.0.0-M5&lt;/version&gt;
            &lt;/plugin&gt;

       &lt;!-- junit 5, unit test --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
            &lt;version&gt;5.4.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年8月17日 22:20:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63452807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定