atom feed2 messages in How to mock System classes like I...
FromSent OnAttachments
Robin SharmaJun 19, 2009 4:39 am 
Rogerio LiesenfeldJun 19, 2009 11:37 am 
Subject:Re: How to mock System classes like InetAddress using JMockit?
From:Robin Sharma (
Date:Jun 19, 2009 4:39:01 am

Rogerio, I tried your suggestion but got the following exception:

*java.lang.UnsupportedOperationException: class redefinition failed: attempted to change method modifiers* * **at$1.<init>(* * **at* at java.lang.reflect.Method.invoke( at at at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( at at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

Also, if I use the *@RunWith(JMockit.class)* annotation on the test class, it fails to compile, saying that *BlockJUnit4ClassRunner* cannot be resolved. I guess this annotation can be used only with JUnit 4.5 or above.

I'm using *JDK 1.5.0_18* and *JUnit 4_4.3.1* and am running this test case from within *Eclipse 3.4 (Ganymede)*. Here's the actual test case:


import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull;


import mockit.Expectations;

import org.junit.After; import org.junit.Before; import org.junit.Test;

/** * @author rs * */ public class TestJMockit {

/** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { }

/** * @throws java.lang.Exception */ @After public void tearDown() throws Exception { } @Test public void testJMockit() throws Exception { System.out.println("TestJMockit.testJMockit()"); new Expectations() { InetAddress ia; { InetAddress.getByName(withAny("")); returns(ia); ia.getHostName(); returns(""); } }; InetAddress inetAddr = InetAddress.getByName("sharo15-xp"); assertNotNull(inetAddr); assertEquals("", inetAddr.getHostName()); }


What could I be missing here?

Thanks, -Robin

On Thu, Jun 18, 2009 at 7:54 PM, Rogerio Liesenfeld <>wrote:

Hi Robin,

Thanks, I am glad to hear that!

You can also send messages to the users mailing list (, and bug reports or feature requests can be entered directly in the Issue Tracker.

The answer to your question is yes! JMockit should be able to mock anything. For the cases you mentioned, I wrote the following test class. I ran it on JDK 1.6, using JUnit 4.6:

@RunWith(JMockit.class) public class SharmaTest { @Test public void mockInetAddress() throws Exception { new Expectations() { InetAddress mockIP;

{ InetAddress.getByName(withAny("")); returns(mockIP); } };

InetAddress ipAddress = InetAddress.getByName("dummy-host-name"); assertNotNull(ipAddress); // this is "mockIP" }

@Test public void mockCalendar() { final Calendar desiredTime = new GregorianCalendar(2099, 5, 20);

new Expectations(desiredTime) // will mock only methods called inside the expectations block { { Calendar.getInstance(); returns(desiredTime); } };

Calendar now = Calendar.getInstance(); assertEquals(2099, now.get(Calendar.YEAR)); assertEquals(5, now.get(Calendar.MONTH)); assertEquals(20, now.get(Calendar.DAY_OF_MONTH)); } } --- On *Thu, 6/18/09, Robin Sharma <>* wrote:

From: Robin Sharma <> Subject: How to mock System classes like InetAddress using JMockit? To: Date: Thursday, June 18, 2009, 12:01 AM

Hi, Rogerio! I have recently started using the JMockit mocking library and it's excellent. Congratulations on that. Sorry that I'm mailing you directly with a question, but I guess there isn't yet a discussion forum for JMockit on

Is it possible to mock JDK classes like InetAddress, Calendar etc, using JMockit? So, for e.g., when calling something like below, I want a mocked InetAddress to be returned:

InetAddress ipAddress = InetAddress.getByName("dummy-host-name");

Thanks, -Robin