为什么我的androidx仪器化测试套件进程崩溃了?

时间:2018-12-02 22:28:18

标签: android android-espresso androidx

启动具有以下依赖项的新项目:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:4.0.2'
    androidTestImplementation "androidx.test.ext:junit:1.0.0"
androidTestImplementation "androidx.test:rules:1.1.0"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

将默认配置设置为此:

android {
  ...
  defaultConfig {
    testInstrumentationRunner 'androidx.test.ext.junit.runners.AndroidJUnit4'
  }
}

我编写了以下测试类:

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule 
import org.junit.runner.RunWith

import org.junit.Rule
import org.junit.Test
import org.pos.activities.Checkout

@RunWith(AndroidJUnit4::class)
@LargeTest
class CheckoutInstrumentedTest {

  @get:Rule
  var activityRule: ActivityTestRule<Checkout> = 
         ActivityTestRule(Checkout::class.java)

  @Test
  fun testAddItem_computeTotal() {
      onView(withId(R.id.itemValue))
          .perform(typeText("5"), closeSoftKeyboard())

      onView(withId(R.id.addItemButton)).perform(click())

      onView(withId(R.id.total)).check(matches(withText("$5.00")))
  }
}

当我使用Android Studio运行测试时,在测试输出中出现以下错误

Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.

logcat中伴随以下错误:

java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{org.pos.test/androidx.test.ext.junit.runners.AndroidJUnit4}: java.lang.InstantiationException: java.lang.Class<androidx.test.ext.junit.runners.AndroidJUnit4> has no zero argument constructor
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5956)
        at android.app.ActivityThread.-wrap3(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1727)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
     Caused by: java.lang.InstantiationException: java.lang.Class<androidx.test.ext.junit.runners.AndroidJUnit4> has no zero argument constructor
        at java.lang.Class.newInstance(Native Method)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5954)

是什么导致跑步者无法加载?我正在运行Android v7.1的Samsung Galaxy Tab A上运行它。

1 个答案:

答案 0 :(得分:3)

您要两次添加jUnit:

// testImplementation 'junit:junit:4.12'
androidTestImplementation "androidx.test.ext:junit:1.0.0"

,测试运行者应为:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"