問題描述
是否可以模擬 Android Camera 類?
Is it possible to mock the Android Camera class ?
@Override
public void setUp() {
_camera = Mockito.mock(Camera.class);
}
無法生成模擬(Mockito 的 createProxyClass
中的 ExceptionInitializerError
).
fails to generate a mock (ExceptionInitializerError
in Mockito's createProxyClass
).
我是否應該在 Camera
周圍創建某種包裝器(不是我最喜歡的解決方案,真的只想模擬類...)?
Should I create some kind of wrapper around the Camera
(not my favorite solution, would really like to just mock the class...)?
或者,我應該使用與 Mockito 不同的模擬庫嗎?
Or, should I use a different mock library then Mockito?
希望有人能指出正確的方向.
Hope somebody can point me in the right direction.
ExceptionInitializerError
java.lang.ExceptionInInitializerError
at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)
at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
at com.cleancode.lifesaver.flashlight.test.FlashLightTests.setUp(FlashLightTests.java:20)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
Caused by: java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
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)
... 18 more
推薦答案
在你的堆棧跟蹤中,注意子字符串.CglibMockMaker":這就是問題所在.CGLib 不能在 android 上運行——有一個 Mockito 插件專門用于 android,它使用 dexmaker,它類似于 CGLib,但適用于 dalvik.只需搜索 dexmaker mockito,您就應該走在正確的道路上.
In your stacktrace, notice the substring ".CglibMockMaker": that's the problem here. CGLib doesn't work on android -- there's a plugin for Mockito specifically for android that uses dexmaker, which is like CGLib but works for dalvik. Just search for dexmaker mockito and you should be on the right path.
您仍然無法模擬 Camera.open()(靜態工廠方法),但您可以圍繞它重構代碼.重要的是 Camera 不是最終類.剩下的就是為測試而苦惱的管道,你應該接受它作為編寫經過良好測試的 Android 應用程序的稅.
You still won't be able to mock Camera.open() (the static factory method), but you can refactor your code around that. What matters is that Camera is not a final class; the rest is just awkward plumbing for the test, which you should just accept as the tax for writing well-tested android apps.
這篇關于Android 模擬相機的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!