2016-05-24 5 views
1

Es gibt Testcode die Ausnahme thorws und ich kann nicht verstehen - warum?Mockito Ausnahme

Ausnahme:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

Prüfregeln:

@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())); 
    } 

Was ich falsch?

UPD: Voll Code http://pastebin.com/rprLG8Nt

UPD2: Diese vollständige Ausnahmemeldung:

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) 
+0

Wie erstellen Sie die Mock-Objekte? –

+0

@BilboBaggins gut, ich bin hinzufügen alle meine Code in diesem http://pastebin.com/rprLG8Nt –

+0

Was die volle Ausnahme ist? I.e. Welche Linie ist die problematische? –

Antwort

1

Die Ausnahme weist uns 106 auszukleiden. Aber diese Zeile hat zwei Matcher, während sich die Ausnahme über drei beschwert. Die nächste nicht leere Zeile, Zeile 108 ist jedoch merkwürdig: Sie enthält Matchierer ohne einen when() Aufruf. Sie können versuchen, es mit tatsächlichen Werten aufzurufen, etwa wie folgt:

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

Sie können später spezifischere Daten verwenden, um diesen Test mit zu versorgen.