单元测试时如何模拟elasticsearchoperations(spring data elasticsearch v.4.0.3)

kwvwclae  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(520)

我正在做一个使用spring 2.3.3、spring-data-elastic-4.0.3、elasticsearch 7.9.0和mockito core 3.3.3的项目
我试图在编写单元测试时模拟elasticsearchoperations,这是我要测试的服务(在使用elasticsearchoperations的地方):

@Service
public class SearchByLabelServiceImpl implements SearchByLabelService {
     private ElasticsearchOperations elasticsearchTemplate;

        public SearchByLabelServiceImpl(ElasticsearchOperations elasticsearchTemplate) {
            this.elasticsearchTemplate = elasticsearchTemplate;
        }

    public SearchPage<TagResponse> searchByLabel(String query, List<String> labelsToExclude, boolean shouldIncludeDescription, Pageable pageable) {

    ....
    enter code here
    var result = elasticsearchTemplate.search(nativeSearchQuery, TagResponse.class, IndexCoordinates.of(tagIndexName));

}

这是测试课:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(profiles = "test")
public class SearchByLabelServiceImplTest {

private ElasticsearchOperations elasticsearchTemplate = mock(ElasticsearchOperations.class);

    private SearchByLabelService searchByLabelService = new SearchByLabelServiceImpl(elasticsearchTemplate);

    @Test
    @Ignore
    public void shouldReturnTheTagsBasedOnSearchRequestIncludingDescription() {

   ...//Arrange

when(elasticsearchTemplate.search(queryArgumentCaptor.capture(), eq(TagResponse.class), IndexCoordinates.of(tagIndexName))).thenReturn(searchHitsResponse);

        var searchResponse = searchByLabelService.searchByLabel(testSearchQuery, testLabelsToExcludeList, true, PageRequest.of(1, 1));

....

//Assertions 

}

我得到的错误是:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface org.springframework.data.elasticsearch.core.ElasticsearchOperations.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

Java               : 11
JVM vendor name    : Oracle Corporation
JVM vendor version : 11.0.5+10-LTS
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 11.0.5+10-LTS
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 10.15.7

Underlying exception : java.lang.TypeNotPresentException: Type org.elasticsearch.cluster.metadata.AliasMetaData not present

    at com.optum.genesis.tag.service.SearchByLabelServiceImplTest.<init>(SearchByLabelServiceImplTest.java:41)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:226)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.TypeNotPresentException: Type org.elasticsearch.cluster.metadata.AliasMetaData not present

非常感谢。

uinbv5nw

uinbv5nw1#

您使用的是spring数据elasticsearch 4.0.3,它是用elasticsearch库7.6.2构建的,但在运行时您提供的是elasticsearch 7.9.0。
elasticsearch在7.6和7.9之间的代码中有一点突破性的变化:类 org.elasticsearch.cluster.metadata.AliasMetaData 已重命名为 org.elasticsearch.cluster.metadata.AliasMetadata -请注意元数据中从d到d的变化。
那你能做什么呢?为什么需要使用7.9中的elasticsearch libs?如果您的集群运行在7.9上,您可能仍然可以使用7.6中的客户端libs访问它。或者更新应用程序以使用昨天发布的SpringDataElasticSearch4.1。

相关问题