我如何知道我的应用程序是否在使用 Robolectric 运行? [英] How do I know if my app is running with Robolectric?

查看:24
本文介绍了我如何知道我的应用程序是否在使用 Robolectric 运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 ORMLite/SQLite 的 android 应用程序,我将 Robolectric 与 JUnit 4 结合使用,以允许我在 Android Studio 和 Jenkins 构建服务器上运行单元测试.

I have an android app that uses ORMLite/SQLite and I use Robolectric in conjunction with JUnit 4 to allow me to run unit tests in Android Studio and on a Jenkins build server.

通常我会在我的测试中设置测试数据,然后在设置中运行我的测试场景,但是当我尝试这样做时,我开始遇到问题和异常,这些问题和异常似乎与文件被锁定或其他相关这似乎是其他人遇到的问题...所以到目前为止我所做的是在我的数据库助手中使用 create database 方法来创建一些测试期望存在的虚拟数据.

Typically I would setup test data in my tests, in the setup, and then run my test scenarios against it but when I tried to do this I started getting issues and exceptions which seemed to be related to files being locked or something and that seems to be a problem others have had... so what I have done up until now is use the create database method in my database helper to create some dummy data which the tests expect to be there.

问题是我的应用程序现在需要插入一个真实的数据库,我不能让它在运行时设置虚拟数据.

The problem is my application now needs to plug into a real database and I can't have it setup dummy data when it runs.

是否有办法在我的数据库助手类中检测代码是在设备上还是在 Robolectric 中执行?

If there a way, within my database helper class, to detect if the code is executing on a device or within Robolectric?

推荐答案

这对我来说在 Robolectric 3 上效果很好.

This is what works well for me on Robolectric 3.

public static boolean isRoboUnitTest() {
    return "robolectric".equals(Build.FINGERPRINT);
}

这篇关于我如何知道我的应用程序是否在使用 Robolectric 运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆