2016-11-16 88 views
0

我想模擬java.nio.channels.FileLock這裏是我的代碼來模擬,它拋出以下異常。Mockito SubclassByteBuddyMockMaker.java:34 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 

回答

2

浪費好幾個小時的原因的Mockito是產生嘲笑類爲.class文件,但由於我在Android運行在後儀器測試,所以我需要.dex文件,而不是.class文件。所以現在我正在使用下面的依賴關係,生活又很好:)

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