gpt4 book ai didi

android - 找不到 AndroidJUnit4 的委托(delegate)运行程序 androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner

转载 作者:行者123 更新时间:2023-12-05 00:02:35 27 4
gpt4 key购买 nike

我已经编写了代码来测试 SQL 命令。但是当我右键单击我的类时(例如 testInsert() ),“运行”报告了错误:

"java.lang.RuntimeException: Delegate runnerandroidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner forAndroidJUnit4 could not be found.".


AS的版本是4.2.2。
抛出错误后,我立即询问了我的老师。他建议在依赖项下输入这些代码。
这是代码:
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
同时,我将测试类放在“test”文件夹下。
但这两种补救措施都无效。
然后我查找了Developer HandBook,但我没有找到解决方案。有什么我错过或误解的吗?
我引用了谷歌或必应,尽可能地导入各种包,但是什么也没有。
假设我添加了新配置。但是没有返回任何结果。同时测试框架意外退出。
所以你能帮我找到一个解决方案、一个方向或任何可以指导我克服这个问题的方法吗?我真的很感激!
例如,我想在 Dao 中测试 insert()。下面是道课(部分)(有一些注释我记下来了,忽略它):
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();
}
}
有依赖关系:
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'
})
}
}
}

最佳答案

最后,我找到了我的解决方案。
这里是:
首先,我犯了依赖关系的错误。我注意到我的 build.gradle 中有两个依赖项。所以我考虑依赖关系是否有问题。所以我重新创建了一个新项目并粘贴代码。我将包放在不包含“android”的依赖项中。但是编译器仍然报告了“无法解析符号'AndroidJUnit4'”的错误。我用谷歌搜索了这个错误。在 [enter link description here][1] 中得到了解决方案,将类放在 androidTest 文件夹中。
所以我可以直接下结论。
1. 将包放入不包含“android”的依赖项中。
2.从Test中移除类到AndroidTest
这是我的 build.gradle:

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.databasedemo"
minSdkVersion 18
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.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 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
我发现的解决方案,我仍然不知道这两个依赖项之间的区别。还有 Test-folder 和 AndroidTest-folder 之间的区别。我会查阅文章。
无论如何,我非常感谢你看到、想到或任何帮助我编辑或传播这个问题的人。谢谢!

关于android - 找不到 AndroidJUnit4 的委托(delegate)运行程序 androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68789382/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com