views:

146

answers:

1

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

A: 

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
Péter Török
That's a good point. The methods are already written separately so I should just be able to Mock up some strings in JUnit to send into each of them and test the response. But how would I do the integration testing with the files to check it was working end to end?
Simon Kenyon Shepard
@Simon You can simply create your mojos and execute() them in JUnit, with the input files set up in the appropriate test input directory. Alternatively you can also create a bunch of full project directories with different file setups, and run maven with your goal on them, then analyze the output with some automated script.
Péter Török
Hi, I am not so familiar with how I would execute the mojos in junit directly. Could you provide a small code example? Thanks
Simon Kenyon Shepard
@Simon Added an example, hope it helps.
Péter Török
TIP: Separate the real work to be done from the Mojo. Use the Maven-2 Mojo only the extract the needed set up through a Maven-2 Plug-in setup. Create a second class, which does the real work, give it one or more execute(...) methods with the required argument(s) (use DbC for argument validation). Then call from the Mojo execute(), that second class, its execute(...) method(s). This way testing the implementation becomes very easy. An additional advantage is, that the implmentation can be easly used from different scenarios (little util application, ant-extentsion, eclipse-plugin, etc)
Verhagen