Mockito SubclassByteBuddyMockMaker.java:34 java.nio.channels.FIleLock Exception

时间:2016-11-16 10:34:52

标签: android exception mocking mockito

我正在尝试模仿java.nio.channels.FileLock,这是我的代码模拟并且它抛出以下异常。

FileLock fileLock = Mockito.mock(FileLock.class);
Mockito.when(fileLock.isValid()).thenReturn(false);

摇篮:

compile  'junit:junit:4.12'
compile  'org.mockito:mockito-core:2.2.17'
provided 'org.hamcrest:hamcrest-junit:2.0.0.0'

Excepton:

java.lang.ExceptionInInitializerError
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:34)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.<init>(ByteBuddyMockMaker.java:22)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:49)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:17)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:16)
at org.mockito.internal.configuration.plugins.Plugins.getMockMaker(Plugins.java:32)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:168)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:162)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.mock(Mockito.java:1632)
at org.mockito.Mockito.mock(Mockito.java:1545)
at com.accu_chek.datastorage_common.StandardJSONProviderTests.writeJSONTest_LockInvalid(StandardJSONProviderTests.java:113)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
Caused by: java.lang.NullPointerException: parentLoader == null && !nullAllowed
at java.lang.ClassLoader.<init>(ClassLoader.java:210)
at java.lang.ClassLoader.<init>(ClassLoader.java:202)
at java.security.SecureClassLoader.<init>(SecureClassLoader.java:48)
at java.net.URLClassLoader.<init>(URLClassLoader.java:710)
at java.net.URLClassLoader.<init>(URLClassLoader.java:555)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.<clinit>(TypeCachingBytecodeGenerator.java:21)
... 47 more

1 个答案:

答案 0 :(得分:2)

浪费了几个小时之后,原因是Mockito正在为.class文件生成模拟类,但由于我在Android Instrumentation测试中运行,所以我需要.dex文件而不是.class文件。所以现在我正在使用以下依赖,生活又好了:)

compile 'org.droidparts.dexmaker:dexmaker-mockito:1.5'