android 用匕首测试异常
android testing exception with dagger
这段代码的问题是我无法向测试添加任何内容class。无法模拟 TestModule 中的所有内容。
我 运行 我的 android 使用 AndroidJUnit4 和 Mockito 进行测试。我们使用 Android X 和 Dagger 库。
在测试中注入对象时出现问题。
code
@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 无法模拟此 class:AppHelper
Underlying exception : 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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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
对于 Mockito 版本 3.2.0,我们提供 "native" Android 支持。要启用 Android 支持,请将 mockito-android
库作为项目的依赖项添加。此工件发布到同一个 Mockito 组织,可以按如下方式为 Android 导入:
repositories {
jcenter()
}
dependencies {
testCompile "org.mockito:mockito-core:+"
androidTestCompile "org.mockito:mockito-android:+"
}
这段代码的问题是我无法向测试添加任何内容class。无法模拟 TestModule 中的所有内容。
我 运行 我的 android 使用 AndroidJUnit4 和 Mockito 进行测试。我们使用 Android X 和 Dagger 库。
在测试中注入对象时出现问题。
code
@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 无法模拟此 class:AppHelper
Underlying exception : 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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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
对于 Mockito 版本 3.2.0,我们提供 "native" Android 支持。要启用 Android 支持,请将 mockito-android
库作为项目的依赖项添加。此工件发布到同一个 Mockito 组织,可以按如下方式为 Android 导入:
repositories {
jcenter()
}
dependencies {
testCompile "org.mockito:mockito-core:+"
androidTestCompile "org.mockito:mockito-android:+"
}