Posts Tagged ‘JUnit’

JUnit Max just rocks!

February 25th, 2009

JUnit Max is an Eclipse plug’in developped by Kent Beck himself. After a few days testing it on a medium size project every day, I can tell you that it just rocks!
Read more…

Blog , , ,

Automatic mock creation with JUnit & Mockito

February 19th, 2009

Mockito is really a great mocking framework. It is based on a powerful stubbing and verification mecanism. Using it is as simple as this:

 import static org.mockito.Mockito.*;

 List mockedList = mock(List.class);



A handy tool provided by Mockito is a custom JUnit runner that automatically creates mock for fields tagged with @Mock annotation:

public class ArticleManagerTest {
    @Mock private ArticleCalculator calculator;
    @Mock private ArticleDatabase database;
    @Mock private UserProvider userProvider;

    private ArticleManager manager;

In our team, we came up with an even simpler solution. All mock names should start with mock to ease test code readability. So we coded a custom Runner that doesn’t need the @Mock annotation.

public class ArticleManagerTest {
    private ArticleCalculator mockCalculator;
    private ArticleDatabase mockDatabase;
    private UserProvider mockUserProvider;

    private ArticleManager manager;

Here is the runner implementation:

public class AutoMockRunner extends BlockJUnit4ClassRunner {
  public AutoMockRunner(Class< ?> clazz) throws InitializationError {

  protected Object createTest() throws Exception {
    Object clazz = super.createTest();

    for (Class< ?> c = clazz.getClass(); c != Object.class; c = clazz.getSuperclass()) {
      scan(testClass, c);

    return clazz;

  private static void scan(Object testClass, Class< ?> clazz) {
    for (Field field : clazz.getDeclaredFields()) {
      if (field.getName().startsWith("mock")) {
        Object mock = Mockito.mock(field.getType(), field.getName());

        try {
          if (field.isAccessible()) {
            field.set(testClass, mock);
          } else {
            try {
              field.set(testClass, mock);
            } finally {
        } catch (IllegalAccessException e) {

Blog , ,

TestNG, la mort de JUnit annoncée ?

April 13th, 2006

Il est intéressant de noter que beaucoup de projets XP utilisent JUnit. Pourtant aux Valtech Days, Christian Bauer, co-créateur d’Hibernate déclarait « JUnit is dead, use TestNG ». Quels sont donc les avantages de TestNG ? Read more…

Blog , ,