尝试模拟任何类的类都会生成ExceptionInInitializerError

编程入门 行业动态 更新时间:2024-10-19 19:26:58
本文介绍了尝试模拟任何类的类都会生成ExceptionInInitializerError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我运行以下代码时:

public class ActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { .... public void testCanCreateMockito() { List mockedList = Mockito.mock(List.class); } }

我得到以下例外:

java.lang.ExceptionInInitializerError at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95) at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) at org.mockito.Mockito.mock(Mockito.java:1285) at org.mockito.Mockito.mock(Mockito.java:1163) at com.acesounderglass.hungertracker.ActivityTest.testCanCreateMockito(ActivityTest.java:60) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837) Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238) at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105) at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70) ... 23 more Caused by: java.lang.reflect.InvocationTargetException at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220) ... 28 more Caused by: java.lang.UnsupportedOperationException: can't load this type of class file at java.lang.ClassLoader.defineClass(ClassLoader.java:300) ... 32 more

任何类都会发生这种情况,列表只是一个简单的例子.我的gradle依赖项是:

This occurs with any class, List was just an easy example. My gradle dependencies are:

dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.0' androidTestCompile "org.mockito:mockito-core:1.+" androidTestCompile files('libs/dexmaker-mockito-1.0.jar') androidTestCompile files('libs/dexmaker-1.0.jar') }

我已将gradle升级到1.1,并尝试使用实验性单元测试功能,但似乎没有任何改变.发生了什么事?

I've upgraded gradle to 1.1, tried using the experimental unit test feature and not, nothing seems to make a difference. What's going on?

推荐答案

我缺少两个dexmaker依赖项时收到此错误.

I received this error when I was missing the two dexmaker dependencies.

将这些行添加到app/gradle.build文件对我来说是有效的.

Adding these lines to the app/gradle.build file is working for me.

androidTestCompile 'org.mockito:mockito-core:1.10.19' androidTestCompile 'com.google.dexmaker:dexmaker:1.2' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

我也在使用Android Studio,并且发现更改依赖项后重新启动AS是个好主意.

I am also using Android Studio and have found it to be a good idea to restart AS after altering the dependencies.

更多推荐

尝试模拟任何类的类都会生成ExceptionInInitializerError

本文发布于:2023-11-16 11:35:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   ExceptionInInitializerError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!