Java Android 单元测试的注释处理(w/ Dagger)
Java annotation processing for Android unit testing (w/ Dagger)
我正在尝试使用来自 Dagger 版本 1.2.2 的 DI 在 Android Studio 1.0 中设置 Android 单元测试。
每当我 运行 我的测试并尝试用我的测试模块实例化一个 ObjectGraph 时,我得到以下 error/stacktrace:
java.lang.IllegalStateException: Module adapter for class java.util.Arrays$ArrayList could not be loaded. Please ensure that code generation was run for this module.
at dagger.internal.FailoverLoader.create(FailoverLoader.java:45)
at dagger.internal.FailoverLoader.create(FailoverLoader.java:40)
at dagger.internal.Memoizer.get(Memoizer.java:56)
at dagger.internal.FailoverLoader.getModuleAdapter(FailoverLoader.java:57)
at dagger.internal.Modules.loadModules(Modules.java:43)
at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:174)
at dagger.ObjectGraph$DaggerObjectGraph.access[=12=]0(ObjectGraph.java:138)
at dagger.ObjectGraph.create(ObjectGraph.java:129)
at com.company.app.HttpRequestManagerTest.setUp(HttpRequestManagerTest.java:35)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
这里是破解代码(@ObjectGraph.create
):
@Override
public void setUp() throws Exception {
super.setUp();
setContext(new MockContext());
mObjectGraph = ObjectGraph.create(Arrays.asList(new TestModule()));
mObjectGraph.inject(this);
}
深入研究,这里是 TestModule
:
package com.company.app.provider;
import dagger.Module;
@Module(
overrides = true,
library = true
)
public class TestModule {
}
这似乎表明 Java 注释处理未激活,但我认为情况并非如此,因为我在我的代码中使用了其他注释,例如 @Override
问题。我想知道 Dagger 注释处理器是否以某种方式未应用于测试版本。这是我的 build.gradle
的价值:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.company.app"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
pickFirst 'META-INF/services/javax.annotation.processing.Processor'
}
}
ext {
daggerVersion = '1.2.2'
mockitoVersion = '2.0.3-beta'
powerMockVersion = '1.5.4'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v13:21.0.0'
compile 'io.realm:realm-android:0.77.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.jakewharton:butterknife:6.0.0'
compile "com.squareup.dagger:dagger:$daggerVersion"
provided "com.squareup.dagger:dagger-compiler:$daggerVersion"
androidTestCompile "org.mockito:mockito-core:$mockitoVersion"
}
我在这里能找到的唯一可疑的东西是 pickFirst 'META-INF/services/javax.annotation.processing.Processor'
,但这是解决已知 Butterknife 问题所必需的。
非常感谢任何意见,谢谢!
ObjectGraph.create(Arrays.asList(new TestModule()));
ObjectGraph.create
是可变参数方法。它不需要 List
。您可以将模块列为单独的参数,也可以传递 Object[]
.
ObjectGraph.create(new TestModule());
我正在尝试使用来自 Dagger 版本 1.2.2 的 DI 在 Android Studio 1.0 中设置 Android 单元测试。
每当我 运行 我的测试并尝试用我的测试模块实例化一个 ObjectGraph 时,我得到以下 error/stacktrace:
java.lang.IllegalStateException: Module adapter for class java.util.Arrays$ArrayList could not be loaded. Please ensure that code generation was run for this module.
at dagger.internal.FailoverLoader.create(FailoverLoader.java:45)
at dagger.internal.FailoverLoader.create(FailoverLoader.java:40)
at dagger.internal.Memoizer.get(Memoizer.java:56)
at dagger.internal.FailoverLoader.getModuleAdapter(FailoverLoader.java:57)
at dagger.internal.Modules.loadModules(Modules.java:43)
at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:174)
at dagger.ObjectGraph$DaggerObjectGraph.access[=12=]0(ObjectGraph.java:138)
at dagger.ObjectGraph.create(ObjectGraph.java:129)
at com.company.app.HttpRequestManagerTest.setUp(HttpRequestManagerTest.java:35)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
这里是破解代码(@ObjectGraph.create
):
@Override
public void setUp() throws Exception {
super.setUp();
setContext(new MockContext());
mObjectGraph = ObjectGraph.create(Arrays.asList(new TestModule()));
mObjectGraph.inject(this);
}
深入研究,这里是 TestModule
:
package com.company.app.provider;
import dagger.Module;
@Module(
overrides = true,
library = true
)
public class TestModule {
}
这似乎表明 Java 注释处理未激活,但我认为情况并非如此,因为我在我的代码中使用了其他注释,例如 @Override
问题。我想知道 Dagger 注释处理器是否以某种方式未应用于测试版本。这是我的 build.gradle
的价值:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.company.app"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
pickFirst 'META-INF/services/javax.annotation.processing.Processor'
}
}
ext {
daggerVersion = '1.2.2'
mockitoVersion = '2.0.3-beta'
powerMockVersion = '1.5.4'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v13:21.0.0'
compile 'io.realm:realm-android:0.77.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.jakewharton:butterknife:6.0.0'
compile "com.squareup.dagger:dagger:$daggerVersion"
provided "com.squareup.dagger:dagger-compiler:$daggerVersion"
androidTestCompile "org.mockito:mockito-core:$mockitoVersion"
}
我在这里能找到的唯一可疑的东西是 pickFirst 'META-INF/services/javax.annotation.processing.Processor'
,但这是解决已知 Butterknife 问题所必需的。
非常感谢任何意见,谢谢!
ObjectGraph.create(Arrays.asList(new TestModule()));
ObjectGraph.create
是可变参数方法。它不需要 List
。您可以将模块列为单独的参数,也可以传递 Object[]
.
ObjectGraph.create(new TestModule());