Skip to content

Instantly share code, notes, and snippets.

@up1
Last active February 12, 2021 17:13
Show Gist options
  • Save up1/20725a7e236465b4509f21f025a4054a to your computer and use it in GitHub Desktop.
Save up1/20725a7e236465b4509f21f025a4054a to your computer and use it in GitHub Desktop.
Android Testing :: Custom View with Robolectric
dependencies {
...
testCompile "org.robolectric:robolectric:3.1.1"
...
}
<?xml version="1.0" encoding="utf-8"?>
<com.workshop.customviewdemo.view.LoadingView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading_view_spinner"
android:visibility="visible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="10dip"
>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:paddingLeft="10dip"
/>
</LinearLayout>
<TextView
android:id="@+id/loading_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
</com.workshop.customviewdemo.view.LoadingView>
public class LoadingView extends RelativeLayout {
public LoadingView(Context context) {
super(context);
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LoadingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void stopLoadingAndSetText(int resourceId) {
TextView textView = (TextView) findViewById(R.id.loading_text_view);
textView.setText(resourceId);
textView.setVisibility(View.VISIBLE);
findViewById(R.id.loading_view_spinner).setVisibility(View.GONE);
}
}
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class LoadingViewTest {
private LoadingView loadingView;
private View loadingSpinner;
private TextView loadingTextView;
@Before
public void setUp() throws Exception {
ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class);
loadingView = (LoadingView) LayoutInflater.from(activityController.get()).inflate(R.layout.loading_view, null);
loadingSpinner = loadingView.findViewById(R.id.loading_view_spinner);
loadingTextView = (TextView) loadingView.findViewById(R.id.loading_text_view);
}
}
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class LoadingViewTest {
...
@Test
public void testStopLoadingAndSetTextShouldHideTheSpinnerAndShowTheTextView() throws Exception {
assertThat(loadingSpinner.getVisibility(), equalTo(View.VISIBLE));
assertThat(loadingTextView.getVisibility(), equalTo(View.INVISIBLE));
loadingView.stopLoadingAndSetText(R.string.unit_tests_ftw);
assertThat(loadingSpinner.getVisibility(), equalTo(View.GONE));
assertThat(loadingTextView.getVisibility(), equalTo(View.VISIBLE));
}
}
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class LoadingViewTest {
...
@Test
public void testStopLoadingAndSEtTextShouldSetTheTextOnTheTextView() {
loadingView.stopLoadingAndSetText(R.string.unit_tests_ftw);
assertThat((String) loadingTextView.getText(), equalTo("Unit Tests FTW!!!"));
}
}
@trietbui85
Copy link

Do you have an improved version with Kotlin & Mockk?

@up1
Copy link
Author

up1 commented Jun 26, 2020

improved

Still in progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment