UmA*_*orn 3 android database-migration android-room android-architecture-components
我读过Room Persistence Library.我还克隆了android-architecture-components,然后我尝试添加Mirgration测试.但是,我无法导入
import android.arch.persistence.room.testing.MigrationTestHelper;
Run Code Online (Sandbox Code Playgroud)
我也使用最新的lib版本.
android.arch.core:core-testing:1.0.0-alpha3
Run Code Online (Sandbox Code Playgroud)
这是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.
}
}
Run Code Online (Sandbox Code Playgroud)
由于代码使用AndroidJUnit4,所以只需使用androidTestCompile
androidTestCompile "android.arch.persistence.room:testing:$arch_version"
Run Code Online (Sandbox Code Playgroud)
官方doc使用依赖性进行本地单元测试.但是,官方样本使用Android跑者......
https://developer.android.com/topic/libraries/architecture/adding-components.html
您正在使用Android运行程序(AndroidJUnit4.class),而您的测试实际上位于src/androidTest。这意味着您正在使用Instrumented Tests应声明的依赖项:
// Instrumented Unit Test or UI Test
androidTestComplile ....
Run Code Online (Sandbox Code Playgroud)
同时,如果您正在编写,则将Local Unit Test测试代码放置在src/test,则可以声明依赖项:
// Local Unit Test
testCompile ....
Run Code Online (Sandbox Code Playgroud)
在Google文档中,他们只是提供了本地单元测试的示例。没错
主要问题是谷歌为 JUnit本地测试用例提供文档而不是工具(这意味着在真实设备或模拟器上进行测试)。
结果我看到了很多例子
testImplementation "androidx.room:room-testing:$room_version"
Run Code Online (Sandbox Code Playgroud)
这是没有意义的,因为您只能使用工具测试用例来测试您的房间数据库。否则你会得到一个错误。因此,您必须在 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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1941 次 |
| 最近记录: |