PowerMockito and URL mocks

3,139 views
Skip to first unread message

Marcel Overdijk

unread,
Sep 1, 2010, 9:59:37 AM9/1/10
to PowerMock

import static org.powermock.api.mockito.PowerMockito.mock;

..

URL url = mock(URL.class);
HttpURLConnection connection = mock(HttpURLConnection.class);
when(url.openConnection()).thenReturn(connection);
System.out.println(url.getClass());

>> java.net.URL

But it is not a mock object.
But the when(url.openConnection().. works.

The problem is later on I cannot verify:

verify(url).openConnection();

org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is not a mock!


Am I doing something wrong here?

Johan Haleby

unread,
Sep 2, 2010, 12:25:15 PM9/2/10
to powe...@googlegroups.com
Not sure what you're doing wrong but my guess is that you're preparing the wrong class for test. Since URL is a system class you need to prepare the class calling the URL class, not the URL class itself. Have a look at the test case "mockingURLWorks" here (virtually the same thing you're trying to do).

/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.


Marcel Overdijk

unread,
Sep 3, 2010, 8:23:10 AM9/3/10
to PowerMock
Thanks Johan,

The example uses easymock and I'm using mockito.
I've writter a small test case based on your example with just URL and
URLConnection.

But this also fails. See the code below.


import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.verifyNew;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(HttpClientImpl.class)
public class HttpClientImplTest {

@Test
public void testURL() throws Exception {
URL url = mock(URL.class);
URLConnection urlConnectionMock = mock(URLConnection.class);

System.out.println("url = " + url.getClass());
System.out.println("urlConnectionMock = " +
urlConnectionMock.getClass());

when(url.openConnection()).thenReturn(urlConnectionMock);

assertSame(url.openConnection(), urlConnectionMock);

verify(url).openConnection();
}
}


Gives:

url = class java.net.URL
urlConnectionMock = class $java.net.URLConnection$
$EnhancerByMockitoWithCGLIB$$c892a26b

So from the getClass() return I wonder if the java.net.URL class mock
is working.

And the verify(url).openConnection(); gives:

org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is not a mock!
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
at
com.googlecode.janrain4j.http.HttpClientImplTest.testURL(HttpClientImplTest.java:
114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
322)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
86)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
94)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:
309)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:
112)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:
73)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:
297)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
222)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
161)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
34)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
44)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:
133)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:
112)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:
57)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
46)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)

Johan Haleby

unread,
Sep 6, 2010, 4:11:35 AM9/6/10
to powe...@googlegroups.com
Can you provide a full example because when I try it it works?

/Johan


--

Marcel Overdijk

unread,
Sep 6, 2010, 4:23:33 AM9/6/10
to PowerMock
Hi Johan,

What do you mean with 'full' example? Imo the posted class is a full
example.


On Sep 6, 10:11 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Can you provide a full example because when I try it it works?
>
> /Johan
>
> > powermock+...@googlegroups.com<powermock%2Bunsubscribe@googlegroups .com>
> > .

Marcel Overdijk

unread,
Sep 21, 2010, 8:19:53 AM9/21/10
to PowerMock
Hi Johan,

Any ideas on this one. I'm still stuck on testing URL mocks with
Mockito/Powermock.

On Sep 6, 10:11 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Can you provide a full example because when I try it it works?
>
> /Johan
>
> > org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(Method Roadie.java:
> > 94)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateIm pl
>
> > $PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImp l.java:
> > 309)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateIm pl
>
> > $PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDele gateImpl.java:
> > 112)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateIm pl
>
> > $PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImp l.java:
> > 73)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateIm pl
>
> > $PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44 RunnerDelegateImpl.java:
> > 297)
> >        at
> > org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
> > 84)
> >        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateIm pl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
> > 222)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateIm pl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
> > 161)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateIm pl
> > $1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
> >        at
> > org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
> > 34)
> >        at
> > org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
> > 44)
> >        at
>
> > org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateIm pl.run(PowerMockJUnit44RunnerDelegateImpl.java:
> > 133)
> >        at
>
> > org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImp l.run(JUnit4TestSuiteChunkerImpl.java:
> > 112)
> >        at
>
> > org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRu nner.run(AbstractCommonPowerMockRunner.java:
> > 57)
> >        at
>
> > org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestRe ference.java:
> > 46)
> >        at
> > org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
> > 38)
> >        at
>
> > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestR unner.java:
> > 467)
> >        at
>
> > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestR unner.java:
> > 683)
> >        at
>
> > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner .java:
> > 390)
> >        at
>
> > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunne r.java:
> > 197)
>
> > --
> > 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<powermock%2Bunsubscribe@googlegroups .com>
> > .

Johan Haleby

unread,
Sep 21, 2010, 12:11:02 PM9/21/10
to powe...@googlegroups.com
I need to take a closer look at this. How ever I have too much in the pipe right now so it'll have to to wait :(

/Johan

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

Marcel Overdijk

unread,
Sep 21, 2010, 2:37:00 PM9/21/10
to PowerMock
Hi Johan,

I understand, no problem.

Cheers,
Marcel

On Sep 21, 6:11 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> I need to take a closer look at this. How ever I have too much in the pipe
> right now so it'll have to to wait :(
>
> /Johan
>
> On Tue, Sep 21, 2010 at 2:19 PM, Marcel Overdijk
> <marceloverd...@gmail.com>wrote:

Johan Haleby

unread,
Dec 2, 2010, 9:12:06 AM12/2/10
to Alasdair Maclean, powe...@googlegroups.com
It seems like verification of instance methods in final system classes is not supported. I'll add it as an issue.

/Johan

On Thu, Dec 2, 2010 at 3:08 PM, Johan Haleby <johan....@gmail.com> wrote:
Your example with File works out of the box when I try it. How ever the Formatter example doesn't seem to work for some reason. Have to investigate why. Really strange since mocking instance methods in URL (which is also a final system class) works. 

/Johan


On Thu, Dec 2, 2010 at 2:29 PM, Alasdair Maclean <a...@alasdairmaclean.com> wrote:
Hi Johan,

I'm afraid id doesn't :-(

If it helps, it fails the isMock() check in verify() because the CGLIB$SET_THREAD_CALLBACKS method is not present on the mock's class (mock.getClass() returns java.util.Formatter). Do you have an idea of what may be wrong?

For comparison, this test passes and the type of mockFile is java.io.File$$EnhancerByMockitoWithCGLIB$$3021ed44, which does provide that method, allowing verify to progress:

package tests.powermock.mockJREClass;

import java.io.File;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class PowerMockTest {

    @Test
    public void testMockClassWithVerification() throws Exception {
        File mockFile = PowerMockito.mock(File.class);
        mockFile.exists();
        Mockito.verify(mockFile).exists(); // doesn't work - bug?
    }
}


Cheers,
Alasdair


On 2 December 2010 13:13, Johan Haleby <johan....@gmail.com> wrote:
Hm does it work in the second test?


On Thu, Dec 2, 2010 at 2:06 PM, Alasdair Maclean <a...@alasdairmaclean.com> wrote:
Sorry forgot to add that in my first test I don't have a second class (SystemClassUser in your example), I was doing everything within the Test class,

Cheers,
Alasdair


On 2 December 2010 13:04, Alasdair Maclean <a...@alasdairmaclean.com> wrote:
Hi Johan,

thanks for the reply.

I had also tried this (adding the test to the classes to prepare) which gave me the same error:


package tests.powermock.mockJREClass;

import java.util.Formatter;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(value={PowerMockTest.class, Formatter.class})

public class PowerMockTest {

    @Test
    public void testMockClassWithVerification() throws Exception {
        Formatter mockFormatter = PowerMockito.mock(Formatter.class);
        mockFormatter.format("hello");
        Mockito.verify(mockFormatter).format("hello");
    }


}

And a more elaborate example which gave the same error:

DocumentWriter.java:

package tests.powermock.mockJREClass;

import java.io.FileNotFoundException;
import java.util.Formatter;

public class DocumentWriter {

    private Formatter formatter;
   
    public DocumentWriter(Formatter formatter) throws FileNotFoundException {
        this.formatter = formatter;
    }
   
    public void insertText(String text) {
        this.formatter.format(text);
    }
}


PowerMockTest.java:


package tests.powermock.mockJREClass;

import java.util.Formatter;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(value={DocumentWriter.class})

public class PowerMockTest {

    @Test
    public void testMockClassWithVerification() throws Exception {
        Formatter mockFormatter = PowerMockito.mock(Formatter.class);
        new DocumentWriter(mockFormatter).insertText("hello");

        Mockito.verify(mockFormatter).format("hello");
    }
}


Cheers,
Alasdair



On 2 December 2010 12:29, Johan Haleby <johan....@gmail.com> wrote:
Well your code snippet won't work because "Formatter" is a system class. You need to prepare the class calling the system class, see here.

/Johan

On Thu, Dec 2, 2010 at 1:21 PM, Alasdair Maclean <a...@alasdairmaclean.com> wrote:
Hi Johan,

I think I'm seeing the same issue as Marcel here. Did you get a chance
to investigate this at all? I've attached my test code below,

Cheers,
Alasdair

Code:

package tests.powermock.mockJREClass;

import java.util.Formatter;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(value={Formatter.class})
public class PowerMockTest {

       @Test
       public void testMockClassWithVerification() throws Exception {
               Formatter mockFormatter = PowerMockito.mock(Formatter.class);
               mockFormatter.format("hello");
               Mockito.verify(mockFormatter).format("hello");

       }


}


On Sep 21, 4:11 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> I need to take a closer look at this. How ever I have too much in the pipe
> right now so it'll have to to wait :(
>
> /Johan
>
> On Tue, Sep 21, 2010 at 2:19 PM, Marcel Overdijk

> > <powermock%2Bunsubscribe@googlegroups .com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/powermock?hl=en.
>
> > --
> > 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
Reply all
Reply to author
Forward
0 new messages