G_H
G_H

Reputation: 11999

Unit tests for a service-provider interface in Maven

Recently our dev team has moved from Ant build files to Maven for our project management and build cycle. So far I'm really happy with the results. Since I'm still getting to grips with Maven, I don't have an answer ready for unusual build requirements.

Now I've created a new project and decided to started to immediately go for a proper Maven setup. The project is really an API that uses the Java Service Provider Interface (SPI) to look up pluggable implementations. The implementations can register themselves in the regular META-INF/services/factoryname manner. The API does the bulk of the work, but an implementation is needed to get anything actually useful.

My question is now: how do I go about writing unit tests for this? Two methods came to mind:

  1. Create a simple implementation in the test packages, have that built and then use this for the unit testing. This doesn't seem to fit weel into the Maven project lifecycle.

  2. Create a separate project containing a test implementation. Although this means another entry in our SVN repository, it feels like the cleaner approach. The project could be built and used as a jar in the testing cycle of the main API project. It could be listed as a dependency artifact with test scope in the main project.

In both cases there's a fundamental issue... The implementation would require the main API to compile. After all, it must have a factory class that implements the abstract factory of the API. But in order to properly build the main API, test phase and all, it would need the test implementation. It's really a matter of cyclic dependencies.

What is a good way to deal with this in Maven? In Ant I would just build the implementation in between the API and its tests. If it helps to know, I'm slightly abusing the SPI to locate bundles of object models rather than a singular implementation. It's less like JAXP and more like a way of registering resources.

Upvotes: 2

Views: 1281

Answers (1)

Chris Nava
Chris Nava

Reputation: 6802

You can use the maven-jar-plugin to generate the test-jar as part of your normal build process. This way you generate both artifacts in one step.

Upvotes: 2

Related Questions