0


记一篇Mockito MyBatisPlus 单元测试时报can not find lambda cache问题

记一篇Mockito MyBatisPlus 单元测试时报can not find lambda cache问题

刚才在写单测时,由于代码中用到了queryWrapper.orderByDesc()方法,报错com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache for this entity [com.orm.mybatis.entity.Stusent]

我的代码片段

LambdaQueryWrapper<Stusent> queryWrapper =newLambdaQueryWrapper<>();
        queryWrapper.eq(Stusent::getProjectSheetId, detail1.getProjectSheetId());
        queryWrapper.eq(Stusent::getStatus,ReviewStatusEnum.APPROVED.getCode());
        queryWrapper.orderByDesc(Stusent::getApprovalCompletedTime);List<Stusent>StusentDetails= stusentDao.getBaseMapper().selectList(queryWrapper);

用到了queryWrapper.orderByDesc()方法,导致单测报错。

报错日志

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: can not find lambda cache forthis entity [com.orm.mybatis.entity.Stusent]

    at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
    at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38)
    at com.baomidou.mybatisplus.core.toolkit.Assert.notNull(Assert.java:72)
    at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.tryInitCache(AbstractLambdaWrapper.java:94)
    at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.getColumn(AbstractLambdaWrapper.java:79)
    at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.columnToString(AbstractLambdaWrapper.java:62)
    at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.columnToString(AbstractLambdaWrapper.java:58)
    at com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper.columnToString(AbstractLambdaWrapper.java:38)
    at com.baomidou.mybatisplus.core.conditions.AbstractWrapper.lambda$orderBy$82c52469$1(AbstractWrapper.java:310)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1384)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.baomidou.mybatisplus.core.conditions.segments.OrderBySegmentList.transformList(OrderBySegmentList.java:37)
    at com.baomidou.mybatisplus.core.conditions.segments.AbstractISegmentList.addAll(AbstractISegmentList.java:60)
    at com.baomidou.mybatisplus.core.conditions.segments.MergeSegments.add(MergeSegments.java:50)
    at com.baomidou.mybatisplus.core.conditions.AbstractWrapper.doIt(AbstractWrapper.java:469)
    at com.baomidou.mybatisplus.core.conditions.AbstractWrapper.orderBy(AbstractWrapper.java:310)
    at com.baomidou.mybatisplus.core.conditions.AbstractWrapper.orderBy(AbstractWrapper.java:47)
    at com.baomidou.mybatisplus.core.conditions.interfaces.Func.orderByDesc(Func.java:264)
    at com.baomidou.mybatisplus.core.conditions.interfaces.Func.orderByDesc(Func.java:245)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
    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.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.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    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)Disconnected from the target VM, address: '127.0.0.1:54177', transport:'socket'Process finished withexit code 255

解决方案

在单测类中写@Before 方法,问题解决

importcom.baomidou.mybatisplus.core.MybatisConfiguration;importcom.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;importcom.baomidou.mybatisplus.core.metadata.TableInfoHelper;importcom.github.pagehelper.PageHelper;importorg.apache.ibatis.builder.MapperBuilderAssistant;importorg.junit.Before;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.mockito.InjectMocks;importorg.mockito.Mock;importorg.mockito.junit.MockitoJUnitRunner;importorg.powermock.core.classloader.annotations.PrepareForTest;importjava.math.BigDecimal;importjava.util.Arrays;importjava.util.Calendar;importjava.util.GregorianCalendar;importstaticorg.assertj.core.api.Assertions.assertThat;importstaticorg.mockito.ArgumentMatchers.any;importstaticorg.mockito.Mockito.verify;importstaticorg.mockito.Mockito.when;@RunWith(MockitoJUnitRunner.class)publicclassClassificationOpenServiceTest{@InjectMocksprivateStusentService stusentService;@MockprivateStudentDao studentDao;@MockprivateStudentDetailMapper studentDetailMapper;@BeforepublicvoidinitTable(){TableInfoHelper.initTableInfo(newMapperBuilderAssistant(newMybatisConfiguration(),""),Student.class);}@Test(expected =Test.None.class/* no exception expected */)publicvoidtestToVoid(){// SetupfinalStudent detail1 =newStudent();
        detail1.setId(0L);
        detail1.setClassificationQueryId("classificationQueryId");
        detail1.setProjectId("projectId");
        detail1.setClassificationCode("classificationCode");
        detail1.setProposalClassificationCode("proposalClassificationCode");
        detail1.setComprehensiveCommercialRating("comprehensiveCommercialRating");
        detail1.setVitalityRankLable("vitalityRankLable");
        detail1.setCommercialBuildingArea(newBigDecimal("0.00"));
        detail1.setCommercialBuildingAreaEnum("commercialBuildingAreaEnum");
        detail1.setRentalIncome(newBigDecimal("0.00"));finalStudentRequest request =newStudentRequest();
        request.setClassificationQueryId("classificationQueryId");
        request.setClassificationAlterCode("classificationAlterCode");
        request.setApprovalStatus("approvalStatus");
        request.setApprovalTime(newGregorianCalendar(2020,Calendar.JANUARY,1).getTime());
        request.setProjectStage("projectStage");when(studentDao.getBaseMapper()).thenReturn(studentDetailMapper);when(studentDetailMapper.selectList(any())).thenReturn(Arrays.asList(detail1));

        stusentService.toVoid(detail1, request);}}

本文转载自: https://blog.csdn.net/weixin_43969830/article/details/125680818
版权归原作者 愛~杦辷个訾 所有, 如有侵权,请联系我们删除。

“记一篇Mockito MyBatisPlus 单元测试时报can not find lambda cache问题”的评论:

还没有评论