testdriven.com Wrangling quality out of chaos

JEasyTest is released

11.18.2007 · Posted in Tools

JEasyTest is an Eclipse plugin created to simplify unit testing of code that is hard to test using standard mock objects frameworks.
JEasyTest uses AspectJ load-time bytecode modification "to inject mock invocations" (constructor and static method invocations) where we could not using standard mock object tools.
JEasyTest has been thought to be integrated with existing mock object frameworks to overcome some of their limitations.
The new release adds Eclipse 3.3 support, JUnit4 support, an Ant task and an improved API.
In JEasyTest the bytecode is modified only at runtime just before test running, without any possibility that production code might contain unwanted testing code. Thanks to the Eclipse plugin nature, JEasyTest usage of AspectJ is completely hidden and it happens behind the scenes while we are writing tests.

Here is a simple example of test written using JEasyTest:

Code:
@ClassUnderTest(MyServiceClient.class)
public class MyServiceClientTest {
@JEasyTest
@org.junit.Test
public void runThrowsIllegalStateExceptionIfInitialContextThrowsNamingException() throws Exception {
on(System.class).expectStaticNonVoidMethod("getProperty").with(arg("objectName")).andReturn("test");
on(InitialContext.class).expectEmptyConstructor().andThrow(new NamingException("message"));

MyServiceClient client = new MyServiceClient();
try {
client.run();
fail(IllegalStateException.class+" should have been thrown");
} catch (IllegalStateException e) {
assertEquals("message", e.getMessage());
}
}
}

where

public class MyServiceClient {
public void run() {
String objectName = System.getProperty("objectName");
if (objectName == null){
throw new IllegalStateException("Object name system property must be defined");
}
try {
InitialContext context = new InitialContext();
Service service = (Service)context.lookup(objectName);
service.doThis();
} catch (NamingException e) {
throw new IllegalStateException(e.getMessage());
}
}
}

  • del.icio.us
  • Digg
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Comments are closed