带有匕首的Android测试异常

问题描述 投票:-1回答:1

这段代码的问题是我无法在测试类中添加任何内容。无法嘲笑TestModule中的所有内容。

我使用AndroidJUnit4和Mockito运行我的android测试。我们使用Android X和Dagger库。

问题在测试中注入对象。

代码

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AppHelperTest {
    @Inject
    public AppHelper appHelper;
    @Before
    public void setup() {
        Application application = (Application) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
        TestComponent component = DaggerTestComponent.builder().application(application).appModule(new TestModules()).build();
        component.inject(this);
    }

    @Test
    public void checkVersion_appHelper_Subscribed() {
verify(appHelper).checkVersion().test().assertSubscribed();
    }
}

TestRunner MockitoException Mockito无法模拟此类:AppHelper

基本异常:java.lang.UnsupportedOperationException:

Cannot define class using reflection
   at com.app.di.modules.TestModules.getAppHelper(TestModules.java:72)
    at com.app.di.modules.TestModules_GetAppHelperFactory.getAppHelper(TestModules_GetAppHelperFactory.java:33)
    at com.app.di.modules.TestModules_GetAppHelperFactory.get(TestModules_GetAppHelperFactory.java:25)
    at com.app.di.modules.TestModules_GetAppHelperFactory.get(TestModules_GetAppHelperFactory.java:8)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at com.app.di.DaggerTestComponent.injectAppHelperTest(DaggerTestComponent.java:450)
    at com.app.di.DaggerTestComponent.inject(DaggerTestComponent.java:435)
    at com.app.ui.helper.AppHelperTest.setup(AppHelperTest.java:46)
    at java.lang.reflect.Method.invoke(Native Method)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:76)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104)
    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 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
    at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2252)
 Caused by: java.lang.UnsupportedOperationException: Cannot define class using reflection
    at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Unavailable.defineClass(ClassInjector.java:819)
    at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:183)
    at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.l
android android-testing robolectric androidx dagger
1个回答
0
投票

使用Mockito版本3.2.0,我们提供了“本地” Android支持。要启用Android支持,请将mockito-android库作为依赖项添加到您的项目中。此工件已发布到相同的Mockito组织,并且可以如下导入Android:

repositories {
   jcenter()
 }
 dependencies {
   testCompile "org.mockito:mockito-core:+"
   androidTestCompile "org.mockito:mockito-android:+"
 }
© www.soinside.com 2019 - 2024. All rights reserved.