如何模拟AndroidJUnit4



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

我有这个:

@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);我得到一个异常:

. lang。ExceptionInInitializerError在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)(MockUtil.java:33org.mockito.internal.MockitoCore.mock (MockitoCore.java: 59)org.mockito.Mockito.mock (Mockito.java: 1285)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。调用(本机方法)org.junit.runners.model.FrameworkMethod runreflectivecall 1.美元(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) atorg.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 78)在org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 57)在org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) atorg.junit.runners.ParentRunner时间表(ParentRunner.java: 71) 1.美元org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 288)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 58)org.junit.runners.ParentRunner评估(ParentRunner.java: 268) 2.美元org.junit.runners.ParentRunner.run (ParentRunner.java: 363)org.junit.runners.Suite.runChild (Suite.java: 128)org.junit.runners.Suite.runChild (Suite.java: 27)org.junit.runners.ParentRunner 3.美元(ParentRunner.java: 290)运行org.junit.runners.ParentRunner时间表(ParentRunner.java: 71) 1.美元org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 288)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 58)org.junit.runners.ParentRunner评估(ParentRunner.java: 268) 2.美元org.junit.runners.ParentRunner.run (ParentRunner.java: 363)org.junit.runner.JUnitCore.run (JUnitCore.java: 137)org.junit.runner.JUnitCore.run (JUnitCore.java: 115)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:数组;InvocationTargetException——>零org.mockito.cglib.core.AbstractClassGenerator.create (AbstractClassGenerator.java: 238)在org.mockito.cglib.core.KeyFactory Generator.create美元(KeyFactory.java: 145)(KeyFactory.java:117org.mockito.cglib.core.KeyFactory.create (KeyFactory.java: 109)org.mockito.cglib.core.KeyFactory.create (KeyFactory.java: 105)org.mockito.cglib.proxy.Enhancer。(Enhancer.java: 70)……43引起:java.lang.reflect.InvocationTargetException atjava.lang.reflect.Method。调用(本机方法)org.mockito.cglib.core.ReflectUtils.defineClass (ReflectUtils.java: 385)在org.mockito.cglib.core.AbstractClassGenerator.create (AbstractClassGenerator.java: 220)…48个以上原因:java.lang.UnsupportedOperationException: can't加载这种类型的类文件java.lang.ClassLoader.defineClass (ClassLoader.java: 300)…51更多

java.lang.NoClassDefFoundError:org.mockito.internal.creation.cglib.ClassImposterizer 3美元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)(MockUtil.java:33org.mockito.internal.MockitoCore.mock (MockitoCore.java: 59)org.mockito.Mockito.mock (Mockito.java: 1285)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。调用(本机方法)org.junit.runners.model.FrameworkMethod runreflectivecall 1.美元(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) atorg.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 78)在org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java: 57)在org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) atorg.junit.runners.ParentRunner时间表(ParentRunner.java: 71) 1.美元org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 288)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 58)org.junit.runners.ParentRunner评估(ParentRunner.java: 268) 2.美元org.junit.runners.ParentRunner.run (ParentRunner.java: 363)org.junit.runners.Suite.runChild (Suite.java: 128)org.junit.runners.Suite.runChild (Suite.java: 27)org.junit.runners.ParentRunner 3.美元(ParentRunner.java: 290)运行org.junit.runners.ParentRunner时间表(ParentRunner.java: 71) 1.美元org.junit.runners.ParentRunner.runChildren (ParentRunner.java: 288)org.junit.runners.ParentRunner.access 000美元(ParentRunner.java: 58)org.junit.runners.ParentRunner评估(ParentRunner.java: 268) 2.美元org.junit.runners.ParentRunner.run (ParentRunner.java: 363)org.junit.runner.JUnitCore.run (JUnitCore.java: 137)org.junit.runner.JUnitCore.run (JUnitCore.java: 115)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)

我做错了什么?

Thanks in advance

我认为你需要设置Mockito与Android一起工作,因为它使用'dex'而不是'class'文件。在这里查看答案:

如何让Mockito在androidTest中工作

相关内容

  • 没有找到相关文章

最新更新