java 重置 Mockito 间谍

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15090317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:27:06  来源:igfitidea点击:

Resetting Mockito Spy

javaunit-testingmockingmockito

提问by Alexander Bezrodniy

I have a test class (based on TestNG) where I use Mockito.verifyfor a spy object.

我有一个测试类(基于 TestNG),Mockito.verify用于间谍对象。

This works:

这有效:

public class Program {
    @Spy
    private TestObject testObject;

    @Test
    public void test1() {
       testObject.makeSth(); 
       verify(testObject, only()).someMethodNeedToBeChecked(); 
    }
}

But here:

但在这儿:

public class Program {
    @Spy
    private TestObject testObject;

    @Test
    public void test1() {
       testObject.makeSth(); 
       verify(testObject, only()).someMethodNeedToBeChecked(); 
    }

    @Test
    public void test2() {
        // Some different scenario
       testObject.makeSth(); 
       verify(testObject, only()).someMethodNeedToBeChecked(); 
        ...
    }
}

I get a Mokito exception that I have more that one invocation of someMethodNeedToBeCheckedmethod. Of course I tried to add Mockito.reset(testObject)but it didn't help me at all.

我得到一个 Mokito 异常,我有更多的someMethodNeedToBeChecked方法调用。当然,我尝试添加,Mockito.reset(testObject)但它根本没有帮助我。

How can I reset a spy object if I need to verify the same method in several tests?

如果我需要在多个测试中验证相同的方法,如何重置间谍对象?

回答by Jean-Philippe Bond

From the Mockito documentation:

从 Mockito文档

Resetting mocks (Since 1.8.0)

Smart Mockito users hardly use this feature because they know it could be a sign of poor tests. Normally, you don't need to reset your mocks, just create new mocks for each test method. Instead of reset() please consider writing simple, small and focused test methods over lengthy, over-specified tests. First potential code smell is reset() in the middle of the test method. This probably means you're testing too much. Follow the whisper of your test methods: "Please keep us small & focused on single behavior". There are several threads about it on mockito mailing list.

The only reason we added reset() method is to make it possible to work with container-injected mocks.

重置模拟(自 1.8.0 起)

Smart Mockito 用户几乎不使用此功能,因为他们知道这可能是测试不佳的标志。通常,您不需要重置模拟,只需为每个测试方法创建新模拟。请考虑在冗长的、过度指定的测试中编写简单、小而集中的测试方法,而不是 reset()。第一个潜在的代码味道是测试方法中间的 reset()。这可能意味着您测试过多。遵循您的测试方法的耳语:“请让我们保持小规模并专注于单一行为”。在 mockito 邮件列表上有几个关于它的主题。

我们添加 reset() 方法的唯一原因是可以使用容器注入的模拟。

You should probably just recreate the spy in a @BeforeMethod:

您可能应该在 a 中重新创建间谍@BeforeMethod

public class Program {

    private TestObject testObject = new TestObject();
    private TestObject spyTestObject;

    @BeforeMethod
    public void buildSpy() {
        spyTestObject = spy(testObject);
    }

    @Test
    public void test1() {
       spyTestObject.makeSth(); 
       verify(spyTestObject , only()).someMethodNeedToBeChecked(); 
    }

    @Test
    public void test2() {
        // Some different scenario
       spyTestObject.makeSth(); 
       verify(spyTestObject , only()).someMethodNeedToBeChecked(); 
        ...
    }
}