Capturing arguments from private method (Mockito & PowerMock)

2,280 views
Skip to first unread message

Eyal Ringort

unread,
Sep 8, 2014, 5:00:18 AM9/8/14
to powe...@googlegroups.com
Hi All,

Using Mockito with PowerMock

I have the following scenario (a more complex one but this is a simplified one):

class A {
 
public int funcA(int i) {
    funcB
("some manipulation of i")

 
}

 
private int funcB(int i) {

 
}
}

I am running funcA and I know how to mock private function funcB, but I want to capture the parameter that is passed to funcB - Is it possible? I tried using Mockito's captor bit I get an error (Argument(s) are different).

Thanks,
Eyal

Johan Haleby

unread,
Sep 8, 2014, 1:17:08 PM9/8/14
to powe...@googlegroups.com
Hi, 

Perhaps you can have a look at this example in git (captorAnnotationWorksOnPrivateMethods). The class that is tested is located here

/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Eyal Ringort

unread,
Sep 10, 2014, 4:05:03 AM9/10/14
to powe...@googlegroups.com
Hi,

This is what I did exactly but the capture didn't work...Are you sure this works?

Johan Haleby

unread,
Sep 10, 2014, 6:09:01 AM9/10/14
to powe...@googlegroups.com
Well it works for me at least. I'm using JUnit thought, haven't tried it in TestNG.

Johan Haleby

unread,
Sep 10, 2014, 6:09:13 AM9/10/14
to powe...@googlegroups.com
-thought + though

Eyal Ringort

unread,
Sep 11, 2014, 2:59:52 AM9/11/14
to powe...@googlegroups.com
Hi Johan,

In my case, the private method name is equal to the public one but with different arguments - Can this be the cause of my problem?

Johan Haleby

unread,
Sep 11, 2014, 5:19:13 AM9/11/14
to powe...@googlegroups.com
Yeah it could. I've updated the test with an example that uses private overloaded methods and captor. The verification step looks like this:

verifyPrivate(demo).invoke(method(PrivateFinalOverload.class, "say", String.class, String.class)).withArguments(anyString(), captor.capture());

However when running this example ran into a bug in PowerMock (had to do with argument concatenation). I've now fixed this and uploaded a new SNAPSHOT to sonatype. So please try it out by depending on version 1.5.7-SNAPSHOT after having added the following repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <snapshots />
        </repository>
</repositories>

Tell me if it works for you or not.
/Johan


Eyal Ringort

unread,
Sep 11, 2014, 7:41:27 AM9/11/14
to powe...@googlegroups.com
HI Johan,

What if I want to mock the private method?

Johan Haleby

unread,
Sep 12, 2014, 3:42:41 AM9/12/14
to powe...@googlegroups.com
Look at the example. It's partially mocking a private final overloaded method.

/Johan
Reply all
Reply to author
Forward
0 new messages