[英]Verify Static Method Call using PowerMockito 1.6
我正在為類似於以下示例的方法編寫JUnit測試用例:
Class SampleA{
public static void methodA(){
boolean isSuccessful = methodB();
if(isSuccessful){
SampleB.methodC();
}
}
public static boolean methodB(){
//some logic
return true;
}
}
Class SampleB{
public static void methodC(){
return;
}
}
我在我的測試類中編寫了以下測試用例:
@Test
public void testMethodA_1(){
PowerMockito.mockStatic(SampleA.class,SampleB.class);
PowerMockito.when(SampleA.methodB()).thenReturn(true);
PowerMockito.doNothing().when(SampleB.class,"methodC");
PowerMockito.doCallRealMethod().when(SampleA.class,"methodA");
SampleA.methodA();
}
現在我想驗證是否調用類Sample B的靜態methodC()。 如何使用PowerMockito 1.6實現? 我嘗試了很多東西,但似乎並沒有為我做好准備。 任何幫助表示贊賞。
就個人而言,我不得不說PowerMock等是解決問題的方法,如果你的代碼不錯,你不應該有這個問題。 在某些情況下,它是必需的,因為框架等使用靜態方法導致代碼無法以其他方式測試,但如果它是關於您的代碼,您應該總是更喜歡重構而不是靜態模擬。
無論如何,在PowerMockito中驗證不應該那么難......
PowerMockito.verifyStatic( Mockito.times(1)); // Verify that the following mock method was called exactly 1 time
SampleB.methodC();
(當然,要使其工作,您必須將SampleB添加到@PrepareForTest
注釋中並mockStatic
調用mockStatic
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.