我有一個包含兩個靜態方法doSomething(Object)和callDoSomething()的Tool類。名稱很直觀,因爲callDoSomething將其調用委託給doSomething(Object);驗證一個靜態方法是由PowerMock中的另一個靜態方法調用的
public class Tool
{
public static void doSomething(Object o)
{
}
public static void callDoSomething()
{
doSomething(new Object());
}
}
我有工具Test類,我想驗證是否DoSomething的(對象)被稱爲(我想要做的爭論在未來匹配太)
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Tool.class })
public class ToolTest
{
@Test
public void toolTest()
{
PowerMockito.mockStatic(Tool.class);
Tool.callDoSomething();// error!!
//Tool.doSomething();// this works! it gets verified!
PowerMockito.verifyStatic();
Tool.doSomething(Mockito.argThat(new MyArgMatcher()));
}
class MyArgMatcher extends ArgumentMatcher<Object>
{
@Override
public boolean matches(Object argument)
{
return true;
}
}
}
驗證拾起doSomething(Object)如果直接調用它。我在上面評論過這段代碼。使用callDoSomething時,驗證不會拾取doSomething(Object),(這是上面顯示的代碼)。這是我運行上面的代碼時的錯誤日誌:
Wanted but not invoked tool.doSomething(null);
However, there were other interactions with this mock.
at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
at Tool.doSomething(Tool.java)
at ToolTest.toolTest(ToolTest.java:22)
... [truncated]
我想避免對Tool類進行任何更改。我的問題是,我怎麼能確認DoSomething的(對象)從callDoSomething(叫),以及對DoSomething的的PARAM執行一些參數匹配
好先生,謝謝。 – sudocoder