First Experience with EasyMock

I like test driven development. But I don't write tests for all features of my code because it is difficult to setup testing environment. For example it is difficult to set up unit test environment for NetBeans Java Model (API). There are too many dependencies. It is possible to run tests in running NetBeans (IDE mode). But I don't prefer this variant because it takes a lot of execution time. Therefore I didn't write tests for pieces code which uses JMI API.

I found regression in my code which work quite long time. So I designed to create Mock objects for few JMI interfaces. Mock objects is implementation of classes for testing purpose. Simply you write tests against API without its implementation, It's mean that you define a scenario of use case of API's pieces in your test. For example you want to to develop tests for application which uses jdbc connection to access data. To setup database is quite difficult. So you write tests without database. You need to simulate jdbc. In this case you can use already implemented mock objects for jdbc api. Developing JDBC applications test-first is nice article about mocking jdbc. Writing of test is simple when jmock library already exists for the API. But in many cases there is no jmock implementation for API which you are using and the api is not well designed for testing. But you want to write tests at least for the critical code. You will have to write jmock objects yourself. Fortunately you can use library for writing jmock object. I know two libraries for Java languages for this purpose:

I tried to use JMock library in the past. You can mock interface and classes. I don't like two things on JMock. JMock uses strings for mocked method names. I have bad memory and I am alredy got a customed to use code completion. The jmock was dessigned for junit 3.8. The recomended way how to use jmock is to use jmock's class like supperclass for yor test case. You don't need to extends your testcase but the code is not then so nice. Today I used Easy Mock it the first time. It is dessigned for TestNG and JUnit 4. It supports jdk 1.5. You can use static import if you don't want to write the classname in everytime. You can use code completion when you define the behavior of mocked code.

I describe my first unittest for code with JMI usage. Look at the code. The comments is bellow.


import java.util.\*;
import junit.framework.\*;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.junit.\*;
import static org.easymock.EasyMock.\*;

public class ElementTest extends NbTestCase {
....
 public void testParseUnknownTag() {
        Method m = createMock(Method.class);
        expect(m.getJavadocText()).andReturn("@myTag");
        expect(m.getAnnotations()).andReturn(Collections.EMPTY_LIST);

        replay(m);

        Element el = Element.findElement(m);
        assertNotNull(el);
        Tag tag = el.getTag("myTag");
        assertNotNull(tag);
        assertTrue(tag.getTagTemplate().isUnknown());
        assertTrue(m instanceof Method);
    }
... 
}   

In the code is tested parsing javadoc tags from Method element. The test is separed into three parts. In the first part is created jmock object for method element. The method contains javadoc with value "/\*\*@myTag\*/" and no annotations. No more methods is used in tested code so it is not necessary to have more mocking methods. The second part activates the mock object by invoking replay(m) method. The code is tested in the last part.

By EasyMock you can also check count of calls. Let's show example. A method's call is expensive. To be sure that the method is called only one time you can write mock object for the class of method. In the test will be tested if the method is called only one time.

Comments:

Post a Comment:
Comments are closed for this entry.
About

xzajo

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today