Dogukan Sonmez

Currently in Munich, Germany

Java Groovy Python

No-Sql TDD Scrum

Django Shell Cloud

May 09 2012

Easy Mock Without EasyMock

I should tell you first I am not going to show you something that can be replacement of easymock. I am going to show you some nice trick to write test more easy without using easymock. EasyMock has a lot of benefits like isolate objects and decrease dependencies. It is useful to test error conditions or it is useful do some test based on database operation.

For our case think about you are writing a test case for a method which is using a class to get some data and do some jobs and return the result. What you need to do is create an instance of depended class and use it in your test case. But sometimes it's not such an easy. You need to care about more unrelated staff to create an instance of class that you related to. So do not waste time to create and instance object and get some data from it, use esymock like I did below or use object oriented programming like I tried to show at this post

if you are not familiar with easymock here is a nice tutorial to start : Testing with EasyMock

Let's test our case by easymock

Assume you want to write a test for reverseString method of MyObject class. So you create an instance of MyObject and then create a mock by using interface of DependedClass then you set expected return value when you call getData method of DependedClass. Between replay and verify lines call method and assert result then you done.

public class ThisIsTestClass {
    @Test
    public void reverseString() {
        MyObject myObject = new MyObject();
        InterfaceOfDependedClass mockObject = EasyMock.createMock(InterfaceOfDependedClass.class);
        EasyMock.expect(mockObject.getData("My parameter")).andReturn("something").anyTimes();
        EasyMock.replay(mockObject);
        assertEquals("gnihtemos", myObject.reverseString();
        verify(mockObject)
    }
}

public class MyObject {
    public String reverseString() {
        //do something
        String data = dependedClass.getData("My parameter");
        //do something
        //return result
    }
}

public class DependedClass {
    public String getData() {
        //do something with other object
        //return data
    }
}
        

Here is the trick to make it more easier

Instead of using easy mock to create mock instance of depended class do below like I did. Create and anonymous class of DependedClass and override of getData method.

  public class ThisIsTestClass {
    DependedClass anonymousDependedClass = new DependedClass(){
        @Override
        public void getData(){
            return "something";
        }
    }

    @Test
    public void reverseString(){
        MyObject myObject = new MyObject();
        myObject.setDependedClass(anonymousDependedClass)
        assertEquals("gnihtemos",myObject.reverseString();
    }
}
//MyObject and DependedClass
        

You might say this is kinda stubs but you can put some logic inside of override method based on parameter.