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

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

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

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

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

Thanks.

+4
source share
3 answers

Regarding Java:

How about creating an abstract test in the testing module, where you will write the basic tests. This test will use the interface defined in the first module in the test.

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

Regarding Maven:

In the tests module, you need to tell Maven that it should create a test package jar :

 <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 need to indicate that you have a dependency on this test-jar library:

  <dependency> <groupId>foo</groupId> <artifactId>bar</artifactId> <version>${pom.version}</version> <type>test-jar</type> <scope>test</scope> </dependency> 
+5
source

Yes, we did it for a similar scenario. You simply create two letters in your test module, one for each implementation. Assuming you have a maven pom to create everything in the parent module, it will 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 a dependency for the corresponding implementation. All dependencies, of course, are covered by testing.

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

+2
source

I did not do this with maven modules, but it should be simple if you implement the ala abstract test case

 public abstract class XyTester { protected abstract YourInterface createImpl(); } 

For junit, it is necessary that the abstract class does not end with "Test", so I choose a tester.

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

+1
source

Source: https://habr.com/ru/post/1302371/


All Articles