【发布时间】:2019-02-13 21:39:12
【问题描述】:
我有一个服务,一个 bean,它包含一个 @Transactional 方法:
public class InMessageService {
...
@Transactional
public boolean retryInMessage(String messageId) {
...
}
}
为了测试,我尝试使用 Mockito 模拟该服务:
@Bean
@Primary
public InMessageService inMessageService() {
return Mockito.mock(InMessageService.class);
}
当我开始测试时,结果是以下异常:
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB
subclass of class somePackage.InMessageService$MockitoMock$156222813: Common
causes of this problem include using a final class or a non-visible class;nested exception is
org.springframework.cglib.core.CodeGenerationException: java.lang.NoClassDefFoundError-->
somePath/InMessageService$MockitoMock$156222813
我想提一下,相同的代码适用于 spring-boot 1.2.1 和 Mockito 1.10.19。我尝试使用 spring boot 2.1.1 和 Mockito 2.23.0 运行上述代码
到目前为止我的观察:
- 无论我使用 2.1.0 和 2.23.0 之间的哪个 Mockito 版本,异常都是一样的。我不能(也不想)使用旧版本的 Mockito,因为该项目不再编译
- 如果我临时删除
@Transactional注释,则不会引发异常。
有什么想法可以通过升级 Spring Boot 进行调整以使测试再次工作?
谢谢!
【问题讨论】:
标签: spring-boot junit mockito cglib