2014-01-29 92 views
0

我有一個包含兩個靜態方法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執行一些參數匹配

回答

1

這聽起來像你想使用靜態間諜(部分模擬)。談到有關嘲諷靜態的section of the PowerMock documentation在第二個小一張紙條,上面可以很容易錯過:

(使用PowerMockito.spy(類),以嘲笑的具體方法)

注意,在你的例如,你實際上並沒有嘲笑這種行爲,只是驗證方法被調用。有一個微妙但重要的區別。如果你不想doSomething(Object)被稱爲做你需要做這樣的事情:

@Test 
public void toolTest() { 
    PowerMockito.spy(Tool.class); //This will call real methods by default. 

    //This will suppress the method call. 
    PowerMockito.doNothing().when(Tool.class); 
    Tool.doSomething(Mockito.argThat(new MyArgMatcher())); 

    Tool.callDoSomething(); 

    //The rest isn't needed since you're already mocking the behavior 
    //but you can still leave it in if you'd like. 
    PowerMockito.verifyStatic(); 
    Tool.doSomething(Mockito.argThat(new MyArgMatcher())); 
} 

如果你還是想方法雖然火,只是刪除了兩行doNothing()。 (我在我的Tool.java版本中添加了一個簡單的System.out.println("do something " + o);作爲doNothing()的附加驗證。)

+0

好先生,謝謝。 – sudocoder

0

你可以用這個做您的驗證:

public class Tool{ 

    public static boolean isFromCallDoSomethingMethod= false; 







    public static void doSomething(Object o){ 

    } 








    public static void callDoSomething() { 

     doSomething(new Object()); 

     isFromCallDoSomethingMethod= true; 

    } 








} 

可以做驗證爲:

if(Tool.isFromCallDoSomethingMethod){ 

     //you called doSomething() from callDoSomething(); 

    } 

記住

不要忘了,如果你的CA做驗證從另一種不是callDoSomething()doSomething(),你可以通過這樣做Tool.isFromCallDoSomethingMethod = false

這是你想要的嗎?

+0

感謝您的建議!我想避免更改Tool類,我應該在之前提到過。 – sudocoder

相關問題