mocking void methods with arguments

5,946 views
Skip to first unread message

Joe Kearney

unread,
Nov 13, 2009, 6:09:36 AM11/13/09
to powe...@googlegroups.com, Melvin, Matthew (IDEAS)
Hi,

I need to mock a static void method that takes arguments. Is this possible, and how do I specify matchers for the arguments? Noddy sample below, I hope this conveys the idea.

Thanks,
Joe

============================================

class Static {
    public static void methodToMock(ParameterClass p) { ... }
}

class TestClass {
    public void testMethod() {
        mockStatic(Static.class);
        PowerMockito.doReturn(someObject).when(Static.class);
        Static.methodToMock(<accepts ParameterClass, not Matcher<ParameterClass>);

        // ... rest of the test and assertions
    }
}

Joe Kearney

unread,
Nov 13, 2009, 6:25:39 AM11/13/09
to powe...@googlegroups.com, Melvin, Matthew (IDEAS)
Ok, so I was using hamcrest matchers, not Mockito matchers, my mistake. Sorry for the spam.

Thanks,
Joe

2009/11/13 Joe Kearney <Joe.K...@morganstanley.com>

Johan Haleby

unread,
Nov 13, 2009, 6:27:06 AM11/13/09
to powe...@googlegroups.com
Hi,

Yes you can mock void methods. There's an example in the documentation (under "How to stub void static method to throw exception"). You can also have a look at e.g. "testMockStaticVoidWhenThrowingException" in MockStaticTest in subversion. These tells how to mock a static void method to throw an exception. If you just want to stub the method to do nothing you can either do something like:

suppress(method(Static.class, "methodName"));

or use the PowerMock equivalent of Mockito's doNothing() or doAnswer() etc.

Good luck,
/Johan

yamini majety

unread,
Jan 23, 2014, 10:19:15 AM1/23/14
to powe...@googlegroups.com
Hi,
 
I am new to Power Mock. Can any one help me on mocking a void static method using doAnswer(). My requirement is when a static void method is called i should be able to capture the arguments and return a value.
 
I tried to do the following but is giving me an error,
 
PowerMockito.mockStatic(StaticClass.class);
 
doAnswer(new Answer(){
 public Object answer(InvocationOnMock inv){
   Object[] args = InvocationOnMock.getArguments;
   //Do something
  return string
 }
}).when(StaticClass.class)staticVoidMethod();
 
I am not able to pass StaticClass.class but is expecting a mock object. And PowerMockito.mockStatic wont return any mock object.
 
Please help.

Johan Haleby

unread,
Jan 29, 2014, 5:43:09 AM1/29/14
to powe...@googlegroups.com
You can do like this if method is private:

 doAnswer(new Answer<String>() {
            public String answer(InvocationOnMock invocation) throws Throwable {
                return "Hello static";
            }
        }).when(StaticExample.class, "privateObjectMethod");

or like this if it's public:
   doAnswer(new Answer<String>() {
            public String answer(InvocationOnMock invocation) throws Throwable {
                return "something";
            }
        }).when(StaticExample.class);
        StaticExample.staticMethodReturningString();



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

Reply all
Reply to author
Forward
0 new messages