atom feed2 messages in net.java.dev.jmockit.usersRe: 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 (robi@gmail.com)
Date:Jun 19, 2009 4:39:01 am
List:net.java.dev.jmockit.users

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

*java.lang.UnsupportedOperationException: class redefinition failed: attempted to change method modifiers* * **at com.xyz.jmockit.TestJMockit$1.<init>(TestJMockit.java:41)* * **at com.xyz.jmockit.TestJMockit.testJMockit(TestJMockit.java:41)* at java.lang.reflect.Method.invoke(Method.java:585) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

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:

package com.xyz.jmockit;

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

import java.net.InetAddress;

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("10.10.10.10"); } }; InetAddress inetAddr = InetAddress.getByName("sharo15-xp"); assertNotNull(inetAddr); assertEquals("10.10.10.10", inetAddr.getHostName()); }

}

What could I be missing here?

Thanks, -Robin

On Thu, Jun 18, 2009 at 7:54 PM, Rogerio Liesenfeld <java@yahoo.com>wrote:

Hi Robin,

Thanks, I am glad to hear that!

You can also send messages to the users mailing list ( use@jmockit.dev.java.net), 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)); } }

jmoc@jmockit.dev.java.ne use@jmockit.dev.java.net --- On *Thu, 6/18/09, Robin Sharma <robi@gmail.com>* wrote:

From: Robin Sharma <robi@gmail.com> Subject: How to mock System classes like InetAddress using JMockit? To: java@yahoo.com 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 java.net.

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