ClassNotFoundException When Executing JUnit Test Runner In Eclipse With M2Eclipse
This would be fine, except that the JUnit plugin simple tells Eclipse to build, which invokes the Maven builder (which is called by m2eclipse plugin of eclipse) to build, which only goes through Maven’s compile phase – meaning it only compiles regular source files – not test files.
This is kind of a miss by the folks who wrote the m2eclipse plugin as they should have added an additional builder for Eclipse that would kick off the compiler:testCompile goal which does compile the test files – but alas, it’s not my plugin.
Anyway, for the JUnit runner to work correctly in Eclipse we need to add an additional builder to each project that we want to run tests. This builder will invoke maven in a separate step of Eclipse’s build process to compile the test classes before JUnit executes.
Follow these steps:
- Select he project to configure and alt+enter to open its properties.
- Select Builders
- Click New to bring up the configuration type dialog.
- Select Program and then click Ok
This brings us to the launch configuration properties dialog which we need to fill out to create the launch configuration.
- Enter Maven Test Compiler for the Name property
- Location refers to the executable that you’d like to run. This needs to point to your mvn.bat file
- In Working Directory, click on the Variables… button and select project_loc and then click Ok
- Under Arguments enter – wthout the qutoes – “compiler:testCompile”
- Go to the Build Options tab, and make sure that the check boxes under Run the builder are selected for all except During a “Clean” and then click Ok
This should take you back to the project’s properties. You can now select Ok and happily enjoy running the eclipse JUnit test runner!