dimanche 27 décembre 2015

You need to use a Theme.AppCompat theme --> in Unit Test

I know this topic has been discussed but it is a little different here:

  • App works fine
  • Error is displayed only in unit test
  • Theme is Fine (new created sample project)

How to reproduce:

  1. Create just a new project --> FullScreenActivity Android 4.3
  2. Create a unit test
  3. Run it

    public class FullscreenActivityTest extends ActivityUnitTestCase<FullscreenActivity> {
    
    public FullscreenActivityTest() {
        super(FullscreenActivity.class);
    }
    
    public void testStart() {
        startActivity(new Intent(getInstrumentation()
                .getTargetContext(), FullscreenActivity.class), null, null);
        Assert.assertNotNull(getActivity());
    }
    
    

    }

Tested with:

  1. Nexus 5 Emulator
  2. Nexus 6P Emulator

Each time the same, app works fine. Unit test fails with:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:124)
        at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
        at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
        at com.comfylight.foobar.FullscreenActivity.onCreate(FullscreenActivity.java:88)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:163)

Thanks,

Paul

Aucun commentaire:

Enregistrer un commentaire