Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Archive for the ‘testing’ Category

Migrating JUnit + JMock tests to TestNG

Posted by Piotr Gabryanczyk on March 17, 2007

Following QCon presentation of Alexandru Popescu and Cedric Beust on TestNG and TDD I started switching IntellJ Eclipse Dependency Sync plugin to use TestNG.
It was very simple with TestNG IntelliJ plugin which has “Convert JUnit test to TestNG” intention.

JMock integration
One thing I struggled with was JMock integration. Although EasyMock seems to be very popular, many people prefer JMock as it provides more “fluent” interface. I think I am it this camp 🙂

The integration of JMock and TestNG turned out to be very simple:
– test class needs to extend MockObjectTestCase
– make sure that setUp() and tearDown() methods in the super class are called. So you need to override them in your test class and annotate with @BeforeMethod, @AfterMethod.

Here is the example:

  1 public class DependencySynchronizerTest extends JDummyTestCase {
  3     @BeforeMethod public void setUp() throws Exception {
  4         super.setUp();
  5         ...
  6     }
  8     @AfterMethod protected void tearDown() throws Exception {
  9         super.tearDown();
 10     }
 12     @Test public void testTraceChangesUserApprovesLibraryName() {
 13         expectFullRegistrationToBeDone();
 14         dependencySynchronizer.traceChanges(file);
 15     }
 16     ...
 17 }

Posted in intellij, java, testing | 4 Comments »

Literate testing – extending JMock

Posted by Piotr Gabryanczyk on December 6, 2006

I have come across the following post today: http://weblogs.java.net/blog/tomwhite/archive/2006/05/literate_progra_1.html

I think it is a great idea to use natural language to simplify unit testing. I even wrote couple of similar extensions to JMock lately. I will share them soon 🙂

JMock vs EasyMock

Many developers choose EasyMock over JMock. The main argument is “JMock tests are not easy to refactor because method names are passed as a string”. I am not going to comment on that as many people already proved that good IDE can handle it easily.

What people do seem to forget is:

  • JMock uses literate API,
  • JMock tests are much more consistent comparing to EasyMock (you can clearly see what the expectations are)
  • In JMock you can easily differentiate between expectations and stubs
  • In JMock you can easily show which parameters are insignificant using ANY-like
  • EasyMock produces very ugly code when expected methods declare exceptions
  • EasyMock produces ugly code when expected method doesn’t return the value

Probably there is more…

Let me know what you think!

Posted in java, testing | Leave a Comment »