我正在实现一种从 Elasticsearch 中的索引中删除别名的方法。为了验证该方法是否使用所需参数执行对 Elasticsearch 的请求,我正在使用 mockK 库编写单元测试。但是,org.apache.http.nio.entity.NStringEntity
即使只有一个这样的构造函数(public NStringEntity(final String s, final ContentType contentType)
),单元测试中类构造函数签名的签名也不匹配。任何想法如何解决这一问题?
这是方法:
override fun removeFromIndex(context: IndexContext, providedAliases: List<String>?) {
val aliasesToRemove = providedAliases ?: getByIndex(context)
val params: Map<String, String> = emptyMap()
for (alias in aliasesToRemove) {
val jsonString = IndexToAliasModel(context.index, alias).toJSONString("remove")
val entity = NStringEntity(jsonString, ContentType.APPLICATION_JSON)
esClientConf[context.cluster].performRequest("POST", "_aliases/", params, entity)
}
}
这是单元测试:
@Test
fun `Removing aliases of an index performs a request`() {
val context = IndexContext(ElasticsearchCluster.AUX, index="dummyIndex")
val providedAliases = listOf("dummyAlias1")
val esResponse = mockk<Response>()
mockkConstructor(NStringEntity::class)
val nStringEntity = mockk<NStringEntity>()
every { NStringEntity(any(), ContentType.APPLICATION_JSON) } returns nStringEntity
every { esClientConf[ElasticsearchCluster.AUX].performRequest(any(), any(), emptyMap<String, String>(), nStringEntity) } returns esResponse
esAliasService.removeFromIndex(context, providedAliases)
verify { esClientConf[ElasticsearchCluster.AUX].performRequest("POST", "_aliases/", emptyMap<String, String>(), nStringEntity) }
}
我在线收到以下错误every { NStringEntity(any(), ContentType.APPLICATION_JSON) } returns nStringEntity
:
io.mockk.MockKException: Failed matching mocking signature for
left matchers: [any()]
at io.mockk.impl.recording.SignatureMatcherDetector.detect(SignatureMatcherDetector.kt:99)
at io.mockk.impl.recording.states.RecordingState.signMatchers(RecordingState.kt:39)
at io.mockk.impl.recording.states.RecordingState.round(RecordingState.kt:31)
at io.mockk.impl.recording.CommonCallRecorder.round(CommonCallRecorder.kt:50)
at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:59)
at io.mockk.impl.eval.EveryBlockEvaluator.every(EveryBlockEvaluator.kt:30)
at io.mockk.MockKDsl.internalEvery(API.kt:92)
at io.mockk.MockKKt.every(MockK.kt:104)
(esClientConf[context.cluster]
返回一个 ElasticsearchRestClient
实例。)