gpt4 book ai didi

java - Mockito异常

转载 作者:行者123 更新时间:2023-11-30 07:21:57 25 4
gpt4 key购买 nike

有一个测试代码会引发异常,我无法理解 - 为什么?

异常(exception):

    org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

测试代码:

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

我做错了什么?

UPD:完整代码http://pastebin.com/rprLG8Nt

UPD2:完整的异常消息:

  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)

最佳答案

异常将我们指向106行。但是该行有两个匹配器,而异常则提示三个。然而,下一个非空行,即 108 行很奇怪:它包含没有 when() 调用的匹配器。您可以尝试使用实际值调用它,如下所示:

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

您稍后可以使用更具体的数据来进行此测试。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com