use*_*784 5 static-methods unit-testing final mockito
我有一些代码在final类中有一个静态方法.我试图嘲笑那种方法.我尝试过做一些事情..
public final Class Class1{
public static void doSomething(){
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能模仿doSomething()?我试过了..
Class1 c1=PowerMockito.mock(Class1.class)
PowerMockito.mockSatic(Class1.class);
Mockito.doNothing().when(c1).doSomething();
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.cerner.devcenter.wag.textHandler.AssessmentFactory_Test.testGetGradeReport_HTMLAssessment(AssessmentFactory_Test.java:63)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
Run Code Online (Sandbox Code Playgroud)
最常用的测试框架是 JUnit 4。因此,如果您使用它,则需要使用以下方式注释测试类:
@RunWith( PowerMockRunner.class )
@PrepareForTest( Class1.class )
Run Code Online (Sandbox Code Playgroud)
比
PowerMockito.mockSatic(Class1.class);
Mockito.doNothing().when(c1).doSomething();
Mockito.when(Class1.doSomething()).thenReturn(fakedValue);
// call of static method is required to mock it
PowerMockito.doNothing().when(Class1.class);
Class1.doSomething();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5542 次 |
| 最近记录: |