如何使用Android的激活与在内存数据库使用Robolectric单元测试?

编程入门 行业动态 更新时间:2024-10-25 20:24:05
本文介绍了如何使用Android的激活与在内存数据库使用Robolectric单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

正如标题所说。我知道,有一个限制在robolectric提供的内存数据库。有没有办法使用此与Active Android的?在默认配置中,看来该数据库被清除所有的测试运行之后,但不是每个测试

As the title says. I am aware that there is a limited in memory database provided in robolectric. Is there any way to use this with Active Android? Under the default configuration, it appears that the database is cleared after all the tests are run, but not for each test.

推荐答案

我用greenDao - 但原理是一样的。

I use greenDao - but the principle is the same.

我的应用程序类初始化我的DB(数据库都有一个名称)。对于我的测试中,我继承应用程序(它允许Robolectric调用此版本代替),并覆盖,获取数据库名称的方法 - 和返回null。这就意味着我在内存数据库创建。由于应用程序的创建是安装程序的一部分,那么在内存中一个新的数据库,用于每个测试。

My Application class initialises my DB (the DB has a name). For my tests I subclass Application (which allows Robolectric to call this version instead) and override the method that gets the DB name - and return null. This then means I create an in memory DB. As the Application creation is part of setUp then a new in memory DB is used for each test.

public class MyApplication extends android.app.Application { @Override public void onCreate() { super.onCreate(); initialiseDB(getDatabaseName()); } protected String getDatabaseName() { return "regular-db-name"; } private void initialiseDB(String dbName) { // DB initialization // one example would be: Configuration.Builder builder = new Configuration.Builder(this); builder.setDatabaseName(dbName); ActiveAndroid.initialize(builder.create()); } } public class TestApplication extends MyApplication { @Override protected String getDatabaseName() { // use fresh in memory db each time return null; } }

更多推荐

如何使用Android的激活与在内存数据库使用Robolectric单元测试?

本文发布于:2023-10-24 08:31:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   单元测试   内存   数据库   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!