2014-01-29 53 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

相关问题