【发布时间】:2019-07-28 05:09:52
【问题描述】:
我正在尝试监视 restTemplate,我想存根 'exchange' 方法
这里有一些代码:
间谍类
@Bean
fun mockedRestTemplate(): RestTemplate = Mockito.spy(RestTemplate::class.java)
另一个类
val headers = HttpHeaders()
headers.setBasicAuth(UUID.randomUUID().toString(), UUID.randomUUID().toString())
val responseBody = "some error message"
val ex = HttpClientErrorException.create(
HttpStatus.NOT_FOUND,
"random",
headers,
responseBody.toByteArray(),
Charset.defaultCharset()
)
val httpEntity = HttpEntity(Any(), headers)
Mockito.doThrow(ex).`when`(restTemplate).exchange(
any() ?: config.randomEndpoint,
any(HttpMethod::class.java) ?: HttpMethod.POST,
any() ?: httpEntity,
any() ?: Foo::class.java
)
我在这里可能做错了什么?我收到此错误消息:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at test.suites.controller.FooTest.canGetFailedErrorFieldsIfApiRejectsRequest(FooTest.kt:468)
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"));
当我模拟/监视其他类时,它工作正常
【问题讨论】:
标签: spring spring-boot kotlin mockito