White Box Testing
Test your code
   Home      Mocking-JMock
 
Mock objects are used when dependent component is not yet ready, or too slow and heavy to test.JMock is a library to support Mocks in Java.
  • Supports unit testing with JUnit 3 and above
  • JMock is an open source library
  • Lightweight and easy to integrate

JMock 1

Designed for Java 1.3+
 
Installation steps
  • Download JMock 1(jmock-core and jmock-cglib) from JMock.org and add  jar libraries to classpath
  • Download CGLib 2.1  and add jar libraries to classpath
 
Testing with JMock1 framework is 4 steps process
  • Setting up the Mock (depends) objects
For interfaces:
Unit test extends org.jmock.MockObjectTestCase
private Mock myMock = mock(My.class);
 
For abstract and concrete classes:
Unit test extends org.jmock.cglib.MockObjectTestCase
private Mock myMock = mock(My.class, new Class[] { <param_types> }, new Object[] { <param_values> });  
  • Define expectations
myMock.expects(<how_many_times>)
.method (“methodName”)[.with(<parameter_matchers>)][.will(<action>)][.onConsecutiveCalls(..)]
  • Execute SUT(System Under Test)
  • Verify your expectations

 

Getting started with JMock1

For a simple example we want to test our following Greeting class. In that class we have dependency on GreetingTime class for which implementation is not yet available.

public class Greeting{

        //we have to define GreetingTime as an interface

        private GreetingTime gt;
        public void setGreetingTime(GreetingTime gt){
               this.gt = gt;
        }
        public String SayGreeting(String name){
               String greeting = gt.getGreeting();
               return greeting+","+name;
       }
}
 
To test Greeting class we have to define GreetingTime as an interface which may look like as below.
public interface GreetingTime{
        public String getGreeting();
} 

Now we will write our test class GreetingTest to test Greeting class.

public class GreetingTest extends MockObjectTestCase{} 

 
We first setup and configure mock object in context to test the Greeting. We have defined mock object mockGreetingTime for GreetingTime.We then register mockGreetingTime with Greeting object greet.
                Greetinggreet = new Greeting();
                Mock mockGreetingTime = mock(GreetingTime.class);
                greet.setGreetingTime((GreetingTime)mockGreetingTime.proxy());
 
Now we will define expectations on the mock object mockGreetingTime that specify the method getGreeting that need to be called upon test execution and should return value “Good Night”
                mockGreetingTime.expects(once()).method("getGreeting")
                                        .will(returnValue("Good Night"));
 
Next we will execute the code that we want to test
                Object out = greet.SayGreeting("Mr. Sam");
 
After executing the test case verify the results
                assertEquals("Good Night,Mr. Sam", out); 
 
Here is the complete code of GreetingTest
 
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;
import junit.framework.TestCase;  
public class GreetingTest extends MockObjectTestCase{
               private Mock mockGreetingTime;
               private Greeting greet;
 
               protected void setUp() throws Exception {
                                //set up : configure mock object
                                mockGreetingTime = mock(GreetingTime.class);
                                greet = new Greeting();
                                greet.setGreetingTime((GreetingTime) mockGreetingTime.proxy());
 
                                super.setUp();
               }
 
               public void testSayGreeting(){
                                //Define Expectations
                                mockGreetingTime.expects(once()).method("getGreeting")
                                                        .will(returnValue("Good Night"));
 
                                //Execute SUT(System Under Test)      
                      Object out = greet.SayGreeting("Mr. Sam");                                                               
                                //Verify your expectations
                                assertEquals("Good Night,Mr. Sam", out);
               }
}
 
 
JMock 2
 
Designed for Java 1.5+
 
Installation steps
  • Download JMock 2 from JMock.org and add  jar libraries to classpath
  • Download CGLib 2.1_3  and add jar libraries to classpath
  • Add jmock-legacy-2.4.0, and objenesis-1.0 to classpath
 
Testing with JMock2 framework is 4 steps process
 
  • Setting up the Mock (depends) objects
For interfaces:
private Mockery context = new Mockery();
private My myMock = context.mock(My.class);
 
For abstract and concrete classes:
private Mockery context = new Mockery() {{setImposteriser(ClassImposteriser.INSTANCE); }};
private MyImpl myMock = context.mock(MyImpl.class);
  • Define Expectations
context.checking(new Expectations() {{… define expectations …}});
  • Execute SUT(System Under Test)
  • Verify your expectations
 

Getting started with JMock2

test class GreetingTest will change for JMock2 as below for the same example taken in JMock1

import org.jmock.Mockery;

import org.jmock.Expectations;import junit.framework.TestCase; 

public class GreetingTest extends TestCase {
               private Mockery context;
               private Greeting greet;
               GreetingTimemockGreetingTime;
 
               protected void setUp() throws Exception {
                                // set up : configure mock object
                                context = new Mockery();
                                greet = new Greeting();
                                mockGreetingTime = context.mock(GreetingTime.class);
                                greet.setGreetingTime(mockGreetingTime);
 
                                super.setUp();
                }
 
                public void testSayGreeting(){
                                //Define Expectations
                                 context.checking(new Expectations() {{
                                                oneOf(mockGreetingTime).getGreeting();
                                                will(returnValue("Good Night"));
                                }});
 
                                //Execute SUT(System Under Test)
                                Object out = greet.SayGreeting("Mr. Sam");
 
                                //Verify your expectations
                                assertEquals("Good Night,Mr. Sam", out);
                 }               
}
 

Sample Code

To run sample code run them as JUnit test
 

External References