我在学习编写测试类来测试SQLite中的SQL语句,但是当我右键运行测试函数时,“run” 里面就会报错,Android Studio的版本为4.2.2,运行的安卓为Android11
报错内容如下:
java.lang.RuntimeException: Delegate runner androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner for AndroidJUnit4 could not be found.
at androidx.test.ext.junit.runners.AndroidJUnit4.throwInitializationError(AndroidJUnit4.java:121)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:85)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:74)
at androidx.test.ext.junit.runners.AndroidJUnit4.<init>(AndroidJUnit4.java:48)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
at org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.ClassNotFoundException: androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:83)
... 18 more
我去询问别人,他给出了办法,一是导入dependencies,二是将测试类放在test文件夹下。要导入的内容如下:
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
这两个方法都试过后,依旧没有效果。
接着我去bing和谷歌查找,导入尽可能的包,依旧没有结果。
随后我在
【Android-SQLite】测试DAO类_小菜要发财的博客-CSDN博客
在写完DAO类进行增删改查操作后,可以对其进行测试
https://blog.csdn.net/qq_40716430/article/details/105452850
中尝试他的新建configuration方法,可是运行时出现Test framework quit unexpectedly。我尝试使用了在StackOverflow中别人给出的办法,可是依旧没有解决问题。
例如我想测试insert()
这是Dao类:
package com.example.databasedemo;
// 该类用于操作数据库的增删改查
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class Dao {
private static final String TAG = "Dao";
private final DatabaseHelper mHelper;
public Dao(Context context){
// 创建数据库
// 步骤:
// 1. 写一个类去继承SqliteOpenHelper
// 2. 实现里面的方法
// 实现的参数介绍
/**
*
* @ context 上下文
* @ name 数据库名称
* @ factory 游标工厂
* @ version 版本号
*/
// 3. 创建子类对象,再调用getReadableDatabase()/getWriteableDatabase()方法,即可创建数据库
mHelper = new DatabaseHelper(context);
mHelper.getWritableDatabase();
}
public void insert(){
// 获取到数据库连接(打开数据库
SQLiteDatabase db = mHelper.getWritableDatabase();
// ? 用来防止sql注入
String sql_insert = "insert into " + Constants.TABLE_NAME + " (id, name, age, salary, phone_number, address) values(?, ?, ?, ?, ?, ?)";
db.execSQL(sql_insert, new Object[]{1, "Tom", 25, 5000, 110110110, "USA"});
db.close();
}
}
这是测试类:
package com.example.databasedemo;
import android.content.Context;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class TestDatabase extends TestCase {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
@Test
public void testInsert(){
// 测试插入数据
Dao dao = new Dao(appContext);
dao.insert();
}
}
这是dependences:
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.databasedemo"
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.test.ext:junit:1.1.2'
testImplementation 'junit:junit:4.13.1'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.3.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.18.0'
testImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'com.android.support:support-annotations:26.1.0'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
debugImplementation ("androidx.fragment:fragment-testing:1.2.5", {
exclude group: 'androidx.test', module: 'core'
})
}
}
}
我不知道我是哪里做错了,还是某些方法看错了还是漏看了什么,希望能有人帮忙给个解决办法,或者指出我的错误,抑或是给一个参考学习的方向。非常感谢。