Use and extension of assert assert in unit tests in Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.