我已经编写了一些操作 MotionEvent 实例的代码,并想为它编写一个单元测试。
单元测试只需要验证我的操作;它不需要模拟任何用户操作。
我知道它需要是一个仪器测试才能使用 MotionEvent 的方法;我需要实际的方法,而不是任何嘲笑。
我在目录 src/androidTest/... 中定义了一个检测测试;但是,它仍然抛出异常
java.lang.RuntimeException:在 android.view.MotionEvent 中获取的方法未模拟。
我的模块 build.gradle 文件包含以下条目:
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
我的测试类中的导入如下:
import android.view.MotionEvent;
import org.junit.Test;
import static org.junit.Assert.*;
我怎样才能运行这个测试?