java 使用 EasyMock 和 Mockito 模拟 void 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5951535/
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-30 13:38:03  来源:igfitidea点击:

Mocking void method with EasyMock and Mockito

javaunit-testingmockitoeasymock

提问by Jarek

Hello I would like to know what is the best approach to mock void methods for example: I have a PersonManager under the test and then I have dao that is mocked.

您好,我想知道什么是模拟 void 方法的最佳方法,例如:我在测试中有一个 PersonManager,然后我有一个被模拟的 dao。

class PersonManager {

    PersonDao dao...

    PersonManager(PersonDao dao)...

    Preson find(String person)...

    void delete(String person)...

}

class PersonManagerTest {

    Map<String, Person> persons ..... = "person1", "person2", "person3";

    PersonDao mock...

    PersonManager manager = new PersonManager(mock);

    //easy one
    @Test public void shouldReturnExistingPerson() {
        expect(mock.find("person1").andReturn(persons.get(0));
        Person result = manager.find("person1");
        // replay and verify logic
    }

    //but what should I do here?
    @Test public void shouldDeleteExistingPerson() {
        //should I remove a person from testing Map holding test data? or what am I doing wrong
    }
}

So testing method with return was easy but how to toset void method? Thank you for suggestions, and Mcokito examples are welcomed too. }

所以带返回的测试方法很简单,但是如何设置 void 方法呢?感谢您的建议,也欢迎 Mcokito 示例。}

回答by Amir Raminfar

With easy mock, you don't need to wrap void functions around expect(). You just need to do something like:

使用简单的模拟,您不需要在 expect() 周围包裹 void 函数。您只需要执行以下操作:

obj = createMock(...)
obj.someVoidMethod();
replay(obj);
...
verify(obj);

回答by Alan Escreet

It depends entirely on whatyou're trying to test.

这完全取决于什么你想测试。

In mockito, if you want to check only that the DAO delete method is called with the correct parameter, then verifyis what you want.

在 mockito 中,如果您只想检查是否使用正确的参数调用了 DAO 删除方法,那么验证是您想要的。

I would suggest that this is exactly what you want since your unit test for PersonManagershould not be testing PersonDao.

我建议这正是您想要的,因为您的单元测试PersonManager不应该是 testing PersonDao

回答by Sorrow

When deleting something, I suggest returning the object you just deleted. It makes testing much, much easier and allows doing things after you deleted (e.g. showing notice, logging, etc). I think most (all?) Java collections are doing so.

删除时,建议返回刚刚删除的对象。它使测试变得更加容易,并允许在删除后执行操作(例如显示通知、日志记录等)。我认为大多数(全部?)Java 集合都是这样做的。

回答by Spoike

Mockito provides a static verifymethod that can verify when you call any method, even those that have void as return type. For your code sample, the following mockito code should work:

Mockito 提供了一个静态verify方法,可以在您调用任何方法时进行验证,即使是那些返回类型为 void 的方法。对于您的代码示例,以下模拟代码应该可以工作:

// Put this among your import statements    
import static org.mockito.Mockito.*

class PersonManagerTest {

    private PersonManager manager; // SUT

    private Map<String, Person> mockedPersons;

    private PersonDao mockDao;

    // Don't forget to setup from scratch for each test
    @Before public void setup() {
        mockDao = mock(PersonDao.class); // mockito mock method
        mockedPersons = new HashMap<String, Person>();
        for (int i=1; i<=3; i++) {
            mockedPersons.put("person"+i, mock(Person.class));
        }
        manager = new PersonManager(mockDao);
    }

    // setup dao to return a mocked person
    private void whenPersonIsAdded(int i) {
        Person personToReturn = mockedPersons.get("person"+i);
        when(mockDao.find("person"+i)).thenReturn(personToReturn);
    }

    @Test public void shouldReturnExistingPerson() {
        whenPersonIsAdded(1);
        Person expectedPerson = mockPerson;

        Person actualPerson = manager.find("person1");

        assertEquals(expectedPerson, actualPerson);
    }

    @Test public void shouldDeleteExistingPerson() {
        String expectedPersonString = "person1";

        manager.delete(expectedPersonString);

        verify(mockDao).delete(expectedPersonString);
    }
}

Hope this helps.

希望这可以帮助。