Piotr Gabryanczyk’s Blog

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

  • Blogroll

    • I have joined Anti-IF Campaign

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 }

4 Responses to “Migrating JUnit + JMock tests to TestNG”

  1. Thanks for blogging about QCon! I just wanted to let you know that we quoted and linked from this entry on the over all QCon 2007 blogger’s key takeaway points and lessons learned article: http://www.infoq.com/articles/qcon-2007-bloggers-summary

    Feel free to link to it and of course blogging about this articles existence would help even more people learn from your and other bloggers takeaways.

    Thanks again!


  2. I have posted a long time ago another possible approach: JMock for TestNG (or JUnit-free JMock) and its continuation More on JMock and TestNG. They are quite different approaches, but each of them is having its pros and cons. So, people may decide which style fits them better.



    .w( the_mindstorm )p.
    TestNG co-founder
    EclipseTestNG Creator

  3. piotrga said

    Thx Alex,

    I was about to quote your email 🙂

    Actually I am leaning towards using JMock class you created with slight extension:

    I would add this method to JMock.java:

    public static Mock2 mock(Class mockedType) {
    return (Mock2) mock(mockedType, defaultMockNameForType(mockedType));

    And this class:

    public class Mock2 extends Mock {
    public Mock2(Class mockedType) {
    public T proxy() {
    return (T) super.proxy();

    Now when you call proxy() you get typed result.

  4. piotrga said

    public static <T> Mock2<T> mock(Class<T> mockedType) {
    return (Mock2<T>) mock(mockedType, defaultMockNameForType(mockedType));

    public class Mock2<T> extends Mock {
    public Mock2(Class mockedType) {
    public T proxy() {
    return (T) super.proxy();

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: