无法解析 class JunitAdapter

Unable to resolve class JunitAdapter

我写了一个测试,我从包 com/intel/epgsw/JunitAdapter.groovy

中导入 class

当我尝试 运行 测试时,出现此错误:

[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

需要导入的

Class: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>

你的目录布局很乱。请将所有测试 类 移动到 src/test/groovy 基本文件夹中。在那里,您根据包结构创建目录结构。例如(如果没有看到每个文件中的包声明,我不能肯定地说),假设

  • 所有申请 类 都是 Java 类,
  • 所有测试类都是Groovy类和
  • 所有 类 都在完全相同的包中 com.intel.epgsw:
src
  main
    java
      com
        intel
          epgsw
            ApplicationClassA.java
            ApplicationClassB.java
  test
    groovy
      com
        intel
          epgsw
            TestResultEnum.groovy
            JunitAdapter.groovy
            FooBar98.groovy
            CloneTestSpec.groovy

如果我的上述假设有任何错误,您需要相应地调整目录结构。但是您的 3 个不同基本目录的混乱目录布局会导致您面临的麻烦。请学习一些 Java、Groovy 和 Maven 基础知识,尤其是 Maven standard directory layout.