当前位置: 代码迷 >> java >> Maven Java Surefire 无法创建测试类
  详细解决方案

Maven Java Surefire 无法创建测试类

热度:60   发布时间:2023-08-02 11:01:03.0

我在运行mvn test时遇到了这个问题。 这是输出的一部分:

Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] There was an error in the forked process
[ERROR] Unable to create test class 'com.models.ExampleTest'
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] Unable to create test class 'com.ExampleTest'
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:657)
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:283)
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:246)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1161)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1002)

这是我的 pom.xml 的一部分:

<build>
       <plugins>    
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
</build>
<dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
</dependencies>

我添加了<useSystemClassLoader>false</useSystemClassLoader>因为当 mvn test 命令给我时我遇到了问题<useSystemClassLoader>false</useSystemClassLoader> Could not find or load main class org.apache.maven.surefire.booter.ForkedBooter 所以我想我的 ClassLoader 有一些东西。 我使用 Ubuntu 18.04、openjdk-11 和 maven 3.6.0。 但是我尝试使用 oracle-jdk-11 和 openjdk-8 运行它。 更重要的是,这个问题不依赖于一个项目。 我用 maven 创建了一个空项目,并在那里添加了简单的类和简单的测试——结果是一样的。

正如日志所说:

请参考转储文件(如果有的话)[date].dump、[date]-jvmRun[N].dump 和 [date].dumpstream

您需要查看xxx.dump文件。 您应该可以在项目的target目录中找到它。 在该文件中,您将找到导致

[错误] 无法创建测试类“com.models.ExampleTest”

此堆栈跟踪将与输出中的堆栈跟踪不同,它将帮助您确定错误的真正原因(在我的情况下它是 Javassist 版本,但它可能与您的情况无关)。

根据我的理解(我对 Surefire 一无所知),Surefire 充当容器并创建一个进程来运行测试。 如果这个“内部”过程出现问题,它将被记录在转储文件中。 但这不是 Surefire 的错(因为输出可能会让你想到)。

  相关解决方案