News from Feb 15, 2008

  2008/02/15
Power of EasyMock 2 with Java 1.4
Last changed: Feb 15, 2008 09:50 by Ürgo Ringo
Labels: easymock, testing

I have been long time wondering if it is possible to improve API of EasyMock Java 1.3 version so that you get rid of MockControls in your test code. So as an experiment I wrote following classes to check how much work does this require. I wanted to achieve same simple API as in EasyMock 2.x but still be Java 1.4 compatible. Also I didn't want to rewrite EasyMock or extend it in any fragile way.

Here are the resulting classes that currently support only returning booleans but with some simple coding it should be possible to make it support whole API of EasyMock:

public class EasyMockHelper {

  private static MockControl lastInvokedControl;

  public static ReturnValueSetter expect(boolean b) {
    return new ReturnValueSetter(lastInvokedControl);
  }

  public static Object getMock(Class cl) {
    Object proxy = Proxy.newProxyInstance(cl.getClassLoader(), new Class[]{cl},
            new MockInvocationHandler(MockControl.createControl(cl)));
    return proxy;
  }

  private static class MockInvocationHandler implements InvocationHandler {

    private final MockControl control;

    public MockInvocationHandler(MockControl control) {
      this.control = control;
    }

    public MockControl getControl() {
      return control;
    }

    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
      Object retValue;
      try {
        retValue = method.invoke(control.getMock(), args);
      } finally {
        lastInvokedControl = control;       
      }
      return retValue;
    }
  }

  public static void replay(Object mock) {
    getControl(mock).replay();
  }

  private static MockControl getControl(Object mock) {
    MockInvocationHandler invocationHandler = (MockInvocationHandler) Proxy
            .getInvocationHandler(mock);
    return invocationHandler.getControl();
  }

  public static void verify(Object mock) {
    getControl(mock).verify();
  }
}
public class ReturnValueSetter {

  private final MockControl lastInvokedControl;

  public ReturnValueSetter(MockControl lastInvokedControl) {
    this.lastInvokedControl = lastInvokedControl;     
  }
 
  public void andReturn(boolean b) {
    lastInvokedControl.setReturnValue(b);
  }

}

And here is basic test:

public class EasyMockHelperTest extends TestCase {

  private interface Sample {
    boolean getBoolean();
  }

  public void testExpectReturnTrue() throws Exception {
    Sample mock = (Sample) EasyMockHelper.getMock(Sample.class);

    EasyMockHelper.expect(mock.getBoolean()).andReturn(true);
    EasyMockHelper.replay(mock);

    assertTrue(mock.getBoolean());
  }

  public void testExpectReturnFalse() throws Exception {
    Sample mock = (Sample) EasyMockHelper.getMock(Sample.class);

    EasyMockHelper.expect(mock.getBoolean()).andReturn(false);
    EasyMockHelper.replay(mock);

    assertFalse(mock.getBoolean());
  }

  public void testExpectMultipleCalls() throws Exception {
    Sample mock = (Sample) EasyMockHelper.getMock(Sample.class);

    EasyMockHelper.expect(mock.getBoolean()).andReturn(false);
    EasyMockHelper.expect(mock.getBoolean()).andReturn(true);
    EasyMockHelper.replay(mock);

    assertFalse(mock.getBoolean());
    assertTrue(mock.getBoolean());
  }
}

Even if above code contains some bugs and does not support all scenarios I think it proves that it should be quite easy to improve EasyMock Java 1.3 version.

Here is the source

Posted at 15 Feb @ 9:47 AM by Ürgo Ringo | 4 Comments
Test your web site in different browsers
Labels: testing

Test your web design in different browsers

There is free online service available at:
http://browsershots.org/
where you can test your sites presentation under different browsers.  Just submit the URL of your site and wait a bit -> you'll get the result as a set of screenshots soon.

There is another similar and simplier service available here:
http://www.browsrcamp.com/
It might be useful when you are working on Windows OS and want to test your website through the eyes of a Mac-user.

Posted at 15 Feb @ 2:18 PM by Andrei Tkatšov | 2 Comments