Maven Surefire 未检测到 Junit5

Maven Surefire not detecting Junit5

我在这里更新了一个非常简单的 Maven 项目,我得到它简化了我的真实项目: GitHub repository

我需要运行 Surefire 插件来编译测试,以便Jacoco 可以使用它们。我尝试了很多东西,甚至 changing/reducing 依赖项,但 none 有效! 我排除了 maven-compiler-plugin 中的测试,因为它说“包 main.it.isp.utility 不存在”。不过我不知道排除它是否正确....

这是我的 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>it.isp.batch</groupId>
<artifactId>Estrazioni-Batch</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
    <!-- Dependency versions -->
    <junit.jupiter.version>5.5.2</junit.jupiter.version>
    <!-- Java 8 -->
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <java.version>1.8</java.version>
    <!-- Encoding -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.17.2</version>
    </dependency>
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-engine</artifactId>
        <version>5.0.0-ALPHA</version>
    </dependency>
    <!-- Jupiter API for writing tests -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.2</version>

    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>1.8.2</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>4.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.18</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.3.18</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>4.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.1.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.9.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>

</dependencies>

<build>
    <finalName>Estrazioni-Batch</finalName>
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>

    <resources>
        <resource>
            <directory>src/</directory>
            <excludes>
                <exclude>test/</exclude>
            </excludes>
        </resource>
    </resources>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>

            <version>3.10.1</version>


            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <outputDirectory>${basedir}/target/diraliases/BATCHROOT/Estrazioni-batch/classes</outputDirectory>
                <testExcludes>
                    <testExclude>**/*Test*</testExclude>
                </testExcludes>
            </configuration>

        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M6</version>
            <configuration>
                <testFailureIgnore>false</testFailureIgnore>
                <skip>false</skip> <!-- Please take care of these parameters. They will help you to spot failure 
                    in test assertions -->
                <skipTests>false</skipTests>
                <testSourceDirectory>${basedir}/src/main/test/</testSourceDirectory>

                <properties>
                    <property>
                        <name>surefire.testng.verbose</name>
                        <value>10</value>
                    </property>
                </properties>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0-M1</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.8.2</version>

                </dependency>
                <!-- https://mvnrepository.com/artifact/org.apache.maven.surefire/surefire-api -->
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-api</artifactId>
                    <version>3.0.0-M6</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.7</version>
            <!-- Suggested to always use the latest, at minimun 0.8.3 -->
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <!-- EXEC format is still used as an intermediate report format, but 
                            it purpose is only internal to JaCoCo execution -->
                        <!-- It will not be parsed by Sonar. There will be one EXEC file for 
                            each module of the application -->
                        <destFile>./jacoco-${project.artifactId}.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>report</id>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <!-- The purpose of REPORT goal is to convert each EXEC in evry output 
                            format that JaCoCo is able to produce, including XML -->
                        <!-- The XML files are placed inside a temporary directory -->
                        <dataFile>./jacoco-${project.artifactId}.exec</dataFile>
                        <outputDirectory>./sonar-report/temp-jacoco-xml/${project.artifactId}</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.coderplus.maven.plugins</groupId>
            <artifactId>copy-rename-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>copy-file</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <!-- The purpose of this plugin is to collect all the XML files under 
                            the temp directory in each module -->
                        <!-- and copy in to the right path scanned by Sonar. There will be 
                            one XML file for each module -->
                        <sourceFile>./sonar-report/temp-jacoco-xml/${project.artifactId}/jacoco.xml</sourceFile>
                        <destinationFile>./sonar-report/jacoco-xml/jacoco-${project.artifactId}.xml</destinationFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>






    </plugins>



</build>

最重要的是遵循 Apache Maven 中的约定。所以不要更改源目录和测试目录的配置,这在 99.999% 的情况下都是没有意义的。

所以先去掉以下配置部分:

<finalName>Estrazioni-Batch</finalName>    
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>

<finalName> ..</finalName> 没有意义,因为您的工件是根据您的 artifactId 命名的...此外,artifactId 应该是小写的。

您的 Eclipse 配置有误,或者它基于错误的配置。

生产代码属于 src/main/java/<packageName>,相应的单元或集成测试应在 src/test/java/<packageName> 中完成。

对于单元测试,您应该遵循 *Test.java 您已经做过的命名约定。对于集成测试,您应该使用 *IT.java.

进一步删除资源的配置:

 <resources>
        <resource>
            <directory>src/</directory>
            <excludes>
                <exclude>test/</exclude>
            </excludes>
        </resource>
    </resources>

因为在您的设置中您已经使用 src/main/resources 进行测试 src/test/resources

查看您提供的 pom 文件还有其他奇怪的事情,例如混合不同 junit-jupiter versions/parts.

因此,首先从您的 pom 文件中删除以下内容:

<dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit5-engine</artifactId>
    <version>5.0.0-ALPHA</version>
</dependency>
<!-- Jupiter API for writing tests -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.2</version>

</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.8.2</version>
</dependency>

最简单的处理方法是使用如下所示的 junit bom 文件:

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.8.2</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      ..
  </dependencyManagement>

之后,您必须像这样在 pom 中定义一个依赖项:

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

进一步删除 maven-surefire-plugin 的整个 setup/configuration 并替换为以下内容:

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

maven-compiler-plugin 的整个 configuration/setup 应该看起来像这样:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.10.1</version>
</plugin>

同时删除 jacoco-maven-plugin 的整个 configuration/setup 并将其替换为:

<plugins>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.8</version>
    <executions>
      <execution>
        <goals>
          <goal>prepare-agent</goal>
          <goal>report</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

删除 copy-rename-maven-plugin 的整个 configuration/setup。

关于依赖项使用的一些话:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

为什么要使用同一个工件的不同版本?删除 commons-lang:commons-lang:2.6 并继续使用 org.apache.commons:commons-lang3:3.12.0

如果您想使用声纳报告,您应该执行此处描述的 sonar-maven-plugin:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-maven/

有完整的工作示例https://github.com/khmarbaise/example-junitjupiter

最后一个强烈建议是定义构建期间使用的所有插件(参见示例)..