【发布时间】:2016-03-18 14:21:03
【问题描述】:
您好,我正在尝试运行 Espresso 测试,但它失败了,我确定问题是什么或我做错了什么。
当我运行测试时,我会在控制台中看到以下内容
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)
它说它失败了,因为我需要在这个活动中使用 Theme.AppCompat 主题(或后代),并显示在堆栈跟踪第 54 行,即 setContentView(R.layout.activity_login);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
我已经在应用程序中设置了主题,甚至通过在活动上设置主题来确保
<application android:theme="@style/AppTheme">
<activity
android:name=".Login.LoginActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan"></activity>
我的主题父是“Theme.AppCompat”
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
所以我有点困惑,为什么当我使用从 Theme.AppCompat 下降的主题时它会失败
这也是我从 AppCompactActivity 扩展的活动
public class LoginActivity extends AppCompatActivity
我在 Gradle for androidTest 中的依赖项是
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'
这是我要运行的测试
@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")));
}
}
也许我做错了什么直截了当。
任何帮助将不胜感激。
顺便说一句,应用程序在运行时运行良好
【问题讨论】:
标签: android testing android-espresso