Hi StackOverflow社区。 p>
我正在为此项目进行单元测试:https://github.com/Artemsetko/kafka-elasticsearch-consumer/tree/ES7 并面对这个问题。基本上,我正在尝试测试此方法: protected void postBulkToEs(BulkRequest bulkRequest)
像这样:
@Test
public void testPostBulkToES() throws InterruptedException, IndexerESRecoverableException, IndexerESNotRecoverableException, ExecutionException, IOException {
String message = "test message";
String eventUUID = "eventUUID";
BulkRequest bulkRequest = new BulkRequest().add(new IndexRequest().id(eventUUID).type(testIndexType).source(message, XContentType.JSON).index(testIndexName));
Mockito.when(elasticSearchClientService.getEsClient()).thenReturn(mockedRestHighLevelClient);
Mockito.when(mockedRestHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT)).thenReturn(mockedBulkResponse);
Mockito.when(mockedBulkResponse.hasFailures()).thenReturn(false);
Mockito.when(mockedBulkRequest.numberOfActions()).thenReturn(1);
elasticSearchBatchService.postBulkToEs(mockedBulkRequest);
Mockito.verify(elasticSearchClientService).getEsClient();
}
org.elasticsearch.common.compress.NotXContentException:只能在某些xcontent字节或压缩的xcontent字节上调用压缩机检测
at org.elasticsearch.common.compress.CompressorFactory.compressor(CompressorFactory.java:56)
at org.elasticsearch.common.xcontent.XContentHelper.createParser(XContentHelper.java:70)
at org.elasticsearch.client.RequestConverters.bulk(RequestConverters.java:223)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1758)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1732)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1694)
at org.elasticsearch.client.RestHighLevelClient.bulk(RestHighLevelClient.java:470)
at org.elasticsearch.kafka.indexer.service.ElasticSearchBatchService.postBulkToEs(ElasticSearchBatchService.java:96)
at org.elasticsearch.kafka.indexer.service.ElasticSearchBatchServiceTest.testPostBulkToES(ElasticSearchBatchServiceTest.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
谁能帮助我正确进行单元测试。我需要验证弹性服务中的调用方法是否具有正确的参数