简体   繁体   中英

How to inject dependencies into a Robolectric Test-class using RoboGuice

I want to start using Robolectric and RoboGuice in my Android-Apps. While I make satisfactory progress using Robolectric I am stuck using RoboGuice. I created a small Android-App for experimenting. It is only one Activity, injecting a Button and setting its OnClickListener. In the related Test-Class I want to Inject this Activity, to be able to test the Button. I tried a lot of things I found all over the internet, but none of these worked, so I give it a go here. Here is some code:

MainActivity.java:

package com.example.TrialApp;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;

public class MainActivity extends RoboActivity implements View.OnClickListener {

    @InjectView(R.id.main_LoginButton) private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        loginButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        SharedPreferences.Editor editor;
        if (view.getId() == R.id.main_Login_Button)
            Log.i("Login-Button pressed... ", "");
    }
}

CustomRobolectricTestRunner.java:

package com.example.TrialApp;

import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.junit.runners.model.InitializationError;

import java.io.File;
public class CustomRobolectricTestRunner extends RobolectricTestRunner {
    public CustomRobolectricTestRunner(Class testClass) throws InitializationError {
        // defaults to "AndroidManifest.xml", "res" in the current directory
        super(testClass, new File("TrialApp"));
    }
}

MainActivity_Test.java:

package com.example.TrialApp;

import com.google.inject.Inject;
import com.xtremelabs.robolectric.Robolectric;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertNotNull;

@RunWith(CustomRobolectricTestRunner.class)
public class MainActivity_Test {

    @Inject MainActivity mainActivity;
    @Inject ClassWithoutAName classWithoutAName;

    @Before
    public void setUp() {

    }

    @Test
    public void mainActivityShouldNotBeNull() {
        assertNotNull(mainActivity);
    }

    @Test
    public void classWithoutANameShouldNotBeNull() {
        assertNotNull(classWithoutAName);
    }
}

classWithoutAName is just a non-Activity-class with no content. I added just for injecting a non-Activity-class.

Running the Test-Class both tests fail giving the following errors:

java.lang.AssertionError
at com.example.TrialApp.MainActivity_Test.mainActivityShouldNotBeNull(MainActivity_Test.java:33) <8 internal calls>
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288) <16 internal calls>

and

java.lang.AssertionError
at com.example.TrialApp.MainActivity_Test.classWithoutANameShouldNotBeNull(MainActivity_Test.java:38) <8 internal calls>
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288) <16 internal calls>

Obviously something is missing. Injecting in MainActivity works fine, and the app is running.
Why is the same pattern of injecting dependencies in a Robolectric-Testclass not working? Where is the missing link?
Thank you

After reading some more articles and threads I found a partial solution. I still haven't found a way to inject UI-elements in a testclass, but I found out, how to inject non-UI-elements in a testclass. The trick is to implement a "RobolectricTestModule" extending the AbstractModule. The bindings made in the modules declared in the roboguice.xml are not present in the test-environment, so we need to declare the bindings for the test-environment in this extra-module. In the customized Testrunner we replace the DefaultRoboModule with the new RobolectricTestModule.
ClassWithoutAName.java:

package com.example.TrialApp;

public class ClassWithoutAName {

    private String string;

    public ClassWithoutAName(String string) {
        this.string = string;
    }

    public String getString() {
        return string;
    }
}

ClassWithoutANameProvider.java

package com.example.TrialApp.GuiceModules;

import com.example.TrialApp.ClassWithoutAName;
import com.google.inject.Provider;

public class ClassWithoutANameProvider implements Provider<ClassWithoutAName> {
    @Override
    public ClassWithoutAName get() {
        return new ClassWithoutAName("testString");
    }
}

ClassWithOutANameModule.java

package com.example.TrialApp.GuiceModules;

import com.example.TrialApp.ClassWithoutAName;
import com.google.inject.AbstractModule;

public class ClassWithOutANameModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(ClassWithoutAName.class).toProvider(ClassWithoutANameProvider.class);
    }
}

RobolectricTestModule.java

package com.example.TrialApp.GuiceModules;

import com.example.TrialApp.WeirdThings;
import com.google.inject.AbstractModule;

public class RobolectricTestModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(ClassWithoutAName.class).toProvider(ClassWithoutANameProvider.class);
    }
}

CustomRobolectricTestRunner.java

package com.example.TrialApp;

import android.app.Application;
import com.example.TrialApp.GuiceModules.RobolectricTestModule;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.junit.runners.model.InitializationError;
import roboguice.RoboGuice;

import java.io.File;

public class CustomRobolectricTestRunner extends RobolectricTestRunner {

    public CustomRobolectricTestRunner(Class testClass) throws InitializationError {
       // defaults to "AndroidManifest.xml", "res" in the current directory
        super(testClass, new File("TrialApp"));
    }

    @Override
    public void prepareTest(Object test) {
        Application application = (Application) Robolectric.application;

        RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
                RoboGuice.newDefaultRoboModule(application), new RobolectricTestModule());

        RoboGuice.getInjector(application).injectMembers(test);
    }
}

Thats it for the non-UI-Elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM