开发者

What are good patterns or supporting fixtures to write unit tests for Maven plugins?

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?


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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜