简体   繁体   English

在开发模式下运行Play应用时出现Mockito问题

[英]Mockito issues when running a Play app in development mode

I am writing unit tests for a Play 1.x app, and using Mockito to easily create mock instances on the fly. 我正在为Play 1.x应用编写单元测试,并使用Mockito轻松地动态创建模拟实例。 I am running in development mode, so that I don't have to restart the app frequently. 我正在开发模式下运行,因此不必频繁重启应用程序。

The problem comes when I change the code, and the re-run my unit tests without restarting the Play app. 当我更改代码并重新运行单元测试而不重新启动Play应用时,问题就来了。 I get the following exception: 我得到以下异常:

A java.lang.ClassCastException has been caught, Cannot cast services.MyService$$EnhancerByMockitoWithCGLIB$$12cee5a2 to services.MyService

When I restart te app, and re-run the tests, all of them execute fine, but it takes me up to a couple of minutes to restart every time. 当我重新启动te app并重新运行测试时,它们都可以正常运行,但是每次最多要花几分钟才能重新启动。 Is there something that could be done? 有什么可以做的吗?

This is a known problem when working with Play, the lib classloader stays the same while the developed code is being changed on each reload, which causes issues for libraries that cache reflection related things, there a CCE . 这是一个已知的问题与播放工作时,的lib类加载器保持不变,而开发的代码被每个重装,这会导致问题对于高速缓存反射有关的东西图书馆改变,有一个CCE。

You have to disable the class cache by overriding the default configuration, see the complete documentation . 您必须通过覆盖默认配置来禁用类缓存,请参阅完整的文档

The idea is that in your test code base, create a class with the following qualified name org.mockito.configuration.MockitoConfiguration that will extends org.mockito.configuration.DefaultMockitoConfiguration and override the method boolean enableClassClache() to return false. 这个想法是,在测试代码库中,创建一个具有以下限定名称org.mockito.configuration.MockitoConfiguration的类, org.mockito.configuration.MockitoConfiguration将扩展org.mockito.configuration.DefaultMockitoConfiguration并覆盖方法boolean enableClassClache()以返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM