Mockito - 无效使用匹配器异常



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

@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)

相关内容

  • 没有找到相关文章

最新更新