Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference unit test classes of a maven dependency in my java project? [duplicate]

I need to reference some JUnit Tests (src/test/java) from project B in the test package src/test/java of project A whereas B is a maven dependecy of A.

Is this even possible?

<dependency>
    <groupId>XYZ</groupId>
    <artifactId>B</artifactId>
    <version>${project.version}</version>
    <type>jar</type>
    <scope>test</scope>
</dependency> 

Both projects are under my controll.

Thanks for your advice

like image 926
Mahatma_Fatal_Error Avatar asked Apr 15 '15 15:04

Mahatma_Fatal_Error


People also ask

Which of the following folder will have the test classes in a Maven Java project?

Test reports are available in consumerBanking\target\surefire-reports folder. Maven compiles the source code file(s) and then tests the source code file(s).

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>


1 Answers

Your pom in project B needs to include this plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Then, you can access it from project A like this:

<dependency>
    <groupId>XYZ</groupId>
    <artifactId>B</artifactId>
    <version>${project.version}</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency> 

Changing 'type' to test-jar allows you to access test classes from that dependency.

like image 163
Dave Avatar answered Nov 14 '22 23:11

Dave