如何模拟AndroidJUnit4

时间:2016-10-05 06:46:05

标签: android junit mocking mockito powermock

我正在开发新的Android测试,我遇到了一些麻烦。我已阅读文档(https://developer.android.com/training/testing/start/index.html)。我已经开始实施测试,我想我需要一个检测单元测试(https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html),因为我需要我的应用程序上下文不是null,但是,我怎么能模拟一个对象?

我有这个:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class SplashPresenterImplTest {
    @Mock
    ApiEndpoints api;
    @Mock
    private SplashView splashView;

    private SplashPresenterImpl splashPresenter;

    @Before
    public void setupSplashPresenterTest() {
        // Mockito has a very convenient way to inject mocks by using the     @Mock annotation. To
        // inject the mocks in the test the initMocks method needs to be called.
        MockitoAnnotations.initMocks(this);

        // Get a reference to the class under test
        splashPresenter = new SplashPresenterImpl(splashView, api);
    }

    @Test
    public void syncGenres_success() {
        final Observable<List<Genre>> observable =     Observable.just(Arrays.asList(new Genre("trial","trial")));
        when(api.syncGenres()).thenReturn(observable);
        splashPresenter.syncGenres();

        verify(splashView).navigateToHome();
        final List<Genre> genres = SharedPreferencesUtils.getGenres();
        Assert.assertEquals(genres.size(), 1);
    }

    @Test
    public void syncGenres_error() {
        final Observable<List<Genre>> observable = Observable.error(new Throwable());
        when(api.syncGenres()).thenReturn(observable);
        splashPresenter.syncGenres();

        verify(splashView).onError(any(ApiError.class));
    }
}

正如您所看到的,我正在测试一个简单的演示者类,但在使用MockitoAnnotations.initMocks(this)时;我得到了一个例外:

  

java.lang.ExceptionInInitializerError at   org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)   在   org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)   在   org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)   在   org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)   在org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)at   org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)at   org.mockito.Mockito.mock(Mockito.java:1285)at   org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)   在   org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)   在   org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43)   在   org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66)   在   org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71)   在   org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)   在   org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)   在   com.vodity.android.splash.SplashPresenterImplTest.setupSplashPresenterTest(SplashPresenterImplTest.java:45)   在java.lang.reflect.Method.invoke(Native Method)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)   在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)   在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)   在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)   在   org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)   在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)   在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)   在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at 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 at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:137)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:115)at at   android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)   在   android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1879)   引起:org.mockito.cglib.core.CodeGenerationException:   java.lang.reflect.InvocationTargetException - &gt; null at   org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)   在   org.mockito.cglib.core.KeyFactory $ Generator.create(KeyFactory.java:145)   在org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)at   org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)at at   org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)at at   org.mockito.cglib.proxy.Enhancer。(Enhancer.java:70)... 43   更多引起:java.lang.reflect.InvocationTargetException at   java.lang.reflect.Method.invoke(Native Method)at   org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)   在   org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)   ... 48更多引起:java.lang.UnsupportedOperationException:不能   加载这种类文件   java.lang.ClassLoader.defineClass(ClassLoader.java:300)... 51更多

     

java.lang.NoClassDefFoundError:   org.mockito.internal.creation.cglib.ClassImposterizer $ 3 at   org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)   在   org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)   在   org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)   在   org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)   在org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)at   org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)at   org.mockito.Mockito.mock(Mockito.java:1285)at   org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)   在   org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)   在   org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43)   在   org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66)   在   org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71)   在   org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55)   在   org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108)   在   com.vodity.android.splash.SplashPresenterImplTest.setupSplashPresenterTest(SplashPresenterImplTest.java:45)   在java.lang.reflect.Method.invoke(Native Method)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)   在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)   在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)   在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)   在   org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)   在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)   在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)   在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at 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 at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:137)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:115)at at   android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)   在   android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1879)

我做错了什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

我认为您需要设置Mockito才能使用Android,因为它使用'dex'而不是'class'文件。请参阅答案:

How can I get Mockito working in androidTest