Automated tests with Eclipse using MoreUnit

Easily manage automated tests in Eclipse with MoreUnit.

Eclipse has some shortcomings in it’s support for managing automated tests. For example, creating test class in the right place, or jumping between the production and the test code isn’t that easy as it should be. Features like these are essential if you regularly write test cases, but it’s a must have if you would like to do TDD, as unnecessary obstacles can easily disrupt the flow of development.

Luckily, there are many plug-ins created by the community to extend the functionality of vanilla Eclipse. MoreUnit does a nice job improving testing experience.

Creating tests with ease

The first thing that I bumped into with Eclipse is that creating a test case for a source class can be painful in some cases. For example in a Maven project there is an option to create a new JUnit Test Case, but by default it creates the test right beside the source file in the src/main directory. That has to be changed manually every time.

MoreUnit is aware of the Maven project structure and by default with Ctrl+j it creates the test case in the right directory. If the test file exists, it just jumps to that file with this command. Moreover, if a method is selected, it jumps to the related test method directly, or gives a list of choices if there are more possibilities. (It works when initiated from the test methods too.)

Creating a test class

After selecting a source method, one can easily create a test case for it with Ctrl+u. If there is none, it jumps to the test file and creates a test case, with a name generated by the method’s name.

Creating a test case for a method

Finishing the test case, press Ctrl+u again on the test method to create a new test case quickly.

Creating a new test case

As a bonus, the plug-in keeps the names used in the production and the test code synchronized, so you don’t have to manually rename the test classes or methods manually after refactoring.

Launching tests

Another really neat feature of this plug-in that it makes possible to run tests related to a source file quickly. Pressing Ctrl+r on the source or the test file both results in running the appropriate tests. It’s also capable of running a subset of the test methods, just the ones that are related to the selected method. I am not a big fan of it, it’s usually much easier to rerun the whole test class without thinking, but it can come handy if there are many slow and unrelated test methods for a class.

It’s a vanilla Eclipse feature, but I’d like to mention here that I usually bind Rerun JUnit Test to Shift+Alt+x r. This is really useful when working with integration tests that have a larger scope, as with this setup I can run the last test regardless what file I am editing to check the results.

Options provided by MoreUnit


With this setup it’s easy to create and run the appropriate tests without continuously browsing the project hierarchy. I think Eclipse should have these features by default. Hopefully one day it will happen, but until then there are plug-ins to do the job. MoreUnit is a mature, stable tool that does these kind of things very well.

17 September 2015

Interesting article?

Get hand-crafted emails on new content!