Java 在 Jenkins 插件中使用 Maven 进行 JUnit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24446979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:14:58  来源:igfitidea点击:

JUnit test with Maven in a Jenkins plugin

javaeclipsemavenjunitjenkins

提问by 23ars

I have a problem when I tried to run JUnit tests with Maven. For a Jenkins plug-in I wrote a class for test. For example: I have the class ConsoleParserin package com.jenkins_pluginin folder src/main/java. The JUnit test case is ConsoleParserTestin package com.jenkins_pluginin folder src/test. For running the JUnit test I added the dependency in the pom:

当我尝试使用 Maven 运行 JUnit 测试时遇到问题。对于 Jenkins 插件,我编写了一个测试类。例如:我在src/main/java文件夹ConsoleParser中的com.jenkins_plugin包中有这个类。JUnit 测试用例位于src/test文件夹中的com.jenkins_plugin 包中。为了运行 JUnit 测试,我在 pom 中添加了依赖项:ConsoleParserTest

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version> 
        </dependency>
</dependencies>

And I ran in cmd the next command: mvn -Dtest=ConsoleParserTest test

我在 cmd 中运行了下一个命令: mvn -Dtest=ConsoleParserTest test

The problem is that I've got the following error:

问题是我遇到了以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
12:test (default-test) on project swatt_jenkins: No tests were executed!  (Set -
DfailIfNoTests=false to ignore this error.) -> [Help 1]

The full pom.xmlis:

完整的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <build>
        <finalName>Jenkins_Plugin</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>emma-maven-plugin</artifactId>
                <version>1.0-alpha-3</version>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <parent>
        <groupId>org.jenkins-ci.plugins</groupId>
        <artifactId>plugin</artifactId>
        <version>1.526</version>
    </parent>


    <groupId>Jenkins_Plugin</groupId>
    <version>1.0</version>
    <packaging>hpi</packaging>



    <repositories>
        <repository>
            <id>repo.jenkins-ci.org</id>
            <url>http://repo.jenkins-ci.org/public/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>repo.jenkins-ci.org</id>
            <url>http://repo.jenkins-ci.org/public/</url>
        </pluginRepository>
    </pluginRepositories>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version> 
        </dependency>
    </dependencies>
</project>

Also, I must mention that with those settings I ran successfully the test but after adding findbugs it crashed. So, I removed findbugs and try to run the test again but the problem that I mentioned appeared. So, can someone explain what I have to do? I tried to add another version of JUnit, Surefire but I had the same problem and I don't understand why.

另外,我必须提到,使用这些设置我成功运行了测试,但在添加 findbugs 后它崩溃了。所以,我删除了 findbugs 并尝试再次运行测试,但我提到的问题出现了。那么,有人可以解释我必须做什么吗?我试图添加另一个版本的 JUnit,Surefire,但我遇到了同样的问题,我不明白为什么。

采纳答案by Puce

Several things:

几件事:

  • add you JUnit classes to src/test/javanot src/test
  • for your JUnit class use the scope "test"
  • 将您的 JUnit 类添加到src/test/javanotsrc/test
  • 对于您的 JUnit 类,请使用范围“test”

E.g.:

例如:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version> 
    <scope>test</scope>
</dependency>

Also I recommend to manage your repositories in a repository manager such as Nexus, not in your POM:

此外,我建议在存储库管理器(例如 Nexus)中管理您的存储库,而不是在您的 POM 中:

http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html

http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html