英文:
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(), "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 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:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
Without one of these plugins, Maven does not support the JUnit Platform.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论