baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.38k stars 4.31k forks source link

与阿里testable-mock框架合用时报错 #4497

Closed endsock closed 1 year ago

endsock commented 2 years ago

我的源文件:

@Service
public class SystemClickService {
  @VariableDataSource(value = DataSourceConstants.ODPS_FRONTLOG_MASTER)
    public List<SystemClick> getAll(){
        System.out.println(mockThis("method:getAll"));
        return systemClickMapper.selectList(new LambdaQueryWrapper<SystemClick>().eq(SystemClick::getSessionid,"3cd278bc-af1d-4ca1-af7d-2d0186390fa9").eq(SystemClick::getPtField,"2022-04-18"));
    }

    public String mockThis(String a){
        return a;
    }
}

测试文件:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:conf/core/ops-devops-common-api-servlet.xml"})
public class SystemClickServiceTest {
    @Autowired
    private SystemClickService systemClickService;

    public static class Mock {
        @MockInvoke(targetClass = SystemClickService.class)
        public String mockThis(String a){
            return "haha";
        }
    }

    @Test
    public void getAll() {
        System.out.println(JsonUtil.toJSONString(systemClickService.getAll()));
    }
}

运行测试的getAll方法,报错: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'ew.sqlSegment != null and ew.sqlSegment != '' and ew.nonEmptyOfWhere'. Cause: org.apache.ibatis.ognl.OgnlException: sqlSegment [org.apache.ibatis.reflection.ReflectionException: Error parsing property name '_Lambda$_getSessionid_1'. Didn't start with 'is', 'get' or 'set'.] 注释掉Mock静态内部类则getAll运行正常

testable-mock作者回复如下:

问题收到,因为Testable默认会将被测类中的方法引用替换为一个可被Mock的实名方法,导致了MyBatisPlus的条件构造器命名检查不通过(Eclipse的编译期曾经也出过类似问题,这种场景理论上更适合警告而不是报错,MyBatisPlus的这个检查有点过于苛刻了)。

这里我们会调整一下,让生成实名方法的范围缩小为实际要被Mock的调用,从而避免发生上述冲突。在当前版本里,暂时先避开使用MyBatisPlus条件构造器的类型吧。

这种场景理论上更适合警告而不是报错,MyBatisPlus的这个检查有点过于苛刻了,是否可以把这种报错改成警告?

miemieYaho commented 2 years ago

nested exception is org.apache.ibatis.builder.BuilderException