junit - Not able to partial mock static method with PowerMockito -
i mock static function named tobemockedfunction in util class. method called tobeunittested public static void method. want tobemockedfunction nothing. tried many approaches (snippet posted of such 2) of partial mock , stubbing , unable succeed.
please suggest doing wrong.
public class util { // code public static void tobeunittested(customobject cb, customobject1 cb1, list<customobject2> rows, boolean delete) { // code tobemockedfunction(cb, "test", "test"); } public static customobject tobemockedfunction(customobject cb, string str1) { // code } }
and below junit class
@runwith(powermockrunner.class) @preparefortest({ util.class}) public class utiltest { @test public void test1() { powermockito.spy(util.class); //mock tobemocked function , make nothing powermockito.when(powermockito.spy(util.tobemockedfunction((customobject)mockito.anyobject(), mockito.anystring()))).thenreturn(null); util.tobeunittested(cb, "test", "test"); } }
approach2
powermockito.mockstatic(util.class); powermockito.when(util.tobeunittested((customobject)mockito.anyobject(),mockito.anystring())).thencallrealmethod(); util.tobeunittested(cb, "test", "test");
this example of how can that:
@runwith(powermockrunner.class) @preparefortest({ util.class}) public class utiltest { @test public void test1() { powermockito.spy(util.class); powermockito.doreturn(null).when(util.class, "tobemockedfunction", mockito.any(customobject.class), mockito.anystring(), mockito.anystring()); list<customobject2> customobject2list = new arraylist<>(); customobject2list.add(new customobject2()); util.tobeunittested(new customobject(), new customobject1(), customobject2list, true); } }
please note code of op doesn't compile. method tobemockedfunction(customobject cb, string str1)
receives 2 parameters , calling 3: tobemockedfunction(cb, "test", "test");
. see, i've added last 1 method signature.
hope helps
Comments
Post a Comment