robolectric

How can I use Active Android with an in memory database for unit tests using Robolectric?

自闭症网瘾萝莉.ら 提交于 2019-12-05 03:44:47
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. Daniel I use greenDao - but the principle is the same. 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

Resources$NotFoundException when calling Robolectric.buildActivity()

孤人 提交于 2019-12-05 03:25:10
I am using Robolectric 3.0 RC3 for unit testing in Android Studio. And I am getting a ResourceNotFoundException every time I run the test. Please help me resolve the issue. build.gradle dependencies { testCompile 'junit:junit:4.12' compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' testCompile 'org.mockito:mockito-core:1.9.5' testCompile 'org.robolectric:robolectric:3.0-rc3' compile 'com.ibm.icu:icu4j:53.1' } Test Class @Before public void setUp() throws Exception { activity = Robolectric.buildActivity(MainActivity.class).create().get(); } @Test

java.util.ConcurrentModificationException in JUnit tests

烂漫一生 提交于 2019-12-05 01:26:10
This one is kind of a shot in the dark. I'm getting the following error occasionally when running my robolectric unit test suite: java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966) at java.util.LinkedList$ListItr.next(LinkedList.java:888) at org.robolectric.shadows.ShadowResources.getOverlayedThemeValue(ShadowResources.java:294) at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:284) at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:187) at org.robolectric.shadows

Robolectric does not support API level 28

北城以北 提交于 2019-12-05 01:10:52
Although this question has been answered for previous versions in other threads, none of the answers seems to work for me with api 28 right now so.. All Robolectric tests worked fine when on api 27. Now when my app api target is 28 they all fail. I have @Config(constants = BuildConfig::class, sdk = [Build.VERSION_CODES.P]) on my test class. Diving into Robolectrics internal SdkConfig.java it looks like they add support for api 28: addSdk(Build.VERSION_CODES.P, "P", "4651975", "P"); But when evaluating that line of code in the debugger, Build.VERSION_CODES.P evaluates to 10000 . Not sure what's

Robolectric: simulate network error in test

萝らか妹 提交于 2019-12-04 16:44:40
How is it possible to produce the same exception like during a real connection-error in robolectric tests? I want to how the program acts if the network is currently not available. Is there a possibility to produce the same exception for my HttpClient? I already tried: Robolectric.getFakeHttpLayer().interceptHttpRequests(false); // with real network to a non existent IP and WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(false); and Robolectric.addPendingHttpResponse(404, null); but none of them produces the same reactions

Class loading collision between Robolectric and Powermock

别来无恙 提交于 2019-12-04 10:04:36
I'm trying to write a test that needs both Robolectric 2.2 and PowerMock , as the code under test depends on some Android libraries and third party libraries with final classes that I need to mock. Given that I'm forced to use the Robolectric test runner through: @RunWith(RobolectricTestRunner.class) ...I cannot use the PowerMock test runner, so I'm trying to go with the PowerMock java agent alternative, without luck so far. I have setup everything according to this guide but I'm facing a collision problem between classes required by the javaagent library and by robolectric through its

kotlin and ArgumentCaptor - IllegalStateException

余生颓废 提交于 2019-12-04 09:52:12
问题 I have a problem with capturing the Class argument via ArgumentCaptor. My test class looks like this: @RunWith(RobolectricGradleTestRunner::class) @Config(sdk = intArrayOf(21), constants = BuildConfig::class) class MyViewModelTest { @Mock lateinit var activityHandlerMock: IActivityHandler; @Captor lateinit var classCaptor: ArgumentCaptor<Class<BaseActivity>> @Captor lateinit var booleanCaptor: ArgumentCaptor<Boolean> private var objectUnderTest: MyViewModel? = null @Before fun setUp() {

Getting Robolectric to work with Volley

寵の児 提交于 2019-12-04 09:07:06
问题 I am trying to get Volley working with Robolectric. I can see that my HTTP request is getting called, and parseNetworkResponse is getting called (I'm sending a custom subclass of JsonRequest), but my Listener is NOT getting called. Any advice? Here is a code sample: @Test public void testTypeAheadClient() throws Exception { Robolectric.getFakeHttpLayer().interceptHttpRequests(false); //mRemoteRequestQueue and mCustomRequest are set up previously mRemoteRequestQueue.add(mCustomRequest); }

Android Unit Test with Retrofit2 and Mockito or Robolectric

帅比萌擦擦* 提交于 2019-12-04 07:28:31
问题 Can I test real response from retrofit2beta4? Do i need Mockito or Robolectic? I don't have activities in my project, it will be a library and I need to test is server responding correctly. Now I have such code and stuck... @Mock ApiManager apiManager; @Captor private ArgumentCaptor<ApiCallback<Void>> cb; @Before public void setUp() throws Exception { apiManager = ApiManager.getInstance(); MockitoAnnotations.initMocks(this); } @Test public void test_login() { Mockito.verify(apiManager)

Robolectric @Config path value for manifest

隐身守侯 提交于 2019-12-04 07:09:23
I am trying to run robolectric unit test but I am getting error as AndroidManifest.xml not found on path. Can anyone give me an example path of @Config manifest value. Is it relative path or absolute one? Thanks in advance If you are using maven to run your tests you can set as follows: @Config(manifest = "../app/AndroidManifest.xml") @RunWith(RobolectricTestRunner.class) public class SomeTestCase { ... } Please note that if you are using Android Studio/Intellij and want to run your tests within the IDE you will have to make a change in the Run configuration. In Run->Edit configuration-