partial mocking with PowerMockito?

805 views
Skip to first unread message

AM

unread,
Apr 8, 2009, 4:13:54 PM4/8/09
to PowerMock
The release notes for 1.2 mentions that partial mocking with
PowerMockito is supported. Is it? Can someone give me an example? I
can't get it to work.

Thanks!

Jan Kronquist

unread,
Apr 9, 2009, 2:26:24 AM4/9/09
to powe...@googlegroups.com

AM

unread,
Apr 9, 2009, 6:14:17 PM4/9/09
to PowerMock
I can't get it to work, any ideas? Thanks.

class UnderTest
{
String test()
{
String s = getString();
System.out.println(s);
return s;
}

String getString() { return "REAL VALUE"; }
}



import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import org.powermock.modules.junit4.*;
import org.powermock.core.classloader.annotations.*;
import org.powermock.api.mockito.powermocklistener.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(UnderTest.class)
@PowerMockListener(AnnotationEnabler.class)
public class Tester
{
@org.powermock.core.classloader.annotations.Mock({"getString"})
private UnderTest underTest;

@Test
public void partialMockitoMock() throws Exception
{
when(underTest.getString()).thenReturn("TEST VALUE");

assertEquals("TEST VALUE", underTest.test());

verify(underTest).getString();
}
}


Getting this exception:

org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be a method call on a mock.
For example:
when(mock.getArticles()).thenReturn(articles);
at com.netledger.core.reporting.components.Tester.partialMockitoMock
(Tester.java:23)


On Apr 8, 11:26 pm, Jan Kronquist <jan.kronqu...@gmail.com> wrote:
> PowerMock's @Mock annotation allows partial mocking. Take a look here:
>
> http://code.google.com/p/powermock/source/browse/tags/powermock-1.2/m...
>
> Please let me know if you have any problems!
>
> /Jan
>

szczepiq

unread,
Apr 10, 2009, 6:29:01 AM4/10/09
to powe...@googlegroups.com
Hi,

I can say some hints from Mockito perspective. It seems underTest is
not a valid Mockito mock. Mockito is protesting at the moment you call
thenReturn() that there is no method 'recorded' for stubbing.

Cheers,
Szczepan Faber

Johan Haleby

unread,
Apr 10, 2009, 8:32:33 AM4/10/09
to powe...@googlegroups.com
Hi,

I will take a look at this asap and see if I can get it to work (I'm on vacation though).

/Johan

Johan Haleby

unread,
Apr 11, 2009, 8:15:54 AM4/11/09
to powe...@googlegroups.com
Ok I've looked at the problem and managed to verify it. The strange thing is that if you change the modifier of the "test" method to final then it works. I've looked at a solution briefly but I currently don't have one. But it's most likely a bug (if I haven't missed anything obvious) and I'll add a new issue to our issue tracker. Remember that the Mockito extension is still in its infancy and I've never used it in real projects myself. If you're interested please feel free to help us out by improving it (just testing it as you do right now is also helpful though).

Btw, you shouldn't necessarily need to use the Mock annotation to do partial mocking with the Mockito API. You can use PowerMocktio.mock(), e.g. mock(MyClass.class, Whitebox.getMethods(MyClass.class, "methodToMock"));. The syntax will most likely improve in the future (to something like mockPartial(MyClass.class, "methodName1", "methodName2")).

/Johan

Alex "Sasha" Murkes

unread,
Apr 11, 2009, 6:26:35 PM4/11/09
to powe...@googlegroups.com
Thanks, Johan.  No rush, enjoy your vacation.

Johan Haleby

unread,
Apr 12, 2009, 6:44:55 AM4/12/09
to powe...@googlegroups.com
I've been on vacation for 2,5 months now so it's alright, but soon it's time to get back home to reality :)

Anyways, I've made no progress on this issue yet but I added mockPartial and mockStaticPartial to the PowerMockito API and I've managed to upgrade to Mockito 1.7. It's available in the trunk. We really need to look into why the partial mocking stuff doesn't work when invoking non-final methods though. I'm quite sure it has something to do with the byte-code manipulation, but we need to look at it more closely.

Thanks for your patience,
/Johan

Alex "Sasha" Murkes

unread,
Apr 12, 2009, 9:50:37 AM4/12/09
to powe...@googlegroups.com
FYI, I've managed to add partial mocking to Mockito itself, in true Mockito style.

when(mock.someMethod()).thenCallRealMethod();

or if the method is void:

doCallRealMethod().when(mock).someMethod();

i'll try to submit this to the Mockito project soon.

An advantage of this style over passing method names as strings is that refactoring (changing method name or signature) is less likely to break tests.

Johan Haleby

unread,
Apr 13, 2009, 6:59:44 AM4/13/09
to powe...@googlegroups.com
Avoiding strings is always a good thing. We also have some ideas to implement invocation and settings up expectations for private methods as well as partial mocking etc by not having to specify the method name. Kinda like the fest reflect project. Would be quite cool I think.

Glad you found a way around the problem anyways!
/Johan
Reply all
Reply to author
Forward
0 new messages