Reputation: 213
I know this topic has been discussed but it is a little different here:
How to reproduce:
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:
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.sample.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
Upvotes: 2
Views: 2501
Reputation: 369
Using the new AndroidX libraries this can be solved by passing the theme to the fragment launching method:
val authDialogScenario = launchFragment<AuthDialog>(themeResId = R.style.AppTheme)
This was the solution in my case.
Upvotes: 4
Reputation: 213
The following code worked for me -- added to the unit test:
@Override
public void setUp(){
ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.AppTheme);
setActivityContext(context);
}
See also: ActivityUnitTestCase and startActivity with ActionBarActivity
Also possible
Use ActivityInstrumentationTestCase2 instead of ActivityUnitTestCase fixes the issue too.
In addition the "onPause" of the activity isn't called. Which is somehow odd with ActivityUnitTestCase
Upvotes: 3