Issue with mocking URL class

110 views
Skip to first unread message

Sibin

unread,
Dec 15, 2009, 12:37:24 AM12/15/09
to PowerMock
When I try to mock URL class with powermock I am getting a NPE. Sample
code I used is

public void testUrl() {
URL url = PowerMock.createMock(URL.class);
EasyMock.expect(url.toString()).andReturn("http://localhost:
8080/abcd").anyTimes();

PowerMock.replay(url);

System.out.println(url.toString());
System.out.println(url);

}

I am getting NPE at second sysout (First sysout is working fine).
PowerMock is trying to call the real toString method in case of second
sysout. Stack trace is given below.

java.lang.NullPointerException
at java.net.URL.toExternalForm(Unknown Source)
at java.net.URL.toString(Unknown Source)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at com.testing.mytest.testUrl(MyURLTest.java:208)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at
org.powermock.modules.junit3.internal.impl.PowerMockJUnit3RunnerDelegateImpl.run
(PowerMockJUnit3RunnerDelegateImpl.java:114)
at
org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.run
(JUnit3TestSuiteChunkerImpl.java:166)
at org.powermock.modules.junit3.PowerMockSuite.run
(PowerMockSuite.java:52)
at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run
(JUnit3TestReference.java:130)
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)


Whats wrong in this. I am using PowerMock 1.3.5 and Junit 3 for
running the test. Please help

Johan Haleby

unread,
Dec 15, 2009, 2:11:45 AM12/15/09
to powe...@googlegroups.com
Hi,

Could you tell me which classes you prepare for test?

/Johan


--

You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.



Sibin

unread,
Dec 15, 2009, 3:32:29 AM12/15/09
to PowerMock
I have tried various combinations
@PrepareTest(MyURLTest.class)
@PrepareTest(URL.class) and
@PrepareTest({URL.class, MyURLTest.class})

Also tried to replay the both the classes with PowerMock (like
PowerMock.replay(URL.class))

One thing I observed from my testing is explicitly calling toString()
method on the mock object is working fine, but when I call
String.valueOf(url) its calling the actual implementation of toString
(). This is not just happening for URL class. I tried with various
final and non-final classes.

Awaiting your replay

Thanks
Sibin
> > org.powermock.modules.junit3.internal.impl.PowerMockJUnit3RunnerDelegateImp­l.run
> > powermock+...@googlegroups.com<powermock%2Bunsubscribe@googlegroups­.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/powermock?hl=en.- Hide quoted text -
>
> - Show quoted text -

Johan Haleby

unread,
Dec 15, 2009, 3:54:23 AM12/15/09
to powe...@googlegroups.com
You should prepare the class calling the URL class for test since URL is a system class (the test is prepared automatically if you prepare anything for test). But that doesn't seem to be the issue here. The issue is that in the case where you explicitly call toString() you've prepared the class calling the toString() method for testing (i.e. the "test"). But when the toString() method is called implicitly it is no longer the test that calls the toString method, it's the static method of java.lang.String.valueOf. I know this is very tricky but that's the work-around you need to do in order to mock methods in system class (which are impossible to byte-code manipulate). In your particular case I'm not sure if there is a nice solution since the toString() method is called from a final system class (String) which cannot be modified.

/Johan

To unsubscribe from this group, send email to powermock+...@googlegroups.com.

Sibin

unread,
Dec 15, 2009, 4:40:59 AM12/15/09
to PowerMock
Thanks Johan

I cannot avoid mocking URL class since the code under test ins opening
a connection to the URL that I want to mock and also have some code
that is calling String.valueOf() indirectly. I dont have permission to
change the source code. Anybody knows a workaround please tell me.

Sibin


On Dec 15, 1:54 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> You should prepare the class *calling* the URL class for test since URL is a
> > > >http://groups.google.com/group/powermock?hl=en.-Hide quoted text -

Johan Haleby

unread,
Dec 15, 2009, 5:57:31 AM12/15/09
to powe...@googlegroups.com
This is a difficult one.. Could you supply some more details on what you're trying to test? To where do you pass the URL?

To unsubscribe from this group, send email to powermock+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages