简体   繁体   English

模拟来自正在测试的同一类的void方法

[英]Mocking a void method from the same class that is being tested

I am trying to test a method using mockito and powermockito, this method internally calling a method which returns void, i wanted to mock this method since i dont care about the result of that method. 我正在尝试使用嘲笑和powermockito测试一个方法,此方法内部调用一个返回void的方法,我想模​​拟这个方法,因为我不在乎该方法的结果。 i have been trying on this but unable to find the solution please help me on this. 我一直在尝试此方法,但找不到解决方案,请对此提供帮助。

I tries using powermockito as 我尝试使用powermockito作为

SearchProcedureCodeControllerBean mock = mock(SearchProcedureCodeControllerBean.class);
PowerMockito.doNothing().when(mock).setErrorMessage((String)anyObject(), Mockito.anyList().toArray(), (String)anyObject(), (String)anyObject());

setErrorMessage is the void method, signature for this method is as follows setErrorMessage是void方法,此方法的签名如下

public void setErrorMessage(String errorName, Object[] arguments,
            String messageBundle, String componentId) {
}

You can stub method of a mocked object only. 您只能对模拟对象的方法进行存根。 Tested object is definitely not mocked. 测试对象绝对不会被嘲笑。 With Power Mockito you can stub static methods, it's not your case. 使用Power Mockito,您可以对静态方法进行存根处理,而不是您的情况。 If you don't care about the method behavior just leave it like this. 如果您不关心方法的行为,则可以像这样保留它。 It`s void so it will baisically don't matter. 它是无效的,所以从根本上来说没关系。 If it does and you would like to suppress it's influence, it would mean the real problem is with the design as the class under test owns too much responsibility. 如果这样做,并且您想抑制它的影响,那将意味着真正的问题出在设计上,因为被测类承担了太多责任。 Extract the method to another class and then mock it. 将方法提取到另一个类,然后对其进行模拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM