Writing tests is not the most glamorous part of developing an Android application but it is an invaluable one. Using libraries like JUnit and Truth provide a great starting point for writing tests.
This library is an extension of Truth which aims to make it even easier to test Android.
assertThat(view).isGone();
assertEquals(View.GONE, view.getVisibility());
assertThat(view.getVisibility()).isEqualTo(View.GONE);
When failing, the Truth Android assertion produces an output which allows you to immediately recognize the problem: Expected visibility <gone> but was <invisible>.
Compare that to the output of regular Truth expected:<[8]> but was:<[4]> and regular JUnit expected: <8> but was: <4> and you should immediately see the advantage.
Because Truth Android offers assertions directly on objects rather than properties, tests are much more readable.
assertThat(layout).isVisible(); assertThat(layout).isVertical(); assertThat(layout).hasChildCount(4); assertThat(layout).hasShowDividers(SHOW_DIVIDERS_MIDDLE);
assertEquals(View.VISIBLE, layout.getVisibility()); assertEquals(VERTICAL, layout.getOrientation()); assertEquals(4, layout.getChildCount()); assertEquals(SHOW_DIVIDERS_MIDDLE, layout.getShowDividers());
assertThat(layout.getVisibility()).isEqualTo(View.VISIBLE); assertThat(layout.getOrientation()).isEqualTo(VERTICAL); assertThat(layout.getChildCount()).isEqualTo(4); assertThat(layout.getShowDividers()).isEqualTo(SHOW_DIVIDERS_MIDDLE);
Assertions exist for nearly every object that you would ever want to test, from LinearLayout to ActionBar to Fragment to MenuItem. AndroidX is mostly included too.
To get started writing tests add the following import:
import static com.pkware.truth.android.Assertions.assertThat;
Modules are also provided for the add-on AndroidX libraries. Add the dependency (listed below) and use the following imports:
legacy-support-core-ui
import static com.pkware.truth.androidx.legacy.Assertions.assertThat;
collection
import static com.pkware.truth.androidx.collection.Assertions.assertThat;
core
import static com.pkware.truth.androidx.core.Assertions.assertThat;
cursoradapter
import static com.pkware.truth.androidx.cursoradapter.Assertions.assertThat;
fragment
import static com.pkware.truth.androidx.fragment.Assertions.assertThat;
Google Play Services
import static com.pkware.truth.android.playservices.Assertions.assertThat;
appcompat
import static com.pkware.truth.androidx.appcompat.Assertions.assertThat;
Material Components
import static com.pkware.truth.android.material.Assertions.assertThat;
loader
import static com.pkware.truth.androidx.loader.Assertions.assertThat;
mediarouter
import static com.pkware.truth.androidx.mediarouter.Assertions.assertThat;
gridlayout
import static com.pkware.truth.androidx.gridlayout.Assertions.assertThat;
cardview
import static com.pkware.truth.androidx.cardview.Assertions.assertThat;
recyclerview
import static com.pkware.truth.androidx.recyclerview.Assertions.assertThat;
pallete
import static com.pkware.truth.androidx.palette.Assertions.assertThat;
import static com.pkware.truth.androidx.print.Assertions.assertThat;
slidingpanelayout
import static com.pkware.truth.androidx.slidingpanelayout.Assertions.assertThat;
swiperefreshlayout
import static com.pkware.truth.androidx.swiperefreshlayout.Assertions.assertThat;
viewpager
import static com.pkware.truth.androidx.viewpager.Assertions.assertThat;
The provided assertions have also been designed to be extended for any custom controls you have developed.
public class CustomLayout extends LinearLayout {
public int getBehavior() {
/* ... */
}
}
Use the following pattern to set up your assertions.
public final class CustomLayoutSubject extends AbstractLinearLayoutSubject<CustomLayout> {
@Nullable
private final CustomLayout actual;
public static CustomLayoutSubject assertThat(@Nullable CustomLayout actual) {
return assertAbout(CustomLayoutSubject::new).that(actual);
}
private CustomLayoutSubject(FailureStrategy failureStrategy, @Nullable CustomLayout actual) {
super(failureStrategy, actual);
this.actual = actual;
}
public void hasSomeBehavior() {
check("getBehavior()")
.withMessage("Expected some behavior but was doing other behavior.")
.that(actual.getBehavior())
.isEqualTo(42);
}
}
Now static import CustomLayoutSubject.assertThat in your test classes.
For more information about writing custom assertions see the official documentation.
Android module:
androidTestCompile 'com.pkware.truth-android:truth-android:2.0.0'legacy-support-core-ui module:
androidTestCompile 'com.pkware.truth-android:truth-android-legacy-support-core-ui:2.0.0'collection module:
androidTestCompile 'com.pkware.truth-android:truth-android-collection:2.0.0'core module:
androidTestCompile 'com.pkware.truth-android:truth-android-core:2.0.0'cursoradapter module:
androidTestCompile 'com.pkware.truth-android:truth-android-cursoradapter:2.0.0'fragment module:
androidTestCompile 'com.pkware.truth-android:truth-android-fragment:2.0.0'Google Play Services module:
androidTestCompile 'com.pkware.truth-android:truth-android-play-services:2.0.0'Material Components library module:
androidTestCompile 'com.pkware.truth-android:truth-android-material:2.0.0'appcompat module:
androidTestCompile 'com.pkware.truth-android:truth-android-appcompat:2.0.0'loader module:
androidTestCompile 'com.pkware.truth-android:truth-android-loader:2.0.0'mediarouter module:
androidTestCompile 'com.pkware.truth-android:truth-android-mediarouter:2.0.0'gridlayout module:
androidTestCompile 'com.pkware.truth-android:truth-android-gridlayout:2.0.0'cardview module:
androidTestCompile 'com.pkware.truth-android:truth-android-cardview:2.0.0'recyclerview module:
androidTestCompile 'com.pkware.truth-android:truth-android-recyclerview:2.0.0'pallete module:
androidTestCompile 'com.pkware.truth-android:truth-android-pallete:2.0.0'print module:
androidTestCompile 'com.pkware.truth-android:truth-android-print:2.0.0'slidingpanelayout module:
androidTestCompile 'com.pkware.truth-android:truth-android-slidingpanelayout:2.0.0'swiperefreshlayout module:
androidTestCompile 'com.pkware.truth-android:truth-android-swiperefreshlayout:2.0.0'viewpager module:
androidTestCompile 'com.pkware.truth-android:truth-android-viewpager:2.0.0'