0
  • 最佳答案

    最终我找到了解决方案。第一个问题在于我将导入包的代码放在的"android"中的dependencies,但是实际上在我重新回顾build.gradle的时候发现,有两个dependencies,所以我重新新建一个项目并且把代码完全复制进去,同时还将依赖库的代码放在的独立在外的dependencies(即不在"android"这个标签中),其次它又报了android Cannot resolve symbol 'AndroidJUnit4'的错误,后来我在android - Cannot resolve symbol 'AndroidJUnit4' - Stack Overflow中找到了解决办法:就是将测试类放在AndroidTest文件夹下而不是Test文件夹下。

    总结一下,两个步骤:

    1. 将依赖库放在独立的dependencies,而不是"android"中的。
    2. 把测试类放在AndroidTest文件夹而不是Test文件夹。

    (我其实还是对这两个dependencies和两个文件夹的区别抱有疑问,接下来会去查询相关内容)

    这是我的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'
    }
    


    感谢每一个浏览,思考问题以及回答问题的人,由衷感谢!

    1424297526529466370  评论     打赏       EndlessShw
    • package com.example.databasedemo;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.mockito.Mock;
      import org.mockito.junit.MockitoJUnitRunner;
      import static org.mockito.Mockito.when;
      import android.content.Context;
      import androidx.test.platform.app.InstrumentationRegistry;
      
      @RunWith(MockitoJUnitRunner.class)
      public class TestDatabase {
      
          @Mock
          Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
      
          @Test
          public void testCreate(){
              // 这里创建数据库
      
          }
      
          @Test
          public void testInsert(){
              // 测试插入数据
              Dao dao = new Dao(appContext);
              dao.insert();
          }
      
          @Test
          public void testDelete(){
              // 测试删除数据
              Dao dao = new Dao(appContext);
              dao.delete();
          }
      
          @Test
          public void testUpdate(){
              // 测试修改数据
              Dao dao = new Dao(appContext);
              dao.update();
          }
      
          @Test
          public void testQuery(){
              // 测试查询数据
              Dao dao = new Dao(appContext);
              dao.query();
          }
      
      }
      




      我模仿官方的代码写,可是会报No instrumentation registered! Must run under a registering instrumentation.

      是不是我漏看了什么还是说哪里的用法不对,还请指个方向

      1424297526529466370  评论     打赏       EndlessShw
      • 如需使用此框架将模拟对象添加到本地单元测试,请遵循以下编程模型:


        1. 在 build.gradle 文件中添加 Mockito 库依赖项,如设置测试环境中所述。
        2. 在单元测试类定义的开头,添加 @RunWith(MockitoJUnitRunner.class) 注释。此注释可告知 Mockito 测试运行程序验证您对框架的使用是否正确无误,并简化了模拟对象的初始化。
        3. 如需为 Android 依赖项创建模拟对象,请在字段声明前添加 @Mock 注释。
        4. 如需模拟依赖项的行为,您可以使用 when() 和 thenReturn() 方法来指定某种条件以及满足该条件时的返回值。


        以下示例展示了如何创建使用模拟 Context 对象的单元测试。

        1153952789488054272  评论     打赏       拉大锯
        • 现在官方的更新了,你能打开这个链接吗?


          https://developer.android.com/training/testing/unit-testing/local-unit-tests?hl=zh-cn


          参考一下以下这段代码:


            import android.content.Context;
              import org.junit.Test;
              import org.junit.runner.RunWith;
              import org.mockito.Mock;
              import org.mockito.junit.MockitoJUnitRunner;
          
              import static com.google.common.truth.Truth.assertThat;
              import static org.mockito.Mockito.when;
          
              @RunWith(MockitoJUnitRunner.class)
              public class UnitTestSample {
          
                  private static final String FAKE_STRING = "HELLO WORLD";
          
                  @Mock
                  Context mockContext;
          
                  @Test
                  public void readStringFromContext_LocalizedString() {
                      // Given a mocked Context injected into the object under test...
                      when(mockContext.getString(R.string.hello_world))
                              .thenReturn(FAKE_STRING);
                      ClassUnderTest myObjectUnderTest = new ClassUnderTest(mockContext);
          
                      // ...when the string is returned from the object under test...
                      String result = myObjectUnderTest.getHelloWorldString();
          
                      // ...then the result should be the expected one.
                      assertThat(result, is(FAKE_STRING));
                  }
              }
          
          1153952789488054272  评论     打赏       拉大锯
          • 就像这样子:




            这几个依赖添加了吗?


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



            1153952789488054272  评论     打赏       拉大锯
            • EndlessShw  回复 @拉大锯
              都有,不过其中有一个是testImplementation 'junit:junit:4.+'
              EndlessShw 2021-08-08 17:54   回复 1424297526529466370
            • 拉大锯  回复 @EndlessShw
              一样的,那就奇怪了。
              拉大锯 2021-08-08 18:01   回复 1153952789488054272
          • 你是写在了androidTest的test目录下了吗?


            你把你的代码放到test目录下吧。

            1153952789488054272  评论     打赏       拉大锯
            • EndlessShw  回复 @拉大锯
              已经放在test目录下了,可是还是会有这个问题。之前百度的时候别人给的回答也是这样emmmm
              EndlessShw 2021-08-08 17:24   回复 1424297526529466370
            • 拉大锯  回复 @EndlessShw
              .AndroidJUnit4ClassRunner for AndroidJUnit4 could not be found.,那你ctrl+n,看看是否可以查找到AndroidJUnit4ClassRunner这个类。
              拉大锯 2021-08-08 17:34   回复 1153952789488054272
            • EndlessShw  回复 @拉大锯
              没有
              EndlessShw 2021-08-08 17:53   回复 1424297526529466370
          相关问题
          2020-11-13 23:44 779 2
          幻影~ · 提问
          2024-04-13 20:13 7 2
          幻影~ · 找工作
          2024-04-07 10:44 12 2
          幻影~ · 问题
          2024-03-31 17:20 7 2
          TONYGFX · AOSP
          2024-03-28 17:11 4 2