Issue
I have two Eclipse projects A and B that I use to run Selenium tests with JUnit.
- Project A tests a web app, so it contains tests that are divided into multiple packages each with their own test suite class (one package for each feature).
- Project B tests an application that uses A's web app but adds new features, so it contains its own tests but must also be able to reuse tests from A in its own test suite class when features are reused.
In both projects, my test code is located under src/test/java
, because I need to be able to test both separately.
The problem is that I can't reuse tests from A
in B
because I can't import the test packages from A
. I tried to fix this by placing A
's packages in its src/main/java
folder instead, but that means I can no longer run A
's test suites separately because it no longer has any tests to speak of.
What I want to do is to simply have tests in my Project A, that I can execute, but to be able to reuse those test classes in Project B without duplicating code. How can I do this?
Solution
You can package your test code in its own artifact
<!-- Packaging of test classes in JAR file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
You can then import the dependency where you need it
Answered By - Beppe C
Answer Checked By - Terry (JavaFixing Volunteer)