Tôi đang đối mặt với một vấn đề rất lạ.Mockito mơ hồ - 0 Matchers dự kiến, 1 Recorded (InvalidUseOfMatchersException)
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
eq(this.config.apiEndpoint() + URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
hoặc thậm chí bằng bao gồm
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(this.config.apiEndpoint() + URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
mang lại cho tôi
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
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.
Thậm chí nếu tôi không sử dụng các biểu RAW.
Kỳ lạ nếu tôi thay đổi chứa phương pháp để:
URL = "/my/specific/url/";
when(this.restHelperMock.post(
eq(myEnum),
contains(URL),
any(JSONObject.class))).thenReturn(new JSONObject(myDesiredJsonContent));
bỏ qua thiết bị đầu cuối, nó hoạt động.
Các Config và RestHelper đều chế giễu:
this.restHelperMock = mock(RESTHelper.class);
this.config = mock(MyBMWConfiguration.class);
when(this.config.apiEndpoint()).thenReturn("http://host:port/api");
URL với ApiEndpoint bằng những gì tôi muốn thử, ngay cả khi nó sẽ không được, tôi sẽ nhận được một NullPointerException, vì sai chế giễu. Nhưng ở đây tôi không có bất kỳ ideea.
Cảm ơn câu trả lời của bạn.
Là 'contains' và' eq' của bạn từ 'Mockito', bởi vì ngay cả' hamcrest' có cả hai. –
Tôi đoán vấn đề có thể là bạn đang gọi một phương thức giả lập ('this.config.apiEndpoint()') trong lời gọi 'eq (...)'. Cố gắng đơn giản đặt URL hoàn chỉnh trong đó (http: // host: port/api/my/specific/url /) thay vì gọi một mô hình khác ở đó, điều này có thể gây nhầm lẫn cho Mockito, vì nó dựa vào trạng thái nội tại cho chế độ nhạo báng –
@ FlorianSchaetz cảm ơn bạn, đó là giải pháp. Tôi không nghĩ chuyện này có thể xảy ra. Bạn có thể vui lòng viết một anwser, vì vậy tôi có thể chấp nhận nó? –