skip to Main Content

I’ve written the code to test the SQL commands. But when I right-clicked my class(such as testInsert() ), the "Run" reported the error:

"java.lang.RuntimeException: Delegate runner
androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner for
AndroidJUnit4 could not be found.".

The version of AS is 4.2.2.

After the error thrown, I asked my teacher immediately. He suggested that input these code under the dependencies.

here is the code:

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Meanwhile, I put the test-class under the "test" folder.

But both of these remedies are not effective.

Then I looked up for the Developer HandBook, but I didn’t find out the solution. Is there anything I missed or misunderstood?

I refered to the Google or Bing, import various packages as I could, however there goes nothing.

Atfer that I add new configuration. But no results were returned. Meanwhile Test framework quit unexpectedly.

So could you help me with a solution, a direction or whatever can guide me to conquer this. I really appreciate!

For example, I wanna test the insert() in Dao. Here is the Dao-class(partial)(There are some notes which I mark to remember the points, just ignore it):

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();
    }
}

Here is the test-class(I created the database and columns previously, here I just wanna insert the data):

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();
    }
}

There is the dependencies:

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

2

Answers


  1. Chosen as BEST ANSWER

    Finally, I found out my solution.
    Here it is:

    Firstly, I made a mistake of dependencies. I had noticed that there were two dependencies in my build.gradle. so I consider whether there is something wrong with dependencies. So I recreated a new project and paste the code. I put the packages in the dependencies in which is not included "android". But the compiler still reported the error which is "Cannot resolve symbol 'AndroidJUnit4'". I googled this error. Got the solution in [enter link description here][1], which is putting the class in androidTest folder.

    So I can jump to the conclusion.
    1. Put the packages in the dependencies in which is not included "android".
    2. Remove the class from Test to AndroidTest

    Here is my 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'
    }
    

    Out Solution as I found, I still don't know the difference between the two dependencies. Also the distinction between the Test-folder and the AndroidTest-folder. I shall consult the articles.

    Anyway, I owe you a great debt of gratitude whoever had seen, thought, or anyone who help me edited or spread this question. Thank you!


  2. I was facing the same problem and tried many things.
    Then I noticed that AndroidStudio was giving me a warning on some tests:

    JUnit test should return Unit

    Just added : Unit as the return type to those tests and everything worked.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search