Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: How to test two modules with the same tests in a third module

Imagine a maven project with 3 modules, an interface module and two different implementations of this interface in a module each. I could test each implementation in its own module, but the test cases are basically the same, because of the same interface.

Is there a way to collect the test cases in a fourth maven module and test the two implementations with this module?

parent
|-interface
|-impl a
|-impl b
|-tests

So that if I build impl a, maven knows to run the tests from the tests module against the impl a build.

Thank you.

like image 841
Juri Glass Avatar asked Feb 27 '23 12:02

Juri Glass


2 Answers

Regarding Java:

What about creating an abstract test in the tests module, where you will write the main tests cases. This test will use the interface defined in the first module in the test.

Then, in each module impl a and impl b, you create a test that extends the abstract test and call the test methods defined in the tests module.

Regarding Maven :

In the tests module, you will have to indicate to Maven that he has to create a jar of the tests package:

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

Then, in each implementing module, you will have to indicate that you have a dependency to this test-jar library:

    <dependency>
        <groupId>foo</groupId>
        <artifactId>bar</artifactId>
        <version>${pom.version}</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>
like image 125
Romain Linsolas Avatar answered Apr 26 '23 17:04

Romain Linsolas


Yes, we have done this for a similar scenario. You simply create two poms in your test module, one for each implementation. Assuming that you have a maven pom for building everything in the parent module, it would now include modules like

<module>../interface</module>
<module>../impl_a</module>
<module>../impl_b</module>
<module>../tests/impl_a</module>
<module>../tests/impl_b</module>

Where each test pom includes the dependency for the appropriate implementation. All dependencies are of course scoped to test.

Now running mvn against parent/pom.xml will build all your projects and run the same tests twice, with the appropriate dependencies. The tests project will then create a target directory under each impl_? subdirectory.

like image 26
Robin Avatar answered Apr 26 '23 17:04

Robin