robolectric

Testing a CursorLoader with Robolectric & Mockito

谁说胖子不能爱 提交于 2019-12-21 12:10:37
问题 Given I'm developing a simple ListFragment (in this case, it reads a list of Artists from the MediaStore, but will also read data from a different source later) like this: @EFragment public class ArtistsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { private static final String TAG = ArtistsFragment.class.getName(); private SimpleCursorAdapter mAdapter; Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI; CursorLoader mCursorLoader; @AfterViews void

getSupportActionBar() returns null with Robolectric

不问归期 提交于 2019-12-21 12:09:20
问题 The method getSupportActionBar() returns null when i invoke it through a Test Case based in Roboelectric and JUnit. This is my simple test case: package com.mobile.test; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import android.app.Activity; import android.content

Use in process-build disappeared in Android Studio v1.3

泪湿孤枕 提交于 2019-12-21 05:39:13
问题 I have recently updated my Android Studio to 1.3 in Beta channel, and I noticed that Use in process-build has been disappeared from Compiler -> Gradle settings menu. Then I wonder how can I debug my Robolectric test considering a known issue as explained in How to debug when running Robolectric tests in Android Studio? and http://www.culmination.org/2014/03/debugging-during-robolectric-test-execution-in-android-studio/ I receive java.net.SocketException "Socket closed" error and I do not know

Robolectric + rxJava + retrofit Second call throws java.io.InterruptedIOException

巧了我就是萌 提交于 2019-12-21 05:04:16
问题 I am developing and android app. I am using retrofit (with OkClient) for api requests and Robolectric for testing. My api looks like this: @GET("/v1/book/{bookId}") Observable<Book> getBook(@Path("bookId") int bookId); Just for Robolectric I am forcing api calls to be synchronous. The restAdapter builder looks like this: RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(environment.getServerEndpoint()) .setClient(new OkClient(client)) .setExecutors(new ImmediateExecutor(),

How to turn on robolectric logging

谁说胖子不能爱 提交于 2019-12-21 04:03:24
问题 I need a very simple way to get Robolectric 3.0 to turn on logging. I want to see the output from robolectric, not just my tests. Everything I tried off the web is not working. Where do I stick this? robolectric.logging.enabled = true I tried the following: In a robolectric.properties file in test/java/res In a robolectric.properties file in test/java/resources In a robolectric.properties file in test/res In a robolectric.properties file in test/resources In gradle: afterEvaluate { project

PowerMockito: NotAMockException on a mock

房东的猫 提交于 2019-12-21 03:45:41
问题 Bit of a complicated setup. Robolectric, PowerMockito rule-based config. @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) // Using "PrepareOnlyThis" prevents powermock from trying to instrument the whole hierarchy, // part of which we've ignored (android.os.* in this case) @PrepareOnlyThisForTest({ServiceCallbackBase.class}) // this class extends Handler, // so we need

Android Robolectric unit test for Marshmallow PermissionHelper

旧巷老猫 提交于 2019-12-21 03:38:23
问题 I wanna learn Robolectric to use it for unit tests on an Android Marshmallow app. I wrote a PermissionHelper with some methods to make permission handling a bit easier. To get started with unit tests for this class, I am trying to test the most simple method: public static boolean hasPermissions(Activity activity, String[] permissions) { for (String permission : permissions) { int status = ActivityCompat.checkSelfPermission(activity, permission); if (status == PackageManager.PERMISSION_DENIED

Mock HttpResponse with Robolectric

匆匆过客 提交于 2019-12-20 19:08:08
问题 Using Robolectric 2.3-SNAPSHOT, I want to test an object that'll execute a request in the background. In order to isolate it, I'm trying to mock the HttpResponse returned, without success after some hours invested. I've created a project that anyone can clone. Simly run ./gradlew check https://github.com/Maragues/RobolectricDummyProject (git clone https://github.com/Maragues/RobolectricDummyProject.git) I've tried Robolectric.setDefaultHttpResponse(200, "my_mocked_word"); MockWebServer (https

ClassCastException: NoClassDefFoundError cannot be cast to RuntimeException

旧街凉风 提交于 2019-12-20 18:26:09
问题 I am working on upgrading my codebase to Gradle 2.2 and Android Studio 1.0. I am currently trying to get Robolectric 2.4 working, but I am experiencing a strange issue when I try and run Unit Tests. The issue occurs only after a gradle clean ; running the test suite multiple times will produce passing tests (as expected). When I run the tests after a clean, I get the following error: java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException I

Robolectric not using test application

爷,独闯天下 提交于 2019-12-20 12:38:33
问题 According to this link I can create a test application which Robolectric will automatically start using in tests. I cannot get this to work. I am using Dagger for dependency injection and have created injection wrapper classes for Activity and Application . Then each of my activities extends the wrapper activity class instead of plain old Activity . The problem I am having is that, in tests, the dependencies provided by the Application modules can't be resolved and so the tests fail. This is