MatchersException的InvalidUse

MatchersException的InvalidUse

我正在尝试模拟一些类,但是以下代码给我带来了问题:

@RunWith(MockitoJUnitRunner.class)
public class CalendarServiceTest {

    private Calendar calendar;

    @Mock
    MyClient myClient;

    @InjectMocks
    CalendarService calendarService;

    @Before
    public void initMocks() {
        List<Proposal> proposals = new ArrayList<>();
        calendar = org.mockito.Mockito.mock(Calendar.class);
        Proposal proposal = new Proposal();
        proposal.setAmount((float) 109.5);
        proposals.add(proposal);
        calendar.getProposal().addAll(proposals);
        when(calendar.getProposal()).thenReturn(proposals);
    }

    @Test
    public void getCalendar(){
        initMocks();
        when(myClient.getCalendar(anyString(), anyString(), anyString(), new LocalDate(), new LocalDate(), new LocalDate(), new LocalDate(), anyString(), anyString(), anyString())).thenReturn(calendar); // <<== exception here
        Assert.assertNotNull(calendar);
    }
}


运行这个得到我:

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

最佳答案

如错误所示,如果您使用匹配器,则所有参数都需要使用它们,例如:

eq(new LocalDate())


或者,如果您不关心LocalDate的使用价值,请执行以下操作:

any(LocalDate.class)

关于java - Mockito-MatchersException的InvalidUse,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27601029/

10-09 03:50