Kadzhaev Marat
Kadzhaev Marat

Reputation: 1317

Mockito exception

There is test code which thorws exception and I can't understand - why?

Exception:

    org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

Test code:

 @Test
    public void testUpdateBookingFormData() throws Exception {
        when(registrantFormService.getRegistrantFormByUUID(any(String.class))).thenReturn(registrantForm);
        when(bookingService.getById(any(Long.class))).thenReturn(booking);
        when(eventFieldRepository.findByEventIdOrderBySortIndexAsc(any(Long.class))).thenReturn(eventFieldList);
        when(registrantAggregateService.getRegistrantDataAggregate(any(RegistrantKey.class))).thenReturn(registrantAggregate);
        when(bookingFormStrategiesFactory.chooseStrategy(any(Long.class))).thenReturn(bookingFormStrategy);
        when(bookingFormValidatorsFactory.getValidatorForForm(any(Booking.class))).thenReturn(validator);
        when(validator.validate(any(BookingFormBean.class))).thenReturn(true);
        when(bookingFormStrategy.getFormByKey(any(Booking.class), any(RegistrantKey.class))).thenReturn(bookingFormBean);

        BookingFormsCollectionBean bookingFormsCollectionBean = bookingFormsService.updateBookingFormData(eq(booking.getKey().getVisitorId()), anyString(), anyMapOf(String.class, String[].class));

        assertThat(bookingFormsCollectionBean, is(IsNull.notNullValue()));
    }

What I am doing wrong?

UPD: Full code http://pastebin.com/rprLG8Nt

UPD2: This full exception message:

  org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
1 matchers expected, 3 recorded:
-> at com.evm.front.service.logic.bookingprocess.BookingFormsServiceTest.testUpdateBookingFormData(BookingFormsServiceTest.java:106)
-> at com.evm.front.service.logic.bookingprocess.BookingFormsServiceTest.testUpdateBookingFormData(BookingFormsServiceTest.java:106)
-> at com.evm.front.service.logic.bookingprocess.BookingFormsServiceTest.testUpdateBookingFormData(BookingFormsServiceTest.java:106)

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"));

For more info see javadoc for Matchers class.


    at com.evm.front.service.logic.bookingprocess.BookingFormsService.updateBookingFormData(BookingFormsService.java:202)
    at com.evm.front.service.logic.bookingprocess.BookingFormsServiceTest.testUpdateBookingFormData(BookingFormsServiceTest.java:106)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)

Upvotes: 1

Views: 263

Answers (1)

Tamas Rev
Tamas Rev

Reputation: 7164

The exception points us to line 106. But that line has two matchers while the exception complains about three. However, the next non-empty line, line 108 is strange: it includes matchers without a when() call. You can try to invoke it with actual values, something like this:

BookingFormsCollectionBean bookingFormsCollectionBean = bookingFormsService.
    updateBookingFormData(booking.getKey().getVisitorId(), "salala", Collections.EMPTY_MAP);

You can later use more specific data to feed this test with.

Upvotes: 1

Related Questions