How do I know if my app is running with Robolectric?

安稳与你 提交于 2019-11-28 11:58:40

This is what works well for me on Robolectric 3.

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

To start with, I'll say that you shouldn't be putting code to initialise dummy/test data in the normal releasable code and in general you shouldn't need to know from the main app if you're in a robo run or not.

Now moving past the disclaimer and to actually answer your question... One way you could to this is to have a method in your application class like this

public boolean isRoboTestRun() {
    return false;
}

Then create a "TestApplication" in the test package that extends your normal application and overrides this method to return true. It's hacky, but that's because it's not really meant to work that way :)

At some point you have to init OrmLiteSqliteOpenHelper with your Context.

Let assume you do this in your application class in onCreate. So just create Test<your application class name> in your tests sources and override onCreate with empty implementation.

Robolectric will find this class and will use during the tests. More details here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!