MCG - Mock Call Generator

This site is all about a code generator plugin that helps and speeds up the creation of JUnit tests that rely on mocking.

Background

In one of my projects in 2012 I came up with the idea (not entirely by myself though) to speed up the creation of JUnit tests by means of a code generator.
This was necessary, because most of the developers at the specific project were very junior in regards to (unit) testing.
So to help them in the creation and simplify mock handling a working code generator was created as an Eclipse Plugin.
Now, in this project, I am open sourcing that technology (re-written from ground and made much more flexible and configurable), which, to be honest, is nothing more then bringing together some of the finest tools from the open source community.
Subjective tests indicate that the cost (time) to create Unit test for a class with MCG can go down to 10% in best case scenarios and down to 50% on average.

Features (current and future releases)

License

Apache License

How it works

MCG works by analysing the executable bytecode of a class.
Then it detects each method call within the methods of the analysed class and decides if that call needs to be mocked or not based on the mocking rules specified in the MCG configuration.
After the analysis is complete, Unit Test code is generated that can be used as a template to specify the expectations and assertions of a test, thus reducing the burden of writing the mock expectations by hand, which most often represents the hindrance for creating mock based unit tests.

Example

Class under Test

...
public Film increaseNumCopies(Long id) {
	Film film = getFilmDao().findById(id);
	if (null!=film) {
		film.setNumCopies(film.getNumCopies()+1);
		getFilmDao().save(film);
	}
	
	return film;
}
...
			

MCG Output

...
@Test
public void testIncreaseNumCopies() throws Exception {
	Long id = 0L;
	Film film = new Film();
	Film film1 = new Film();
	FilmDao mockFilmDao = getMock(FilmDao.class);
	
	EasyMock.expect(mockFilmDao.findById(id)).andReturn(film).anyTimes();
	EasyMock.expect(mockFilmDao.save(film)).andReturn(film1).anyTimes();
	
	// replay mocks
	replayAllMocks();
	
	// call target method
	Film ret = filmService.increaseNumCopies(id);
	
	//TODO: [mock-call-generator] add assertions
}
...
			

Source






(c) Pressenna Sockalingasamy