使用@TempDir在实例变量上时出现空指针异常。

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

Getting NullPointerException when using @TempDir on instance variable

问题

我有一个测试类,其中有一个用 @TempDir 注释的实例变量。类中的许多测试使用这个变量,并期望为每个测试创建一个新的 TempDir(这似乎是正确的)。

当我在 Idea 中运行测试时,它们都能成功完成。然而,当我从终端运行它们时,它们会失败,因为 TempDir 为 null。如果我将 @TempDir 移动到每个测试中,作为每个方法的参数,测试在 Idea 和终端中都能成功运行。

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;

public class FileTest {
    @TempDir
    private Path tempDir;

    @Test
    public void testConstructor_FileDoesNotExist_ExpectIllegalArgumentException() {
        Path nonExistingFile = Path.of(tempDir.toString(), "NonExistingFile.xls");
        Assertions.assertFalse(() -> nonExistingFile.toFile().exists());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>file-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

我尝试过使用不同的测试和实例变量作用域,但结果是相同的。这是什么原因?

英文:

I have a test class with an instance variable annotated with @TempDir. Many of the tests in the class use this variable and expected that a new TempDir will be created for each test (and this looks too be true).

When I run the tests in Idea they all complete successfully. However when I run them from a terminal they fail because the TempDir is null. If I move the @TempDir to each test as an argument to each method the test runs successfully in both Idea and in a terminal.

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;

public class FileTest {
    @TempDir
    private Path tempDir;

    @Test
    public void testConstructor_FileDoesNotExist_ExpectIllegalArgumentException() {
        Path nonExistingFile = Path.of(tempDir.toString(), &quot;NonExistingFile.xls&quot;);
        Assertions.assertFalse(() -&gt; nonExistingFile.toFile().exists());
    }
}
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.test&lt;/groupId&gt;
    &lt;artifactId&gt;file-test&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;

    &lt;dependencies&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.9.2&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
            &lt;version&gt;5.9.2&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

I have tried using different scopes for the tests and instance variable but the results where the same.

What is the reason for this?

答案1

得分: 1

你需要在你的pom.xml文件中添加Maven Surefire插件或者Maven Failsafe插件:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>
    </plugins>
</build>

如果没有这两个插件之一,Maven将不支持JUnit平台。
英文:

You'll have to add either Maven Surefire Plugin or Maven Failsafe Plugin to your pom.xml:

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.0.0&lt;/version&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

Without one of these plugins, Maven does not support the JUnit Platform.

huangapple
  • 本文由 发表于 2023年4月19日 22:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055649.html
匿名

发表评论

匿名网友

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

确定