Show-n-Tell Thursday

Using easymock for disconnected Domino Java testing

One of the projects I'm working on at the moment has a *lot* of code written in a J2EE application which talks to Oracle using JDBC and Domino over CORBA. It actually all works really well but the approach of one of our non-Domino developers was really interesting to me, so I asked if I could write about it up here.

So the approach that Greg takes is test driven development, that is he writes his tests before writing the code. For his testing he would rather not connect to the Domino server all the time, but instead mock up objects to make sure that his other code works correctly. Enter EasyMock, which is a JAR file which you can include in any project that basically pretends to be whatever object you want it to be.

package test;

 

import junit.framework.*;

import java.util.Vector;

import org.easymock.EasyMock;

import org.easymock.*;

import static org.easymock.EasyMock.*;

 

import lotus.domino.*;

import com.agcs.cores.utilities.*;

 

 

public class testDominoFieldProcessing extends TestCase {

 

  Document mock = null;

  DateTime datemock = null;

  DominoFieldProcessing dfield = null;

  public void setUp() throws Exception

  {

 

    mock = createMock(Document.class);

 

    Vector<DateTime> v = new Vector<DateTime>();

    for (int i = 0; i< 3;i++)

    {

      datemock = createMock(DateTime.class);

      expect(datemock.getLocalTime()).andReturn("01:02:03");

      expect(datemock.toJavaDate()).andReturn(new java.util.Date(System.currentTimeMillis()   ));

      v.add(datemock);

      replay(datemock);

    }

 

    expect(mock.getItemValue("time")).andReturn(v);

    expect(mock.hasItem("time")).andReturn(true);

    replay(mock);

 

  }

 

 

  public void testNotesDateToJavaDate()  throws Exception

  {

    for (java.util.Date d:  DominoFieldProcessing.getFieldValueAsListDate(mock,"time"))

      System.out.println(d);

  }

}
Java2html
What this code shows is the use of EasyMock to be a Notes Document and a DateTime Object. You tell it what the methods that you want to call should return, and then, for all intents and purposes, the object is a Domino object but without having to go through the hassle of deploying your code to a server which has the rights to connect to a proper Domino server. Now of course this should only be used early on in the development lifecycle, but for environments where connecting to Domino can be difficult this is a real time saver.