Search code examples
jenkinsgroovypom.xmlspock

Unable to resolve class JunitAdapter


I wrote a test where I am importing a class from package com/intel/epgsw/JunitAdapter.groovy

When I try to run the test, I get this error:

[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] ^

Test file is present in: src/test/groovy

Class that needs to be imported: com/intel/epgsw/JunitAdapter.groovy My test file is CloneTestSpec.groovy

Here is the tree :

src
│   ├── com
│   │   └── intel
│   │       ├── epgsw
│   │       │   ├── TestResultEnum.groovy
│   │       │   ├── **JunitAdapter.groovy**
│   │       │   
│   └── test
│       ├── com
│       │   └── intel
│       │       └── epgsw
│       ├── epgsw
│       │   └── FooBar98.groovy
│       ├── groovy
│       │   ├── **CloneTestSpec.groovy**

This is a section of my 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>

Solution

  • Your directory layout is chaotic. Please move all your test classes into the src/test/groovy base folder. There, you create a directory structure according to your package structure. For example (I cannot say for sure without seeing the package declarations in each file), assuming that

    • all application classes are Java classes,
    • all test classes are Groovy classes and
    • all classes are in the exact same package 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
    

    If any of my above assumptions are false, you need to adjust the directory structure accordingly. But your chaotic directory layout with 3 different base directories causes the trouble you are facing. Please do learn some Java, Groovy and Maven basics, especially the Maven standard directory layout.