Reputation: 761
Hi i'm trying to run an Espresso test but it fails and I'm sure what the problem is or what I am doing wrong.
When I run the tests I get the following in my console
Running tests
Test running started
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.Login.LoginActivity.onCreate(LoginActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
it says it fails because I need to use a Theme.AppCompat theme (or descendant) with this activity and shows in the stacktrace line 54 which is setContentView(R.layout.activity_login);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
I have set the theme in the application and even made sure by setting the theme on the activity
<application android:theme="@style/AppTheme">
<activity
android:name=".Login.LoginActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan"></activity>
and what my themes parent is "Theme.AppCompat"
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
So I am a bit confused as to why it fails on this when I am using a theme descending from Theme.AppCompat
here is also my activity extending from AppCompactActivity
public class LoginActivity extends AppCompatActivity
my dependecies in my Gradle for androidTest are
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
and this is the test I am trying to run
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginNavigationForgotPasswordTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);
@Test
public void testClick() {
// Find Button and Click on it
onView(withId(R.id.login_forgot_password_button)).perform(click());
// Find TextView and verify the correct text that is displayed
onView(withId(R.id.forgot_top_title)).check(matches(withText("Forgot Password")));
}
}
Maybe there is something bluntly obvious I am doing wrong.
Any help would be appreciated.
btw the app works fine when running it
Upvotes: 4
Views: 2867
Reputation: 3646
If you don't want to mess with your theme setup or your Manifest then there's a simpler solution.
Set the theme on runtime, like so:
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
context.setTheme(R.style.Theme_AppCompat_Light)
}
Upvotes: -1
Reputation: 5744
I had the same problem but the solution for me was simply to put an AppCompat-base theme in the manifest for AndroidTest, just like I had it for the Android app manifest. For my project the application manifest is not used for the Espresso tests, it uses the AndroidTest manifest instead.
Upvotes: 2
Reputation: 761
Not sure what fixed it but I did a few changes and managed to get a setup that works.
before:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
after:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
before:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
after:
compile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.github.afollestad.material-dialogs', module: 'core'
exclude group: 'com.github.afollestad.material-dialogs', module: 'commons'
exclude group: 'com.github.traex.rippleeffect', module:'library'
exclude group: 'com.balysv', module:'material-ripple'
exclude module: 'recyclerview-v7'
}
androidTestCompile ('com.android.support.test:runner:0.5'){
}
androidTestCompile ('com.android.support.test:rules:0.4.1'){
}
androidTestCompile ('junit:junit:4.12')
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
I also added the following resolutionStrategy
configurations.all{
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
}
}
In hindsight I could of also added support annotations this way
configurations.all{
resolutionStrategy {
force 'com.android.support:support-annotations:23.2.1','org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
}
}
Upvotes: 2