在项目中执行默认测试时,未能执行org.apache.maven.plugins:maven-surefire-plugin:2.12:test的目标。

92

我已经尝试了几天来解决以下错误,但是我无法解决它 :(

我的模块的pom.xml文件是:

<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>
<parent>
<artifactId>junitcategorizer</artifactId>
<groupId>com.topdesk.test.junitcategorizer</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>junitcategorizer.instrument</artifactId>
<name>JUnitCategorizer InstrumentationAgent</name>
<description>The agent used to instrument the called Java classes</description>
<dependencies>
<dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm-commons</artifactId>
    <version>4.0</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>
</dependencies>
<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
        </manifest>
        <manifestEntries>
            <Premain-Class>com.topdesk.junitcategorizer.instrument.InstrumentationAgent</Premain-Class>
            <Agent-Class>com.topdesk.junitcategorizer.instrument.InstrumentationAgent</Agent-Class>
            <Can-Redefine-Classes>true</Can-Redefine-Classes>
            <Can-Retransform-Classes>true</Can-Retransform-Classes>
            <Boot-Class-Path>${project.artifactId}-${project.version}.jar</Boot-Class-Path>
            <Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
        </manifestEntries>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.5</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration> 
          <artifactSet>
            <includes>
              <include>org.ow2.asm:*</include>
            </includes>
          </artifactSet>
          <relocations>
            <relocation>
              <pattern>org.objectweb.asm</pattern>
              <shadedPattern>org.shaded.asm</shadedPattern>
            </relocation>
          </relocations>
        </configuration>
      </execution>
    </executions>
  </plugin>
 </plugins>
 </build>
 </project>

我遇到了以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
12:test (default-test) on project junitcategorizer.instrument: There are test failures.
[ERROR]
[ERROR] Please refer to D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project junitcategorizer.instrument: There are test failures.

Please refer to D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports for the individual test results.

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

    Please refer to D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports for the individual test results.

    at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution(SurefireHelper.java:83)
    at org.apache.maven.plugin.surefire.SurefirePlugin.writeSummary(SurefirePlugin.java:673)
    at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary(SurefirePlugin.java:647)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:137)
    at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:98)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 19 more
    [ERROR]
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.

我尝试了以下方法但没有帮助:

  1. 运行命令:mvn dependency::tree
  2. 运行命令:mvn clean install -U 或右击“项目”,选择“Maven” >> “Update”

请帮助我!


我曾经遇到过类似的问题,我的答案在这里:https://dev59.com/zuo6XIcBkEYKwwoYPR7f#41666587 - Spenhouet
检查冲突的依赖项并将其删除。当我删除了JPA依赖项时,它对我起作用了。由于Spring Boot内置了JPA,因此我删除了我拥有的外部依赖项。希望它能够正常工作! :) - Madhawa Dias
确保没有测试用例失败。 - Amit Upadhyay
OP当时使用的是2.12版本,而那个时候它已经发布了4年。自那以后,Maven Surefire插件甚至有了更新的版本。https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin - MarkHu
37个回答

109

这解决了我的问题。我的POM中是2.10版本,我刚刚更新到了2.19.1并刷新了POM。

将以下内容添加到你的pom文件中:

 <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.19.1</version>
        </plugin>
  </plugins>

根据您的错误代码,他没有找到surefire插件,因此请添加它


1
谢谢您的回复!但是这并没有帮助我,因为这个pom文件是模块的,而父pom文件确实有那个插件。 - Srinu
那不是问题! - Spenhouet
3
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> 该代码段是一个Maven插件的配置,用于运行Java单元测试。其中,<groupId>指定了插件的组ID,<artifactId>指定了插件的唯一标识符,<version>指定了插件的版本号。在<configuration>标签中,设置<testFailureIgnore>为true则表示在测试失败时不会中断构建过程。 - Devendra Singraul
2
在我的情况下,我不得不从2.22.2回退到2.22.1(我仍然不明白为什么,但它可以工作:S) - White_King
这对我没用。环境:Oracle Java 17 LTS;Windows 10;Maven 3.8.2 尝试构建Pentaho Kettle。 - KailiC
关于为什么会发生这种情况的提示,请点击此处 - 正如他们在那里评论的那样,从2.22.0版本开始,Surefire要求使用JUnit 5。 - undefined

43

这对我有用!谢谢。 - Termato

29

这种情况发生在 Maven 在构建 jar 文件时尝试运行测试用例的时候。您可以通过在 Maven 命令的末尾添加 -DskipTests 来跳过运行测试用例。

例如:mvn clean install -DskipTestsmvn clean package -DskipTests


1
这是某些情况下的一个好解决方案,但当我们需要进行单元测试时,似乎需要另一种答案。我能得到一些建议吗? - Cheolsoon Im
完全的Java新手想知道,在运行mvn clean package -DskipTests之后(并且我的jar包已经构建完成),是否有另一个命令可以在构建后运行测试? - bkwdesign

11

曾经多次遇到同样的问题,我有两个解决方案:

解决方案1: 在pom.xml中添加surefire插件引用。确保你有所有的节点!在我的IDE自动导入版本中缺少了一个节点!!!

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
    </plugin>
</plugins>
解决方案 2: 我的 IDE 在文件开头添加了错误的导入。

IDE 添加

import org.junit.Test;

我不得不用新的替换它

import org.junit.jupiter.api.Test;

2
在升级maven-surefire-plugin后,这对我完美地起了作用。 - SyntaX

8

试试这个,它有效!

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <shutdown>kill</shutdown> <!-- Use it if required-->
                </configuration>
            </plugin>

10
将此设置为true只是忽略故障,而不是帮助故障停止。 - mrinal

7
这是一种测试失败。@SpringBootApplication注释包含以下配置。
1) @Configuration
2) @ComponentScan
3) @EnableAutoConfiguration
@EnableAutoConfiguration 是造成这个错误的原因。它会尝试根据 pom.xml 中的依赖项自动配置应用程序。
例如,当您有 spring-data-jpa 依赖项在 pom.xml 文件中时,它会尝试通过查看 application.properties 文件中的数据源来向应用程序添加配置。因此,您需要添加数据源来解决此问题。
对于 MySQL:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost/lahiru
spring.datasource.username=root
spring.datasource.password=

或者你可以通过跳过测试来隐藏这个问题。

mvn install -DskipTests

更多细节请参考。


7

对我而言,在3.0.0-M1版本上运行良好。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M1</version>
</plugin>

您可能需要使用sudo命令运行它。


3

当测试失败时,我会得到完全相同的堆栈跟踪。 更靠近顶部,您应该看到标识失败测试类的消息。 或转到

D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports

请查看失败报告。 修复问题,您的构建就可以正常了。

好消息:您的POM文件似乎没问题,Maven可以编译和执行测试。


3
我遇到了同样的问题,尝试了几乎所有这些建议和其他选项,但对于我的情况都没有起作用。
所以我运行了详细命令:(mvn install -X),看到了Jacoco库的问题,最终发现了这个答案,它对我有用。
在我的pom文件中更改Jacoco库的版本。
<groupId>org.jacoco</groupId>
     <artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>

3

以下是最简单的解决此错误的方法:

1)进入您的 pom.xml 文件路径

2)并编辑 pom.xml,例如:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
    </plugin>
</plugins>

3) 保存文件 就是这样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接