使用 mockito 模拟最终类和通用接口

问题描述

我在 Spring Boot (2.4.x) 和 JUnit 4 中使用 Mockito (3.6.x) 进行单元测试。有些单元测试需要模拟最终类(例如 java.lang.reflect.Method),并且还有其他单元测试需要模拟通用接口(例如 org.springframework.core.env.Environment)。

我尝试了很多解决方案,但没有一个我有用。我发现这个 article 可以模拟最终类,但是当我添加 mock-inline-maker 插件时,只有模拟最终类的单元测试才有效(我模拟 java.lang.reflect.Method 的类)和其他崩溃(我模拟 org.springframework.core.env.Environment 的类),反之亦然,当我删除 mock-inline-maker 时。

有没有办法为测试中的某些类而不是其他类配置模拟内联生成器?

附言模拟最终类的唯一原因是在我的单元测试中实现这个代码块:

MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getmethod();
CustomAnnotation customAnnotation = method.getAnnotation(CustomAnnotation.class);

非常感谢大家。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)