开发者

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

开发者 https://www.devze.com 2022-12-21 01:29 出处:网络
Imagine a maven project with 3 modules, an interface module and two different implementations of this interface in a module each.

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.


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>


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.


I didn't do this with maven modules, but it should be simple if you implement an abstract test case ala

public abstract class XyTester {

  protected abstract YourInterface createImpl();
}

It is necessary for junit to let the abstract class not ending with "Test" so I choose Tester.

See here for the abstract class in real world and here or here for one of its implementations.

0

精彩评论

暂无评论...
验证码 换一张
取 消