当前位置: 首页 > 知识库问答 >
问题:

Jenkins没有执行Junit测试类(Maven项目)

庄智
2023-03-14

Jenkins不熟悉,所以我有一个java web项目(maven)和一个单元测试文件。测试文件结构:src/test/Java/preprocessortest . Java

该测试在intellij中成功执行。但是Jenkins并不认为这个测试文件是成功的。

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

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和整个网络上找到的所有可能的解决方案。任何帮助都将不胜感激。

共有2个答案

董花蜂
2023-03-14

Surefire似乎无法检测到您的JUnit 5平台。

Surefire如何检测平台引擎

根据Surefire doc,如果没有配置任何内容,Surefire会通过以下算法检测使用哪个JUnit版本:

if the JUnit 5 Platform Engine is present in the project
    use junit-platform
if the JUnit version in the project >= 4.7 and the <<<parallel>>> configuration parameter has ANY value
    use junit47 provider
if JUnit >= 4.0 is present
    use junit4 provider
else
    use junit3.8.1

指定提供程序

您可以尝试指定一个提供程序以确保:(使用 JUnit 5.1.0,JUnit 平台 1.1.0)

<build>
   <plugins>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.19.1</version>
      <dependencies>
         <dependency>
           <groupId>org.junit.platform</groupId>
           <artifactId>junit-platform-surefire-provider</artifactId>
           <version>${org.junit.platform.version}</version>
         </dependency>
         <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${org.junit.version}</version>
         </dependency>
      </dependencies>
      </plugin>
   </plugins>
</build>
闻人升
2023-03-14

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

所以改变这个

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

到surefire的最新版本

<groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M4</version>
 类似资料:
  • 当在我的pom.xml中启用强制使用JUnit47提供程序时,我得到: pom.xml如下所示

  • 我已经导入了JUNIT 4.12和Hamcrest 1.3库以及测试文件夹设置。指向正确测试包文件夹的源代码。我将使用Shift F6为每个Java测试文件运行测试,它将执行该Java文件中的所有测试。重新启动计算机后,我按照同样的程序运行测试,结果“没有执行任何测试”。原因和解决方案可能是什么。 没有任何修改的同一个项目可以在另一台计算机上正常工作。试图重新安装Netbeans 8.2,但它没有

  • 在使用maven运行单元测试时,我遇到了这个异常。我的所有测试都没有执行。我的测试类的格式是 我正在运行以下命令来运行此命令: 使用的surefire插件是: 有人知道为什么我的测试没有执行吗?我用的是jUnit 4.8.2和surefire 2.14.1

  • 我有一个包含JUnit 4和JUnit 5测试的Maven项目。 我使用的是版本<code>3.0.0-M07<code>中的Maven Surefire插件。Maven依赖关系树中提供了以下相关依赖关系(我使用<code>mvn依赖关系:树): 根据JUnit 5留档,JUnit Vintage Engine需要存在才能执行JUnit 4测试。 然而,Maven Surefire插件同时执行JU

  • 这是我添加的公共类NewEmptyJUnitTest的代码{ 这是pom.xml文件 我一直收到这个错误:

  • 我试图用jenkins execution命令测试我的java项目,但是我得到了以下错误消息: 有关单个测试结果,请参阅/var/jenkins_home/workspace/test1/target/surefire-reports。请参阅转储文件(如果存在)[date].dump,[date]-jvmrun[N].dump和[date].dumpstream。分叉的VM在没有正确地说再见的情况