为什么我的 androidx 插桩测试套件进程崩溃了?

Why is my androidx instrumented test suite process crashing?

开始一个具有以下依赖项的新项目:

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'
  }
}

我写了下面的测试class:

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(".00")))
  }
}

当我运行使用Android工作室进行测试时,我在测试输出中得到以下错误

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)

是什么导致 运行ner 加载失败?我在 Samsung Galaxy Tab A 运行ning Android v7.1 上 运行ning 这个。

您要添加 jUnit 两次:

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

测试运行器应该是:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"