首页 > 解决方案 > Jenkins 没有执行 Junit 测试类(Maven 项目)

问题描述

Jenkins 的新手,所以我有一个 java web 项目(maven)和一个单元测试文件。测试文件结构:src/test/java/PreProcessorTest.java

测试在intellij中成功执行。但是 Jenkins 并没有考虑这个测试文件并说构建成功。

它显示“没有要运行的测试”

詹金斯控制台日志

Commit message: "Update pom.xml"
Parsing POMs
Established TCP socket on 33330
maven35-agent.jar already up to date
maven35-interceptor.jar already up to date
maven3-interceptor-commons.jar already up to date
[DEVACDMY-29158] $ java -cp /home/jenkins/jenkins/maven35-agent.jar:/home/jenkins/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.5/boot/plexus-classworlds-2.5.2.jar:/home/jenkins/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.5/conf/logging jenkins.maven3.agent.Maven35Main /home/jenkins/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.5 /home/jenkins/jenkins/remoting.jar /home/jenkins/jenkins/maven35-interceptor.jar /home/jenkins/jenkins/maven3-interceptor-commons.jar 33330
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven:  -B -f /home/jenkins/jenkins/workspace/DEVACDMY-29158/pom.xml install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for groupId:npl:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.mockito:mockito-all:jar -> duplicate declaration of version 2.0.2-beta @ line 85, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ----------------------------< groupId:npl >-----------------------------
[INFO] Building npl 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ npl ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/jenkins/jenkins/workspace/DEVACDMY-29158/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ npl ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ npl ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/jenkins/jenkins/workspace/DEVACDMY-29158/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ npl ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /home/jenkins/jenkins/workspace/DEVACDMY-29158/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.8.1:test (default-test) @ npl ---
[INFO] Surefire report directory: /home/jenkins/jenkins/workspace/DEVACDMY-29158/target/surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[JENKINS] Recording test results
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ npl ---
[INFO] Building jar: /home/jenkins/jenkins/workspace/DEVACDMY-29158/target/npl-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ npl ---
[INFO] Installing /home/jenkins/jenkins/workspace/DEVACDMY-29158/target/npl-1.0-SNAPSHOT.jar to /home/jenkins/.m2/repository/groupId/npl/1.0-SNAPSHOT/npl-1.0-SNAPSHOT.jar
[INFO] Installing /home/jenkins/jenkins/workspace/DEVACDMY-29158/pom.xml to /home/jenkins/.m2/repository/groupId/npl/1.0-SNAPSHOT/npl-1.0-SNAPSHOT.pom
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.300 s
[INFO] Finished at: 2020-03-19T04:06:01-05:00
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /home/jenkins/jenkins/workspace/DEVACDMY-29158/pom.xml to groupId/npl/1.0-SNAPSHOT/npl-1.0-SNAPSHOT.pom
[JENKINS] Archiving /home/jenkins/jenkins/workspace/DEVACDMY-29158/target/npl-1.0-SNAPSHOT.jar to groupId/npl/1.0-SNAPSHOT/npl-1.0-SNAPSHOT.jar
channel stopped
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: SUCCESS

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>
       <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
           <junit.jupiter.version>5.6.0</junit.jupiter.version>
           <maven.compiler.release>11</maven.compiler.release>
                 <project.build.testSourceDirectory>src/test/java</project.build.testSourceDirectory>
    </properties>
             <build>
             <plugins>
             <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.8.1</version>
    </plugin>
             </plugins>
             </build>

        <groupId>groupId</groupId>
        <artifactId>npl</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-servlet-api</artifactId>
                <version>8.5.0</version>
            </dependency>
           <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
     <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.6.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.17</version>
                <type>jar</type>
                <scope>provided</scope>
            </dependency>

            <!-- https://mvnrepository.com/artifact/edu.stanford.nlp/stanford-corenlp -->
            <dependency>
                <groupId>edu.stanford.nlp</groupId>
                <artifactId>stanford-corenlp</artifactId>
                <version>3.9.2</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/edu.stanford.nlp/corenlp -->




            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.6.2</version>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.6</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>2.0.2-beta</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>2.0.2-beta</version>
                <scope>compile</scope>
            </dependency>
       </dependencies>
       </project>

(编辑)当我使用 cmd 作为“mvn test”在 pc 中构建 maven 时

 C:\Users\AP078081\IdeaProjects\npl>mvn test
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for groupId:npl:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.mockito:mockito-all:jar -> duplicate declaration of version 2.0.2-beta @ line 74, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ----------------------------< groupId:npl >-----------------------------
[INFO] Building npl 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ npl ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ npl ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ npl ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\AP078081\IdeaProjects\npl\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ npl ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ npl ---
[INFO] Surefire report directory: C:\Users\AP078081\IdeaProjects\npl\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.053 s
[INFO] Finished at: 2020-03-19T15:53:37+05:30
[INFO] ------------------------------------------------------------------------

我已经尝试了在 StackOverflow 和整个网络上找到的所有可能的解决方案。任何帮助,将不胜感激。

标签: javamavenjunit

解决方案


显然,我使用的 surefire 插件不支持 Junit 5。

所以改变这个

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

到最新版本的万能

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

推荐阅读