【问题标题】:Test class cannot resolve main class when trying to Unit Test groovy using maven on Jenkins尝试在 Jenkins 上使用 maven 对 groovy 进行单元测试时,测试类无法解析主类
【发布时间】:2020-04-09 14:02:58
【问题描述】:

我无法让我的单元测试在 Maven 中为用 Groovy 编写的 Jenkins 共享库工作。

我是 Maven 新手,对 Jenkins 来说相对较新。情况如下: 我们有一个 TFVC 服务器托管我们的共享库。共享库是这样存储的:

TFVC
- sharedLibrary
    - src
        - br
            - common
                - v1
                    - SomeClass1.groovy
                    - SomeClass2.groovy
                    - SomeClass3.groovy
        - test
            - groovy
                - SomeClass1Tests.groovy
                - SomeClass2Tests.groovy
- Jenkinsfile
- pom.xml

结构 src-br-common-v1 不能更改。我根据网上找的资料添加了test-groovy结构。

Jenkinsfile 包含用于在 Maven 中测试库的作业。它在召唤

mvn clean gplus:testCompile

我的 POM 如下所示:

<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>my.company</groupId>
  <artifactId>sharedLib</artifactId>
  <version>1.0</version>
  <name>Jenkins Shared Pipeline Library</name>

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

  <dependencies>
    <dependency>
      <groupId>org.jenkins-ci.main</groupId>
      <artifactId>maven-plugin</artifactId>
      <version>3.5</version>
    </dependency>

    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-install-plugin</artifactId>
      <version>2.5.2</version>
    </dependency>

    <dependency>
      <groupId>org.jenkins-ci.main</groupId>
      <artifactId>jenkins-core</artifactId>
      <version>2.85</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.12</version>
    </dependency>

    <dependency>
      <groupId>com.cloudbees</groupId>
      <artifactId>groovy-cps</artifactId>
      <version>1.11</version>
    </dependency>

    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>script-security</artifactId>
      <version>1.24</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
    </dependency>

    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>pipeline-utility-steps</artifactId>
      <version>1.5.1</version>
    </dependency>

   <dependency>
      <groupId>org.apache.maven.doxia</groupId>
      <artifactId>doxia-site-renderer</artifactId>
      <version>1.9.2</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/br/common/v1</sourceDirectory>
    <testSourceDirectory>src/test/groovy</testSourceDirectory>
    <resources>
      <resource>
        <directory>E:\Jenkins\plugins\pipeline-utility-steps\WEB-INF\lib</directory>
      </resource>
    </resources>
    <plugins>
     <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>addSources</goal>
              <goal>addTestSources</goal>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
        <sources>
          <source>
            <directory>${project.basedir}/src/br/common/v1</directory>
            <includes>
              <include>**/*.groovy</include>
            </includes>
          </source>
        </sources>
        <testSources>
          <testSource>
            <directory>${project.basedir}/src/br/common/v1</directory>
            <directory>${project.basedir}/src/test/groovy</directory>
            <includes>
              <include>**/*.groovy</include>
            </includes>
          </testSource>
        </testSources>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <configuration>
          <trimStackTrace>false</trimStackTrace>
          <argLine>${surefireArgLine}</argLine>
          <includes>
            <include>**/*Test*.*</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

这是一个 Test 类的简化版本,我们假设 SomeClass1 包含一个方法 returnTrue ,它完全符合您的想法:

package test.groovy;
import br.common.v1.SomeClass1;

public class SomeClass1Tests extends GroovyTestCase
{
    public void testReturnTrue() {
        def someClass1Object = new SomeClass1();

        def expected = true;

        def result = someClass1Object.returnTrue();

        assertEquals(expected, result);
    }
}

我现在遇到的问题是我的 Test 类无法解析我想要测试的类。

Unable to resolve class br.common.v1.SomeClass1 @ line 2, column 1

最初我将测试文件放在 TFVC 的另一个位置,但这不起作用,我读到 gmaven-plus 对存储测试类的位置非常挑剔。

我希望我以实用的方式提供了所有需要的信息,如果我遗漏了什么,请告诉我。

提前感谢您的帮助!

【问题讨论】:

  • 乍一看,您似乎将源目录设置为深度。如果要导入br.common.v1,则必须是类路径中的路径。
  • 谢谢你。我将源目录和测试源目录都设置为 /src。现在它构建了一切。但是 Maven 似乎没有运行我的测试。目标目录现在只包含一个 test-classes 目录,但没有我可以导入到 Jenkins 的 JUnit 测试报告。
  • 没关系!问题似乎只是错误的源目录深度。 gplus:testCompile 本身并不运行测试。我现在正在运行“mvn clean gplus:testCompile test”并且所有测试都正确运行!再次感谢您的快速帮助!

标签: maven groovy junit jenkins-pipeline shared-libraries


【解决方案1】:

为 maven 配置的源目录是特定的(它们指向文件所在的位置)。如果要导入 br.common.v1,请确保目录层次结构 br/common/v1 在源根目录内。

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多