0

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>
Subhojoy Dey
  • 3
  • 1
  • 3
  • I commented on your [previous question](https://stackoverflow.com/q/70062088/1082681) already, trying to educate you about why an [MCVE](https://stackoverflow.com/help/mcve) is necessary to precisely answer your question. So please, read the article I linked to and edit your question, providing a full, minimal example project reproducing the problem, either inline or on GitHub (preferred). Please also give your questions more love, formatting them properly (I just did that for you). Thank you. I know you are new here, no problem. But please try and improve. – kriegaex Nov 25 '21 at 11:49
  • Are you sure that the package name is `src.com.intel.epgsw.JunitAdapter` and not simply `com.intel.epgsw.JunitAdapter`? – kriegaex Nov 25 '21 at 11:53
  • Yes the package is com.intel.epgsw I meant the directory while specifying the problem. – Subhojoy Dey Nov 26 '21 at 04:40
  • But the directory should be `src/test/groovy`, like you also said. So there is an inconsistency here. `unable to resolve class src.com.intel.epgsw.JunitAdapter` means that either an import statement or your directory structure are wrong, containing a superfluous `src`. That is why I need to see the MCVE, if you cannot figure it out by yourself. – kriegaex Nov 26 '21 at 04:58
  • I tried importing with com.intel.epgsw.JunitAdapter. Still getting the same problem. Just to make sure everything is working, I tried copying the class JunitAdapter to my test CloneTestSpec.groovy, and then it was working fine. I have updated my question, added the tree structure of my project. What else should I add ? – Subhojoy Dey Nov 26 '21 at 11:15

1 Answers1

0

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • You assumed it correctly. the package was com.intel.epgsw creating a directory like you suggested solved the problem for me. Thanks a lot. – Subhojoy Dey Nov 29 '21 at 06:53