java - Powermock verify private static method call in non static method -
dear stackoverflow comrades, again have problem in getting specific powermock / mockito case work. the issue is, need verify call of private static method, called public non-static method. similar example posted on how suppress , verify private static method calls?
this code:
class factory { public string factorobject() throws exception { string s = "hello mary lou"; checkstring(s); return s; } private static void checkstring(string s) throws exception { throw new exception(); } }
and testclass:
@runwith(powermockrunner.class) @preparefortest(factory.class) public class tests extends testcase { public void testfactory() throws exception { factory factory = mock(factory.class); suppress(method(factory.class, "checkstring", string.class)); string s = factory.factorobject(); verifyprivate(factory, times(8000)).invoke("checkstring", anystring()); } }
the problem here is, test successful, shouldn't be. shouldn't because private static method should called 1 times. no matter value put in times(), verifies true... halp :(
ok, think found answer, headache. rudy gave me final hint using using spy, still not trivial (although solution looks "baby-easy"). here complete solution:
import static org.mockito.matchers.anystring; import static org.mockito.mockito.times; import static org.powermock.api.mockito.powermockito.verifyprivate; import static org.powermock.api.mockito.powermockito.donothing; import static org.powermock.api.mockito.powermockito.spy; @runwith(powermockrunner.class) @preparefortest(factory.class) public class tests extends testcase { public void testfactory() throws exception { factory factoryspy = spy(new factory()); string s = factoryspy.factorobject(); donothing().when(factoryspy, "checkstring", anystring()); verifyprivate(factoryspy, times(1)).invoke("checkstring", anystring()); } }
Comments
Post a Comment