简体   繁体   English

使用EasyMock和Mockito模拟void方法

[英]Mocking void method with EasyMock and Mockito

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? 因此返回的测试方法很简单但是如何设置void方法呢? Thank you for suggestions, and Mcokito examples are welcomed too. 感谢您的建议,也欢迎Mcokito的例子。 } }

With easy mock, you don't need to wrap void functions around expect(). 使用easy mock,您不需要在expect()周围包含void函数。 You just need to do something like: 你只需要做一些事情:

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

It depends entirely on what you're trying to test. 这完全取决于你要测试的内容

In mockito, if you want to check only that the DAO delete method is called with the correct parameter, then verify is what you want. 在mockito中,如果只想检查使用正确参数调用DAO delete方法,则验证是否是您想要的。

I would suggest that this is exactly what you want since your unit test for PersonManager should not be testing PersonDao . 我建议这正是你想要的,因为你对PersonManager的单元测试不应该测试PersonDao

When deleting something, I suggest returning the object you just deleted. 删除某些内容时,我建议您返回刚刚删除的对象。 It makes testing much, much easier and allows doing things after you deleted (eg showing notice, logging, etc). 它使测试变得更加容易,并允许在删除后执行操作(例如显示通知,记录等)。 I think most (all?) Java collections are doing so. 我认为大多数(所有?)Java集合都是这样做的。

Mockito provides a static verify method that can verify when you call any method, even those that have void as return type. Mockito提供了一种静态verify方法,可以验证何时调用任何方法,甚至是那些返回类型为void的方法。 For your code sample, the following mockito code should work: 对于您的代码示例,以下mockito代码应该有效:

// 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. 希望这可以帮助。

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

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