views:

26

answers:

2

I'm developing a Maven plugin, and I realise I'm not sure how to write unit tests for it since I don't know how to exercise the functionality of the plugin except by just running Maven. How have other Maven plugin developers managed testing of their code?

A: 

You should try separating as much as possible of the core functionality to use mockable interfaces rather than the Maven API directly. This way you can unit test that functionality separately from the environment.

E.g. if you read data from a file during execution, you should inject a Reader into your class rather than having it open the file itself. Then a separate class can initialize the input stream in real life, but in unit tests you can supply a StringReader preloaded with the desired string.

Of course you also need integration tests - these can be run simplest by calling execute() on your Mojo class.

Péter Török
Thanks Péter. My plugin has a large number of parameters, so parameter injection is one thing I want to have covered by the unit tests. Likewise, changing the source and output directories (my plugin attaches to the generate-sources phase) is something that should be tested. These are the kinds of things I'm unsure about.Totally agree that core functionality should be abstracted away from the maven API where possible and tested separately.
Ian Dickinson
+1  A: 

You also need acceptance (or end-to-end) tests. The maven-invoker-plugin will help a lot in such kind of tests.

Andriy Plokhotnyuk