代码之家  ›  专栏  ›  技术社区  ›  AirSquid

maven单元测试失败,但独立通过

  •  1
  • AirSquid  · 技术社区  · 3 年前

    JUnit5不会在用 @BeforeEach 注释,在这里我初始化测试中需要的测试对象的一些字段。当尝试访问测试方法(用注释的方法)内部的这些字段时 @Test )我显然得到了一个NullpointerException。因此,我向这些方法添加了一些输出消息。

    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    
    public class TestClass {
       private String s;
    
       public TestClass() {
    
       }
    
       @BeforeEach
       public void init() {
          System.out.println("before");
          s = "not null";
       }
    
       @Test
       public void test0() {
          System.out.println("testing");
          assertEquals("not null", s.toString());
       }
    
    }
    

    在运行时的测试输出中 mvn clean test 我从 test0() 方法注释为 @测试 注释,但不会打印“before”消息。

    Running de.dk.spielwiese.TestClass
    !!!testing!!!
    Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0 sec <<< FAILURE!
    de.dk.spielwiese.TestClass.test0()  Time elapsed: 0 sec  <<< FAILURE!
    java.lang.NullPointerException
            at de.dk.spielwiese.TestClass.test0(TestClass.java:24)
    

    我能想到的最明显也是唯一的原因是 init() 方法未被调用。文件 @BeforeEach

    @BeforeEach用于表示带注释的方法 在每次@Test、@RepeatedTest、@ParameterizedTest之前执行, @TestFactory和@TestTemplate方法。

    我还试着在eclipse中运行测试,它们总是通过,没有任何错误。

    我使用的是maven 3.5.3。 我在pom.xml中声明JUnit Jupiter 5.1.0为依赖项

    <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>de.dk</groupId>
    <artifactId>spielwiese</artifactId>
    <version>0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>Spielwiese</name>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>de.dk.spielwiese.Spielwiese</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <appendAssemblyId>false</appendAssemblyId>
                    <finalName>Spielwiese</finalName>
                </configuration>
                <executions>
                    <execution>
                        <id>assemble-all</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
        </plugins>
    </build>
    
    <dependencies>
        <dependency>
            <groupId>de.dk</groupId>
            <artifactId>util</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    为什么是我的 init() 方法未被调用?

    0 回复  |  直到 8 年前
        1
  •  211
  •   Michael    6 年前

    就我而言,问题在于 @Test 注释取自错误的导入。 最初它是从 org.junit.Test 。 一旦我切换到 org.junit.jupiter.api.Test 问题解决了。

    原始代码错误:

    import org.junit.Test;
    
    @BeforeEach
    ...some code
    
    @Test
    ...some code
    

    正确的固定代码:

    import org.junit.jupiter.api.Test;
    
    @BeforeEach
    ...some code
    
    @Test
    ...some code
    
        2
  •  47
  •   Sam Brannen    8 年前

    你的 init() 方法未被调用,因为您尚未指示Maven Surefire使用JUnit平台Surefire提供程序。

    因此 惊人地 您的测试甚至没有使用JUnit运行。相反,它是在Maven Surefire对他们所谓的支持下运行的 POJO Tests

    将以下内容添加到 pom.xml 应该解决问题。

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.1.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    
        3
  •  7
  •   KHanusova    6 年前

    现在不需要在插件中添加提供者。只需将junitjupiter引擎添加到您的依赖项中(如官方文档中所写 https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html )。

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
    
        4
  •  7
  •   Srikanth    5 年前

    我的gradle项目也面临同样的问题。 注意到,@Test annotation使用了错误的包( org.junit.Test )使用正确的软件包后问题得到解决( org.junit.jupiter.api.Test )

        5
  •  4
  •   nikli    6 年前

    junit-jupiter-api 缺少依赖项

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
    </plugins>
    
        6
  •  2
  •   Display name    5 年前

    在我的例子中,问题是我在测试的子类中重写了一个用@BeforeEach注释的方法,所以没有调用超级方法。

        7
  •  2
  •   OSGI Java    4 年前

    为了让Maven使用 @BeforeEach 您必须通过正确配置您的项目 pom.xml

    您的项目 pom.xml 必须包含以下部分:

    1. dependencyManagement 具有 Junit BOM
    2. dependency 具有 朱尼特木星
    3. plugin 具有 Maven Surefire插件

    这是正式记录的 here 官方项目示例如下 here

    这是一个 link 到示例项目的 pom.xml

    以下是示例项目的 pom.xml 为了您的方便:

    <?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>com.example</groupId>
        <artifactId>junit5-jupiter-starter-maven</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.junit</groupId>
                    <artifactId>junit-bom</artifactId>
                    <version>5.7.2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.2</version>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
        8
  •  0
  •   Eric Romrell    8 年前

    Sam Brannen的回答对我来说很有效,但它似乎不适用于2.22.0版本的maven surefire插件,除非你将junit平台的surefire提供程序升级到1.2.0。注意!