robolectric

Android: Unit testing Android applications with Robolectric and Mockito

☆樱花仙子☆ 提交于 2020-01-01 08:20:09
问题 I have a Java library that uses a few things from the Android APIs. I'd like to use Mockito to write unit tests for this library. Is there a way I can go about this? Mockito doesn't play nice on the Dalvik VM, see this post: Using Mockito with Android virtual machine UPDATE: Since this post I've discovered Robolectric, and I've had the opportunity to work out of Pivotal Labs and make some small contributions to this library. I would recommend using this over the Android testing framework

SimpleTestCursor robolectric 3.0— compilation issue

瘦欲@ 提交于 2019-12-31 04:30:10
问题 I am trying to test the cursor in my app. But for the following import I get the error message-- cannot resolve symbol tester. I am using Robolectric 3.0. Why the import is not resolving, am I missing some dependency? I have looked internet but no support. Please help. I am trying to use:this API and following is the import import org.robolectric.tester.android.database.SimpleTestCursor; These are my dependencies: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile

java.lang.IllegalStateException: Activity has been destroyed

别说谁变了你拦得住时间么 提交于 2019-12-30 10:48:11
问题 Working with Robolectric , I'm very new to android. I made a first test class using Activity. It worked nicely. Now I want make a test for fragment. @RunWith(RobolectricTestRunner.class) public class LoginFragmentTest { private LoginFragment fragment; @Before public void setup() { fragment = new LoginFragment(); startFragment(fragment); assertThat(fragment, notNullValue()); assertThat(fragment.getActivity(), notNullValue()); } private void startFragment(LoginFragment fragment) {

ShadowNetworkInfo is always TYPE_MOBILE when testing WiFi connectivity with robolectric v3.0

元气小坏坏 提交于 2019-12-30 07:05:33
问题 I'm attempting to use Robolectric (3.0 RC2) to write some offline tests for a SyncAdapter implementation in an Android app. The app will be run on SIM-less handsets in factories which will use the wifi connection. So I'd like to be able to write tests against my wifi connectivity checking code. I've written a simple test, based on examples I found online to shadow the ConnectionManager with a view to simulate being on the wifi. However, despite setting the NetworkInfo to TYPE_WIFI, the

ShadowNetworkInfo is always TYPE_MOBILE when testing WiFi connectivity with robolectric v3.0

旧城冷巷雨未停 提交于 2019-12-30 07:04:32
问题 I'm attempting to use Robolectric (3.0 RC2) to write some offline tests for a SyncAdapter implementation in an Android app. The app will be run on SIM-less handsets in factories which will use the wifi connection. So I'd like to be able to write tests against my wifi connectivity checking code. I've written a simple test, based on examples I found online to shadow the ConnectionManager with a view to simulate being on the wifi. However, despite setting the NetworkInfo to TYPE_WIFI, the

Testing that button starts an Activity with Robolectric

删除回忆录丶 提交于 2019-12-29 18:44:12
问题 Hi I have the following code: @RunWith(Test9Runner.class) public class MainActivityTest { private MainActivity activity; private Button pressMeButton; @Before public void setUp() throws Exception { activity = new MainActivity(); activity.onCreate(null); pressMeButton = (Button) activity.findViewById(R.id.button1); } @Test public void shouldUpdateResultsWhenButtonIsClicked() throws Exception { pressMeButton.performClick(); ShadowActivity shadowActivity = shadowOf(activity); Intent intent =

Robolectric 3.0 testing Vibrator service

ε祈祈猫儿з 提交于 2019-12-25 07:39:30
问题 I am in process of migrating my test cases to latest Robolectric 3.0. To test the viberator service in my app, earlier I used org.robolectric.shadows.ShadowVibrator but now I am not able to test it, even using custom shadow class. Even the Robolectric wesite is not updated and it shows the use Robolectric.shadowOf_() which does not exist. This is the link of the website, which is not updated version. Kindly guide. Following is code for custom implementation:-- The custom class:-- @Implements

Robolectric test to check if a SnackBar is shown?

醉酒当歌 提交于 2019-12-25 01:49:33
问题 I am calling the following code in my app: Snackbar.make(this, R.string.empty_task_message, Snackbar.LENGTH_LONG) .show() How can I assert that this was indeed called, inside my Robolectric test? I was thinking of something like traversing the view hierarchy and searching for a Snackbar instance and checking if the view is visible. This looks like a long standing problem in Robolectric. 回答1: Use a custom shadow: see an example in my github reop: https://github.com/cafesilencio/snackbarshadow

java.lang.NoClassDefFoundError: org/robolectric/internal/ShadowExtractor

穿精又带淫゛_ 提交于 2019-12-24 23:03:37
问题 Using testImplementation 'org.robolectric:shadows-play-services:3.4-rc2' testImplementation "org.robolectric:robolectric:3.6.1" testImplementation "com.google.android.gms:play-services-auth:$rootProject.ext.googlePlayServicesVersion" // the robolectric shadow bogusly needs this I am trying this: import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; import org.junit.Before; import org.junit.runner.RunWith; import org.robolectric

Android Roboelectric 3.0 testing next activity –choosing from multiple activities

╄→尐↘猪︶ㄣ 提交于 2019-12-24 15:00:47
问题 I have scenario like this:-- I have three activities ActivityA(launcher activity), ActivityB, ActivityC Now in Activity A I read the Application context to decide whether to start ActivityB or ActivityC. But even after setting the context value manual, the ActivityA is not updated and second test case fails. Any helps? private ActivityA activityA; private ShadowActivity shadowActivity; @Before public void setUp() { activityA = Robolectric.setupActivity(ActivityA.class); assertNotNull(