how to to unit test a maven2 mojo plugin that validates files
I have created a maven2 Mojo that inspects certain file types for instances of certain strings. It is designed to be used in the test phase to report whether these files are vaild or not.
When it finds these undesired strings it outputs build failure alerts and fails the maven build using MojoFailureException.
I would like to do some integration testing with this Mojo everytime I compile/install it. Ideally I imagine a scenario where I have several files with the strings that should fail the build and then test the maven2 output to assert that those files are failing开发者_运维知识库. What is the best way to go about doing this kind of testing in a maven2 mojo?
Thanks
I would try to separate the validation logic from the file I/O and then test the logic in unit tests with mocked input (ideally from Strings, not files). The bulk of the small-scale testing can be done in real unit tests this way.
On top of that, there should be integration tests as well, where the whole mojo is tested with real files, like:
MyMojo mojo = new MyMojo();
// set up input files in test directory
// set up mojo properties if needed
mojo.execute();
// assert results
精彩评论