Unable to resolve import from (src/test/java) Test classes packages from one subproject to another subproject (src/test/java) test classes

49 views Asked by At

Error: The import XXX cannot be resolved

I'm facing this issue in Java 11 while trying to migrate from java 8.

Parent Project X:
    Project A:
        Project Aa:
            src/test/java:
                MyTestA.java
            pom.xml
        pom.xml
    Project B:
        Project Ba:
            src/test/java:
                MyTestB.java
            pom.xml
        pom.xml
    pom.xml

Here I'm trying to access MyTestA.java from MyTestB.java

What Have I tried:

Remove the <scope> tag. So now in project Ba pom.xml, I declared two dependencies of project A, one with <type>jar</type> and another with <type>test-jar</type>, and both don't have declared.

In project A's and project Aa pom.xml, the execution goal of the "maven-jar-plugin" should specify both "jar" and "test-jar".

Still facing the same issue..

But I'm able to access classes from Project Aa's src/main/java packages

1

There are 1 answers

0
nquincampoix On

Test classes of an artifact are not exposed, only those in src/main are.

In order to achieve what you want, you can add another module to your project (let's say Project C), in which you will put the common code you need in both test classes. This code will be in src/main/java.
Then you can add this module as a test dependency for Project Aa and Project Ba.

Parent Project X:
    Project A:
        Project Aa:
            src/test/java:
                MyTestA.java   <== import MyCommonTestCode
            pom.xml            <== depends Project C (scope : test)
        pom.xml
    Project B:
        Project Ba:
            src/test/java:
                MyTestB.java   <== import MyCommonTestCode
            pom.xml            <== depends Project C (scope : test)
        pom.xml
    Project C:
        src/main/java:
            MyCommonTestCode.java
        pom.xml
    pom.xml