Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse a Spring test context in another project

Tags:

java

junit

spring

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?

like image 894
user1071914 Avatar asked Oct 21 '13 03:10

user1071914


People also ask

What is spring TestContext?

The Spring TestContext Framework (located in the org. springframework. test. context package) provides generic, annotation-driven unit and integration testing support that is agnostic of the testing framework in use.

What is SpringJUnitConfig?

@SpringJUnitConfig is a composed annotation that combines @ExtendWith(SpringExtension. class) from JUnit Jupiter with @ContextConfiguration from the Spring TestContext Framework. As of Spring Framework 5.3, this annotation will effectively be inherited from an enclosing test class by default.

What is @ContextConfiguration?

@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.

Is spring used for testing?

It ensures performance and quality of the product. The Java platform supports many testing frameworks. Spring introduces the principle of dependency injection on unit testing and has first-class support for integration testing.


1 Answers

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")
like image 121
MattSenter Avatar answered Oct 07 '22 05:10

MattSenter