8

我已阅读Room Persistence Library。我还克隆了 android-architecture-components然后我尝试添加迁移测试。但是,我无法导入

import android.arch.persistence.room.testing.MigrationTestHelper;

我也使用最新的 lib 版本。

android.arch.core:core-testing:1.0.0-alpha3

这是 MigrationTest 的代码

import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import android.arch.persistence.room.testing.MigrationTestHelper;

@RunWith(AndroidJUnit4.class)
public class MigrationTest {
    private static final String TEST_DB = "migration-test";

    @Rule
    public MigrationTestHelper helper;

    public MigrationTest() {
        helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
                MigrationDb.class.getCanonicalName(),
                new FrameworkSQLiteOpenHelperFactory());
    }

    @Test
    public void migrate1To2() throws IOException {
        SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);

        // db has schema version 1. insert some data using SQL queries.
        // You cannot use DAO classes because they expect the latest schema.
        //db.execSQL(...);

        // Prepare for the next version.
        db.close();

        // Re-open the database with version 2 and provide
        // MIGRATION_1_2 as the migration process.
        db = helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);

        // MigrationTestHelper automatically verifies the schema changes,
        // but you need to validate that the data was migrated properly.
    }
}
4

4 回答 4

10

由于代码使用 AndroidJUnit4 然后只需使用 androidTestCompile 代替

 androidTestCompile "android.arch.persistence.room:testing:$arch_version"

官方文档使用依赖项进行本地单元测试。但是,官方示例使用 Android runner...

https://developer.android.com/topic/libraries/architecture/adding-components.html

于 2017-06-26T04:35:04.217 回答
7

您正在使用 Android runner ( AndroidJUnit4.class),并且您的测试实际上放置在src/androidTest. 这意味着您正在使用Instrumented Tests应该声明哪些依赖项:

// Instrumented Unit Test or UI Test
androidTestComplile ....  

同时,如果你正在编写Local Unit Test,测试代码放在src/test,你可以声明依赖:

// Local Unit Test
testCompile ....

在 Google 文档中,他们只是给出了本地单元测试的示例。这里没有错。

于 2017-06-28T09:48:34.143 回答
5

主要问题是谷歌为 JUnit本地测试用例提供文档而不是工具(这意味着在真实设备或模拟器上进行测试)。

结果我看到了很多例子

testImplementation "androidx.room:room-testing:$room_version"

这是没有意义的,因为您只能使用工具测试用例来测试您的房间数据库。否则你会得到一个错误。所以你必须在你的 gradle 文件中使用androidTestImplementation而不是 testImplementation 。

def room_version = "2.2.5"
def test_version = "1.2.0"
androidTestImplementation "androidx.test:runner:$test_version"
androidTestImplementation "androidx.test:rules:$test_version"
androidTestImplementation "androidx.room:room-testing:$room_version"
于 2020-05-04T14:10:45.587 回答
1

QuangUmAnusorn的答案是正确的,但有点过时了。对于那些想知道的人:

  • 使用androidTestImplementation代替androidTestCompile
  • 安卓X:androidx.room:room-testing

gradle 中的完整实现:

androidTestImplementation "androidx.room:room-testing:$room_version"

(2.2.4 是撰写本文时的最新版本)

于 2020-03-10T10:11:56.967 回答