First look at a set of comparisons, for example, we want to test the result is a linearlaout
-
ASSERTJ Android:
Assertthat(Layout).isVisible() .isvertical() .Haschildcount(4) .hasshowdividers(Show_dividers_middle);
-
Regular JUnit:
assertequals(View.VISIBLE, Layout.getvisibility());assertequals(VERTICAL, Layout.getorientation());assertequals(4, Layout.Getchildcount());assertequals(Show_dividers_middle, Layout.getshowdividers());
-
Regular ASSERTJ:
Assertthat(Layout.getvisibility()).Isequalto(View.VISIBLE);Assertthat(Layout.getorientation()).Isequalto(VERTICAL);Assertthat(Layout.Getchildcount()).Isequalto(4);Assertthat(Layout.getshowdividers()).Isequalto(Show_dividers_middle);
From the form, the use of more convenient.
From the output results, the output is more directly visible.
-
ASSERTJ Android: Expected visibility <gone> but was <invisible> .
-
Regular JUnit expected:<[8]> but was:<[4]>
-
Regular ASSERTJ: expected: <8> but was: <4>
so let's take a look at how Assert Android is used. 1 First write a data structure to be testedPublic class draft{Private String id;//Unique identification ID of the draft
Private String uid;//Send account UID to distinguish drafts of different accounts
private int type;//the saved type
Private String displayedtitle;//display title content
}2 \ First we come from defining an assertion class:Public class Draftassert extends abstractassert<draftassert, Draft>{public Draftassert (Draft Draft) { Super (draft,Draftassert.class);}} Public static Draftassert assertthat (Draft Draft) { return new Draftassert (draft);}public Draftassert hasdisplayedname (String name) { isnotnull ();if (!draft.getdisplayedtitle (). Equals (name)){Failwithmessage ("expected character ' name to being <%s> but is <%s>", name,draft.getdisplayedtit Le ());)return this;}}3, the use of time draftassert.assertthat (draft). hasdisplayedname (" @ gallant "); if import draftassert. assertthat static import, you can also directly use assertthat
Use and extension of assert assert in unit tests in Android