java.lang.RuntimeException: 无法实例化测试运行器 class androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
java.lang.RuntimeException: Failed to instantiate test runner class androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
每当我执行测试时,我都会收到 RuntimeException
:
package com.example.mytipcalculator
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
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.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.Matchers.containsString
import org.junit.Rule
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class CalculatorTests() {
@get:Rule
val activity = ActivityScenarioRule(MainActivity::class.java)
fun test(){
onView(withId(R.id.cost_of_service_edit_text))
.perform(typeText("50.00"))
.perform(ViewActions.closeSoftKeyboard())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.calculate_button))
.perform(click())
onView(withId(R.id.tip_result))
.check(matches(withText(containsString(".00"))))
onView(withId(R.id.totalexpense))
.check(matches(withText(containsString(".00"))))
}
}
当我运行前面的代码时,我得到以下初始化错误:
java.lang.RuntimeException: Failed to instantiate test runner class androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
Test class class com.example.mytipcalculator.CalculatorTests is malformed. (1 problems):
java.lang.Exception: No runnable methods
at androidx.test.ext.junit.runners.AndroidJUnit4.throwInitializationError(AndroidJUnit4.java:129)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:121)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:82)
at androidx.test.ext.junit.runners.AndroidJUnit4.<init>(AndroidJUnit4.java:56)
... 14 trimmed
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:112)
... 17 more
Caused by: org.junit.runners.model.InitializationError
at org.junit.runners.ParentRunner.validate(ParentRunner.java:418)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:43)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:48)
... 20 more
您必须使用 @Test
注释来注释 test()
方法:
@Test
fun test() {
...
}
每当我执行测试时,我都会收到 RuntimeException
:
package com.example.mytipcalculator
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
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.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.Matchers.containsString
import org.junit.Rule
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class CalculatorTests() {
@get:Rule
val activity = ActivityScenarioRule(MainActivity::class.java)
fun test(){
onView(withId(R.id.cost_of_service_edit_text))
.perform(typeText("50.00"))
.perform(ViewActions.closeSoftKeyboard())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.increase))
.perform(click())
onView(withId(R.id.calculate_button))
.perform(click())
onView(withId(R.id.tip_result))
.check(matches(withText(containsString(".00"))))
onView(withId(R.id.totalexpense))
.check(matches(withText(containsString(".00"))))
}
}
当我运行前面的代码时,我得到以下初始化错误:
java.lang.RuntimeException: Failed to instantiate test runner class androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
Test class class com.example.mytipcalculator.CalculatorTests is malformed. (1 problems):
java.lang.Exception: No runnable methods
at androidx.test.ext.junit.runners.AndroidJUnit4.throwInitializationError(AndroidJUnit4.java:129)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:121)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:82)
at androidx.test.ext.junit.runners.AndroidJUnit4.<init>(AndroidJUnit4.java:56)
... 14 trimmed
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:112)
... 17 more
Caused by: org.junit.runners.model.InitializationError
at org.junit.runners.ParentRunner.validate(ParentRunner.java:418)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:43)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:48)
... 20 more
您必须使用 @Test
注释来注释 test()
方法:
@Test
fun test() {
...
}