Diederik
Diederik

Reputation: 6488

Injecting memebers on Instrumented test with toothpick

I've got an instrumented Android test using Toothpick DI:

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    private static final String LOG_TAG = ExampleInstrumentedTest.class.getName();

    @Inject
    Context mContext;

    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertThat(appContext.getPackageName(), startsWith("com.honeybeeapp.toothpicktest.mytoothpickapplication"));

        final SimpleApp application = (SimpleApp) appContext.getApplicationContext();
        Scope scope = Toothpick.openScopes(application, this);

        Module module = new Module();
                    module.bind(Context.class).toInstance(application);
        scope.installTestModules(module);
        Toothpick.inject(this, scope);

        assertTrue(mContext != null); // FAILS :(

        Log.d(LOG_TAG, "Injected");
    }
}

(File in Full repo).

The rest of the application works, and injection on my Activity, Services and Fragments work fine.

When I call Toothpick.inject(otherThing, scope); on an instance of a different class in the instrumented test, that injection works fine.

My suspicion is that the Tests class (or app?) isn't finding the decorated @Inject members correctly for some reason.

I tried scope.installModules(module); and that doesn't work either.

Some bits of my gradle file:

android {
    compileSdkVersion 27
    flavorDimensions "default"
    defaultConfig {
        applicationId "com.honeybeeapp.toothpicktest.mytoothpickapplication"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                        'toothpick_registry_package_name': 'com.honeybeeapp.toothpicktest.mytoothpickapplication',
                ]
            }
        }
    }
    snip...

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.0'

        implementation 'com.android.support:support-v4:27.1.1'
        compile 'com.github.stephanenicolas.toothpick:toothpick-runtime:1.1.3'
        compile 'com.github.stephanenicolas.toothpick:smoothie:1.1.3'
        annotationProcessor 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.1.3'

        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

        testCompile 'com.github.stephanenicolas.toothpick:toothpick-testing:1.1.3'
    }

Full gradle file.

Upvotes: 0

Views: 578

Answers (1)

Diederik
Diederik

Reputation: 6488

Looking at a similar problem in dagger2 I see they simply had to add the annotation processor for tests explicitly. Doing the same for Toothpick seems to work as well!

Added the following to my gradle file:

    dependencies {
        ...
        androidTestAnnotationProcessor 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.1.3'
        ...
    }

Fixes the problem, and my tests pass!

Upvotes: 1

Related Questions