robolectric

Android Robolectric Click RecyclerView Item

人走茶凉 提交于 2019-11-30 11:11:46
Is there any way to simulate a click on a RecyclerView item with Robolectric? So far, I have tried getting the View at the first visible position of the RecyclerView , but that is always null . It's getChildCount() keeps returning 0 , and findViewHolderForPosition is always null . The adapter returns a non-0 number from getItemCount() (there are definitely items in the adapter). I'm using Robolectric 2.4 SNAPSHOT. Seems like the issue was that RecyclerView needs to be measured and layed out manually in Robolectric. Calling this solves the problem: recyclerView.measure(0, 0); recyclerView

Testing async tasks with robolectric

霸气de小男生 提交于 2019-11-30 10:57:48
Do you know how to implement unit testing for AsyncTasks using Robolectric ? Any pointers will be appreciated. Call execute(...) on the task, then to wait for the result call Robolectric.runBackgroundTasks() / Robolectric.flushBackgroundThreadScheduler() then you can assert. @Test public void test() { //create task MyAsyncTask asyncTask = new MyAsyncTask(); //start task asyncTask.execute(...); //wait for task code // Robolectric.runBackgroundTasks(); (pre 3.0) Robolectric.flushBackgroundThreadScheduler(); //from 3.0 //can run asserts on result now assert...(asyncTask.get()); } With Robolectric

Providing test data for SharedPreferences for Robolectric

坚强是说给别人听的谎言 提交于 2019-11-30 10:48:37
问题 Just started to use Robolectric and it seems to be pretty much what I need. However, I've hit a bit of a roadblock with regards to the use of SharedPreferences. I have two tests cases Activity expects a new/empty sharedPreferences Activity expects sharedPreferences with some data in it already For Test Case 1, the tests are passing as expected, so all good :) However, for Test Case 2 I can't seem to figure out a nice way to provide Robolectric with some fake data, so the Activity is able to

Robolectric Unit Test failing with Android Studio 2.3 updates

时光总嘲笑我的痴心妄想 提交于 2019-11-30 08:58:17
All my Unit Test started throwing this error: No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml java.lang.NullPointerException at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:375) at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:117) at android.content.res.AssetManager.getResourceValue(AssetManager.java) at android.content.res.Resources.getValue(Resources.java:1347) at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:332) at android

Mock class in class under test

 ̄綄美尐妖づ 提交于 2019-11-30 08:17:34
How I can mock with Mockito other classes in my class which is under test? For example: MyClass.java class MyClass { public boolean performAnything() { AnythingPerformerClass clazz = new AnythingPerformerClass(); return clazz.doSomething(); } } AnythingPerformerClass.java class AnythingPerformerClass { public boolean doSomething() { //very very complex logic return result; } } And test: @Test public void testPerformAnything() throws Exception { MyClass clazz = new MyClass(); Assert.assertTrue(clazz.performAnything()); } Can I spoof AnythingPerformerClass for excluding unnecessary logic from

How can I shadow the PackageManager with Robolectric

做~自己de王妃 提交于 2019-11-30 08:02:38
问题 My Android application has a simple method to fire off an intent to display a URL. protected void launchBrowser(int id) { Uri uri = Uri.parse( getString( id ) ); Intent intent = new Intent( ACTION_VIEW, uri); PackageManager packageManager = getPackageManager(); List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); if (activities.size() > 0) { startActivity(intent); } else { Toast.makeText(getApplicationContext(), "ERROR - no application to display a web page", Toast

Does Robolectric support API level?

拜拜、爱过 提交于 2019-11-30 07:47:45
I have some Test which I would like to run with Robolectric, I use the 2.3-SNAPSHOT as my APP uses the ActionbarCompat i needed to use 2.3-SNAPSHOT Version as Robolectric could not find the AppCompat Themes before. So I setup the Classpath in Eclipse and I end up with this: java.lang.UnsupportedOperationException: Robolectric does not support API level 9, sorry! at org.robolectric.SdkConfig.<init>(SdkConfig.java:24) at org.robolectric.RobolectricTestRunner.pickSdkVersion(RobolectricTestRunner.java:288) at org.robolectric.RobolectricTestRunner.getEnvironment(RobolectricTestRunner.java:264) at

Testing custom Views with Robolectric

怎甘沉沦 提交于 2019-11-30 07:47:30
I'm trying to run unit tests with Robolectric 2.1.1 and I cannot get it to inflate custom layouts (e.g. ViewPagerIndicator classes). Suppose this is my layout: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="test" android:id="@+id/test_test"/> <com.viewpagerindicator.CirclePageIndicator android:layout_width="fill_parent" android:layout_height="wrap_content"/> <

How to use findViewById() in robolectric

被刻印的时光 ゝ 提交于 2019-11-30 07:27:58
I simply want to test with robolectric if a certain view is visible in a fragment. My unit test looks like this: ActivityController controller = Robolectric.buildActivity(FragmentActivity.class); FragmentActivity activity = (FragmentActivity) controller.create().start().resume().visible().get(); F fragment = new MyFragment(); activity.getSupportFragmentManager().beginTransaction() .add(fragment, FRAGMENT_TAG).commit(); View view = fragment.getView().findViewById(R.id.my_view); assertEquals(view.getVisibility(), View.VISIBLE); I'm using the latest android gradle plugin 1.1.3, robolectirc 2.4

In Robolectric, how do I get around DrawerLayout must be measured with MeasureSpec.EXACTLY error?

こ雲淡風輕ζ 提交于 2019-11-30 06:18:47
I'm trying to add a DrawerLayout to my application for navigation, and I'm testing my application with Robolectric (I've seen this problem both with Robolectric 2.1.1 and 2.1-SNAPSHOT - 2.2-20130712.161723-17) I'm using the following xml for the base of my drawer layout. I encounter the error in Robolectric when I have the andioid:layout_width or android:layout_height set to be "match_parent" on the Drawer Layout. <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android