我正在实现从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]
返回一个Elasticsearch RestClient
实例。)