在构建Jenkins插件时尝试运行单元测试时出现NoClassDefFoundError

时间:2018-03-15 04:39:00

标签: maven jenkins jenkins-plugins

我有一个我自己构建的自定义Jenkins插件。我有一些测试用例可以利用jenkins-test-harness项目中的一些代码(即Junit JenkinsRule。)

无论如何,当我在IntelliJ中运行它时,单元测试运行并通过。没有例外,没有错误。

但是,当我尝试从Maven命令行运行它们时,所有依赖于JenkinsRule的测试都会失败:

[ERROR] testGetLastDate(com.mycompany.myplugin.portlet.utils.UtilsHudsonTest)  Time elapsed: 0 s  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.jvnet.hudson.test.TestPluginManager
        at org.jvnet.hudson.test.JenkinsRule.<init>(JenkinsRule.java:325)
        at com.mycompany.myplugin.portlet.utils.UtilsHudsonTest.<init>(UtilsHudsonTest.java:19)

TestPluginManager是in the jenkins-test-harness jar。尝试调用它的JenkinsRule就在它旁边的同一个jar中的同一个包中,所以很明显jar本身在类路径上是成功的。

我无法弄清楚为什么我无法使用mvn test从命令行运行此作业。

我的POM非常简单 - 我在Jenkins插件-3pom上声明了一个Parent:

<parent>
  <groupId>org.jenkins-ci.plugins</groupId>
  <artifactId>plugin</artifactId>
  <version>3.2</version>
  <relativePath/>
</parent>

我依赖于测试工具:

  <dependencies>
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>dashboard-view</artifactId>
      <version>${dashboard-view.version}</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.jenkins-ci.main</groupId>
      <artifactId>jenkins-test-harness</artifactId>
      <scope>test</scope>
      <version>2.34</version>
    </dependency>
  </dependencies>

(这是整个依赖关系部分,没有遗漏。)

在我的构建部分中,我只声明了编译器,surefire和hpi插件:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
      </plugin>
      <plugin>
        <groupId>org.jenkins-ci.tools</groupId>
        <artifactId>maven-hpi-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>hpi</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

(surefire配置继承自插件-3.2父级。)

以下是导致错误的UtilHudsonTest位 - 第19行是@Rule注释:

public class UtilsHudsonTest {

    @Rule // This is line 19
    public JenkinsRule j = new JenkinsRule();

    @Test
    public void testGetLastDate() throws Exception {
        FreeStyleProject prj = j.createFreeStyleProject("prj1");
        prj.scheduleBuild2(0).get();
        FreeStyleProject prj2 = j.createFreeStyleProject("prj2");
        prj2.scheduleBuild2(0).get();

        List<Job> jobs = new ArrayList<>();
        jobs.add(prj);
        jobs.add(prj2);

        LocalDate lastDate = Utils.getLastDate(jobs);
        assertNotNull(lastDate);
    }

    // other tests
}

我有点难过为什么这会在IntelliJ中运行而不是在Maven中运行。 mvn dependency:tree显示正确的和预期的依赖关系。关于如何让这个插件从命令行成功运行测试的任何想法?

0 个答案:

没有答案
相关问题