简体   繁体   English

Mockito的验证和参数捕获器的工作

[英]Working of Mockito's verify and argument captor

I'm trying to test the following method 我正在尝试测试以下方法

public void saveToMultipleSources(MyObject myObject)
{
    if (myObject.isOfSomeType()) {
        firstDAO.saveObject(myObject);
        myObject.setContent(null);
    }
    secondDAO.saveObject(myObject);
}

The test I wrote was 我写的测试是

@Test
public void testSaveFeature()
{
    //initialize all objects
    obj.saveToMultipleSources(myObject); //myObject has a valid content.
    Mockito.verify(firstDAO).saveObject(myObject); 
    myObject.setContent(null);
    Mockito.verify(secondDAO).saveObject(myObject);
}

But on running, I get the error that expected and actual arguments differ for the verify statement of firstDAO. 但是在运行时,我得到的错误是firstDAO的verify语句的预期参数和实际参数不同。 The expected was an object with valid content but actual arguments invoked are with Content set as null. 期望的是具有有效内容的对象,但是实际调用的参数的Content设置为null。 I tried the exact same thing with ArgumentCaptor as well, but got the same results. 我也使用ArgumentCaptor尝试了完全相同的方法,但是得到了相同的结果。

Can someone explain why does Mockito behave this way? 有人可以解释为什么Mockito会这样吗? I tried logging the entire object and can see valid Content being set just before I call the firstDAO. 我尝试记录整个对象,并可以在调用firstDAO之前看到设置了有效的Content。

Also how do I go about testing this? 另外我该如何测试呢?

 //initialize all objects obj.saveToMultipleSources(myObject); //myObject has a valid content. Mockito.verify(firstDAO).saveObject(myObject); 

The problem is that setting the objects content to null is a side effect of your method. 问题在于将对象内容设置为null是方法的副作用。 In consequence Mockito compares the recorded parameter (having valid content) with the object modified by your method (having content already set to null ). 结果,Mockito将记录的参数(具有有效的内容)与通过您的方法修改的对象(具有的内容已设置为null )进行比较。

In that case, how can I test this? 在这种情况下,我该如何测试? – Abhilash Panigrahi – Abhilash Panigrahi

In the test Make myObject a Mockito.spy() and prevent the execution of setContent() : 在测试中,将myObjectMockito.spy()并阻止执行setContent()

@Test
public void testSaveFeature()
{
    //initialize all objects
    MyObject spyOfMyObject = Mockito.spy(myObject);
    doNothing().when(spyOfMyObject).setContent(null); // special null matcher may be needed...

    obj.saveToMultipleSources(spyOfMyObject); 

    Mockito.verify(firstDAO).saveObject(spyOfMyObject); 
    Mockito.verify(spyOfMyObject).setContent(null);
    Mockito.verify(secondDAO).saveObject(spyOfMyObject);
}

But you most likely want to be sure that myObject.setContent(null); 但是您很可能希望确保myObject.setContent(null); is called before econdDAO.saveObject(myObject);`... 在econdDAO.saveObject(myObject);之前被调用...

@Test
public void testSaveFeature()
{
    //initialize all objects
    MyObject spyOfMyObject = Mockito.spy(myObject);
    doNothing().when(spyOfMyObject).setContent(null);

    obj.saveToMultipleSources(spyOfMyObject); 

    Mockito.verify(firstDAO).saveObject(spyOfMyObject); 
    InOrder inOrder = Mockito.inOrder(spyOfMyObject,secondDAO);
    inOrder.verify(spyOfMyObject).setContent(null);
    inOrder..verify(secondDAO).saveObject(spyOfMyObject);
}

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

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