【发布时间】:2022-01-03 16:31:39
【问题描述】:
我写了一个测试,我从包com/intel/epgsw/JunitAdapter.groovy导入一个类
当我尝试运行测试时,我收到此错误:
[ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.5:testCompile (groovy) on project jenkinsfile-test-shared-library: Error occurred while calling a method on a Groovy class from classpath. InvocationTargetException: startup failed:
[ERROR] src/test/groovy/CloneTestSpec.groovy: 3: unable to resolve class src.com.intel.epgsw.JunitAdapter
[ERROR] @ line 3, column 1.
[ERROR] import com.intel.epgsw.JunitAdapter
[ERROR] ^
测试文件存在于:src/test/groovy
需要导入的类:com/intel/epgsw/JunitAdapter.groovy
我的测试文件是 CloneTestSpec.groovy
这是树:
src
│ ├── com
│ │ └── intel
│ │ ├── epgsw
│ │ │ ├── TestResultEnum.groovy
│ │ │ ├── **JunitAdapter.groovy**
│ │ │
│ └── test
│ ├── com
│ │ └── intel
│ │ └── epgsw
│ ├── epgsw
│ │ └── FooBar98.groovy
│ ├── groovy
│ │ ├── **CloneTestSpec.groovy**
这是我的 pom.xml 的一部分
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<configuration>
<testSourceDirectory>src/test/groovy</testSourceDirectory>
<includes>
<include>**/*Spec.java</include>
<include>**/*Spec.class</include>
<include>**/*Spec</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${groovy.gmaven.pluginVersion}</version>
<executions>
<execution>
<id>groovy</id>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>
<directory>src</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</sources>
<testSources>
<testSource>
<directory>src/test/groovy/com/intel/epgsw</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
</testSources>
</configuration>
</plugin>
</plugins>
【问题讨论】:
-
我已经对您的previous question 发表了评论,试图让您了解为什么需要MCVE 才能准确回答您的问题。因此,请阅读我链接到的文章并编辑您的问题,提供一个完整的、最小的示例项目来重现该问题,无论是内联的还是在 GitHub 上(首选)。也请给你的问题更多的爱,正确地格式化它们(我只是为你做的)。谢谢你。我知道你是新来的,没问题。但请尝试改进。
-
你确定包名是
src.com.intel.epgsw.JunitAdapter而不是简单的com.intel.epgsw.JunitAdapter? -
是的,包是 com.intel.epgsw 我的意思是指定问题时的目录。
-
但是目录应该是
src/test/groovy,就像你说的那样。所以这里有不一致的地方。unable to resolve class src.com.intel.epgsw.JunitAdapter表示导入语句或您的目录结构错误,包含多余的src。这就是为什么我需要查看 MCVE,如果您无法自行解决。 -
我尝试使用 com.intel.epgsw.JunitAdapter 导入。仍然遇到同样的问题。为了确保一切正常,我尝试将类 JunitAdapter 复制到我的测试 CloneTestSpec.groovy,然后它工作正常。我已经更新了我的问题,添加了我的项目的树结构。我还应该添加什么?
标签: jenkins groovy pom.xml spock