Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android mock Camera

Is it possible to mock the Android Camera class ?

@Override
public void setUp() {
    _camera = Mockito.mock(Camera.class);
}

fails to generate a mock (ExceptionInitializerError in Mockito's createProxyClass).

Should I create some kind of wrapper around the Camera (not my favorite solution, would really like to just mock the class...)?

Or, should I use a different mock library then Mockito?

Hope somebody can point me in the right direction.

Complete stacktrace of 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
like image 507
bas Avatar asked Jan 12 '23 11:01

bas


2 Answers

The answer is late but here is a mock camera example for Android.

You can set the VideoFileInputSource to mock camera from video file

textureVideoInputSource = new VideoFileInputSource(this, "mock_input_video.mp4");

or you can enable hardware camera for video stream.

textureVideoInputSource = new CameraTextureVideoInputSource(this);

You can find the complete sample project here. https://github.com/muneikh/MockCamera

like image 183
muneikh Avatar answered Jan 21 '23 23:01

muneikh


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.

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.

like image 24
user2863920 Avatar answered Jan 21 '23 23:01

user2863920