Issue
I have two Java projects, "A" and "B", and B has a Maven dependency on A:
<dependency>
<!-- Back end stuff -->
<groupId>com.myapp</groupId>
<artifactId>ProjectA</artifactId>
<version>1.0.0</version>
</dependency>
The two projects sit side by side on my workstation in a common parent folder:
/Myproject
/ProjectA
/ProjectB
I'd like to use Project A's unit test context, "test-context.xml", for all my unit testing in Project B as well. Is there a way to directly reference an external context for testing? These are Maven projects using Surefire and Junit for testing, but I'm afraid Surefire and Junit are not my strong points. I'm pretty sure that there's a way to do this, but I don't know where to look for the answers - Spring, Junit, Maven, Surefire... ?
My project "A" unit test classes are configured like this:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:test-context.xml"})
And the file "test-context.xml" is in Project A at /src/test/resources/test-context.xml. Ideally I would simply configure my Project "B" unit test classes like this:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"ProjectA-reference:test-context.xml"})
However, I don't know how to configure the ContextConfiguration element to point to the other project. Has anyone done this before?
Solution
In the pom of ProjectA, do this to produce a test-jar dependency:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, in ProjectB's pom.xml, do this:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ProjectA</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
And finally, in your test class of ProjectB, you should be able to reference any xml files from src/test/resources in ProjectA using the classpath approach you were trying above. Let's say your file is called projectA-test-context.xml
and resides in /src/test/resources/META-INF/spring.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/spring/projectA-test-context.xml")
Answered By - MattSenter
Answer Checked By - Clifford M. (JavaFixing Volunteer)