Android 模拟相机

Android mock Camera(Android 模拟相机)

本文介绍了Android 模拟相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以模拟 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 模拟相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android 模拟相机

基础教程推荐