TechnoBuzz

A Techno Blog, mainly about Java

mock objects

I am again looking at mock objects
Mock Objects [pdf]

There are choices like mockobjects , easymock, jmock.

org.easymock.MockControl

Version 1.2 Docs w API

[Examples ]

mvn clover:clover

Here is an easymock like example:

public class MyClass extends
{
    private FirstObject       theObject;

    public  SecondObject createWorkItem(String name)  {
        SecondObject data = getTheObject().createObject(MyConstants.FIRSTVALUE);
        data.setPropertyValue(MyConstants.NAME, name);
        return data;
   }
   public FirstObject getTheObject() {
        return theObject;
    }
   
    public void setTheObject(FirstObject theObject) {
        this.theObject = theObject;
    }
}
———————————–

public class ExampleTest extends TestCase {

    private MyClass classUnderTest;

    private MockControl controlFirstObject;
    private FirstObject mockFirstObject;

   private MockControl controlSecondObject;
   private FirstObject mockSecondObject;
    setup
    —–
    classUnderTest = new MyClass();

    controlFirstObject = MockClassControl.createControl(FirstObject.class);
    mockFirstObject = (FirstObject) controlFirstObject.getMock();

   controlSecondObject = MockClassControl.createControl(SecondObject.class);
   mockSecondObject = (SecondObject) controlSecondObject.getMock();

    testCreateWorkItem
    ——————
    classUnderTest.setTheObject(mockFirstObject);
    controlFirstObject.expectAndReturn(mockFirstObject
                .createWorkItem(MyConstants.FIRSTVALUE),
                mockSecondObject);
   
    mockSecondObject.setPropertyValue(MyConstants.NAME, "name");

    controlFirstObject.replay();
    controlSecondObject.replay();
       
       
    controlFirstObject.verify();
    controlSecondObject.verify();
   

Advertisements

May 1, 2006 - Posted by | SW Tools

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: