VerifyError using Mockito 1.9.5 and DexMaker-Mockito-1.0

后端 未结 5 610
渐次进展
渐次进展 2021-01-03 22:56

Like many others I was excited to hear that Mockito now works with Android and followed this tutorial to see it with my own eyes. Everything seemed fan-flapping-tastic and I

5条回答
  •  梦毁少年i
    2021-01-03 23:22

    We just had the same problem in a project, but our tests also failed on a real device.

    The cause was tracked to how Mockito uses the class loader, and resulted in the following error in LogCat:

    W/ActivityThread(5777): ClassLoader.getResources: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
    

    The fix was to explicitly set the class loader before calling mock() a test, eg.

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        fooImpl = mock(Foo.class)
    }
    

    The problematic file in Mockito is this one: org.mockito.internal.configuration.ClassPathLoader (line 121 in 1.9.5)

提交回复
热议问题