No more development, TDD begins with MoreUnit

Do you program or do you already TDD? 🙂
If you prefer to do TDD you will love the eclipse plug-in called MoreUnit. It is as simple as powerful. All it does is executing tests from corresponding productive class and makes it very simple to change between them.
Just press Ctrl-J (like Jump) to jump from test to implementing class and the other way. Or press Ctrl-R (like Run) to run the test regardless if you are in the test or the implementation class.

Configuration

There are just a few things you have to configure. I recommend to remove the prefix for tests because I name test classes like the original name with the suffix test.

Find classes with different names

If you have more than one test class for an implementation you prefer to decide into which test to jump. If you like this enable the option ‘Enable flexible naming of tests’. (At beginning I could not imagin who could have like this but since a short time I use it very intensive)

Mocks

There is a experimental feature to create mocks for dependencies. If you have a class which has a constructor to get some configurations MoreUnit is able to create initializing our testee with a mock. I tried it with mockito as mocking framework. My sample is a simple class which takes an object of type Param to be configured.

public class Feature {
 private final Param param;

 public Feature(final Param param) {
 this.param = param;
 }

 public void create() {
 System.out.println(param);
 }
}
public interface Param {
}

While creating the test with MoreUnit (just press Ctrl-J in the Feature class) it will as us in the second step of creation wizard which dependencies should be mocked.

As you can see I selected the constructor which takes an instance of Param. I don’t select any method to create a test method for it. So it will create a test class like this:

@RunWith(MockitoJUnitRunner.class)
public class FeatureTest {
 @Mock
 private Param param;
 private Feature feature;

 @Before
 public void createFeature() throws Exception {
 feature = new Feature(param);
 }
}

In my opinion this is not bad but, I’d prefere to have the initialisation for mocking within the @Before method and not with the @RunWith. Because sometimes I have other JUnit runners like the Parameterized.class. So I have to remove the @RunWith and add the ‘MockitoAnnotations.initMocks(this);’. So the test will look like this:

public class FeatureTest {
 @Mock
 private Param param;
 private Feature feature;

 @Before
 public void createFeature() throws Exception {
 MockitoAnnotations.initMocks(this);
 feature = new Feature(param);
 }
}

You might say: “If I have to do so much I just can write it on myself”. Nearly right. Imagin you have more than just one parameter. MoreUnit will create all the members with corresponding Annotations. There are some problems with the current version (2.4.2). Creating the mocks in an already existing test will have the result there is a second member for the unit under test and if the annotation @RunWith is already on the class it will be added twice. So it will be very helpful while creating a new test but not very comfortable if the test is existing and contains mocks.

Where to find

http://moreunit.sourceforge.net
Update-Site: http://moreunit.sourceforge.net/update-site
The plugin should also be available in the Eclipse Marketplace. Just search for ‘moreunit’

I think it is a very helpful tool if you will program with TDD. Because it helps you to change between implementation an test very quick. (Yes, much faster than Ctrl-F6, try it 😉 )

About the author

Adrian Elsener

Add comment

By Adrian Elsener

Recent Posts