Search code examples
unit-testingjunitandroid-holo-everywhere

Is it possible to test thru ActivityUnitTestCase a org.holoeverywhere.Activity?


I am coding my first ui test using holoEverywhere, I want to test a Activity which extends from org.holoeverywhere.app.Activity and I have this problem...

I tried to in the target app manifest this

And also in the manifest of the unit test project.

java.lang.IllegalStateException: Application instance isn't HoloEverywhere. Please sure that you extend class android.test.mock.MockApplication from a org.holoeverywhere.app.Application at android.support.v4.app._HoloActivity.onInit(_HoloActivity.java:449) at android.support.v4.app._HoloActivity.forceInit(_HoloActivity.java:207) at org.holoeverywhere.app.Activity.onCreate(Activity.java:227) ......

This is my code:

public class MainActivityUnitTest extends
android.test.ActivityUnitTestCase<MainActivity> {
    private MainActivity activity;

    public MainActivityUnitTest () {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
      super.setUp();
      Context context = getInstrumentation().getTargetContext();

      Intent intent = new Intent(context,
              MainActivity.class);
      startActivity(intent, null, null);
      activity = getActivity();
    }

Solution

  • The problem is your mock application (which is created by the unit test) is not a HoloEverywhere application. Call setApplication before calling startActivity. This will replace the used android.test.mock.MockApplication with an org.HoloEverywhere.app.Application.

    @Override
    protected void setUp() throws Exception
    {
       super.setUp();
    
       setApplication(new org.HoloEverywhere.app.Application());
    
       Intent intent = new Intent(getInstrumentation().getTargetContext(), DeclarerenActivity.class);
       startActivity(intent, null, null);
    }