简体   繁体   中英

Why does upgrading Mockito from 1.9.5 to 1.10.8 break this Captor?

Given this target code:

...
sessionWrapper.execute(arenaCreateCql, arenaGuid, arenaName, displayName, authorName, createdOn);
...

And Mockito code to validate that line:

...
@Captor
private ArgumentCaptor<Date> createdOnCaptor;
...
@Test
public void testThat_Execute_CreatesNewArena() throws Exception {
    ...
    inOrder.verify(mockSessionWrapper).execute(
        eq(arenaCreateCql), eq(testArenaGuid), eq(testArenaName), eq(testArenaDisplayName), eq(testAuthorName), createdOnCaptor.capture());
    ...
    assertNotNull(createdOnCaptor.getValue());
}

This works using Mockito 1.9.5. When upgrading 1.10.8, the verify passes, but the getValue() fails with this error:

org.mockito.exceptions.base.MockitoException: 
No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()

Edit to add MCVE. The following code runs green with Mockito 1.9.5, red with Mockito 1.10.8.

MockitoExample.java:

package org.makeyourcase.example;

import java.util.Date;

public class MockitoExample {

    private MockitoExampleExecutor executor;

    public void execute(){
        executor.execute("var1", new Date());
    }
}

MockitoExampleExecutor.java:

package org.makeyourcase.example;

public class MockitoExampleExecutor {

    public void execute(Object... bindVariables){
    }
}

MockitoExample_UT:

package org.makeyourcase.example;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class MockitoExample_UT {

    @Mock
    private MockitoExampleExecutor mockitoExampleExecutor;
    @Captor
    private ArgumentCaptor<Date> dateCaptor;
    @InjectMocks
    private MockitoExample subject;

    @Test
    public void testThat_Execute_InvokesCalendar() throws Exception {
        subject.execute();
        verify(mockitoExampleExecutor).execute(eq("var1"), dateCaptor.capture());
        assertNotNull(dateCaptor.getValue());
    }
}

One other piece of info came to light as a result of creating the MCVE - the test works fine if the Date is the only element passed for bindVariables . That is, remove "var1" from target and test code, then the test runs fine under 1.9.5 and 1.10.8. Also, it doesn't matter that the captor is for a Date. The same issue occurs if the parameter is of another type, such as Integer.

Thanks, this is probably a bug, I've created the report on GH-188 .

Not sure when it will be fixed though. Fixed in GH-211 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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