mercredi 4 février 2015

Java annotation processing for Android unit testing (w/ Dagger)

I am attempting to set up Android unit tests in Android Studio 1.0 using DI from Dagger version 1.2.2.


Whenever I run my tests and I attempt to instantiate an ObjectGraph with my test module, I get the following 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$1.create(FailoverLoader.java:45)
at dagger.internal.FailoverLoader$1.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$000(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)


Here is the breaking code (@ ObjectGraph.create):



@Override
public void setUp() throws Exception {
super.setUp();
setContext(new MockContext());
mObjectGraph = ObjectGraph.create(Arrays.asList(new TestModule()));
mObjectGraph.inject(this);
}


Diving deeper, here is the TestModule:



package com.company.app.provider;

import dagger.Module;

@Module(
overrides = true,
library = true
)
public class TestModule {

}


It seems as though it's indicating that Java annotation processing is not active, but I don't think that's the case as I'm using other annotations such as @Override in my code without issues. I'm wondering if somehow the Dagger annotation processor is not being applied to the test build. Here is my build.gradle for what it's worth:



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"
}


The only fishy thing I could find here was the pickFirst 'META-INF/services/javax.annotation.processing.Processor', but that is necessary to overcome a known Butterknife issue.


Any input is greatly appreciated, thanks!


Aucun commentaire:

Enregistrer un commentaire