Two. More usage of the Android unit test Mockito

Source: Internet
Author: User

A. The simple use of the Android unit test Mockito
Three. Android unit test Powermock assign values to private variables
Four. Android unit test Powermock mock static method

In contrast to the previous article, this article introduces more Mockito usage, and controls Meetingpresenterimp.java and Meetingpresenterimptest.java to see how the Android unit test case is written.

public class Meetingpresenterimp implements Meetingcontract.meetingpresenter {private Meetingcontroller meetingcontr

    Oller;

    Private Meetingaudiomanager Audiomanager;

    Private Meetingcontract.meetingview Meetingview;

    Private Meetingparticipant localparticipant; Public Meetingpresenterimp (Meetingcontract.meetingview Meetingview, Meetingcontroller Meetingcontroller,
        Meetingaudiomanager audiomanager) {this.meetingcontroller = Meetingcontroller;
        This.audiomanager = Audiomanager;
    This.meetingview = Meetingview;
    //Switch camera @Override public void Togglecamera () {Meetingcontroller.togglecamera (); }//Switch speaker @Override public void Togglespeaker () {if (Audiomanager.isspeakeron ()) {Audio
        Manager.turnoffspeaker ();
        else {audiomanager.turnonspeaker (); }//Set orientation public void setorientation (int orientation) {Meetingcontroller.setorienTation (orientation); }//Update mainview visibility public void updatemainviewvisibility (Boolean isshowallbuttons) {meetingview.updatebut
        Tonsvisibility (isshowallbuttons);
        Meetingview.updateselfsurfacevisibility (isshowallbuttons);
    Meetingview.updateselficonvisibility (isshowallbuttons);
        //update meeting times public void Updatemeetingtime (long) {Date d = new Date (time);
        SimpleDateFormat SDF = new SimpleDateFormat ("HH:mm:ss", locale.us);
        Sdf.settimezone (Timezone.gettimezone ("UTC"));
    Meetingview.updatetime (Sdf.format (d)); //Watch a participant's video source synchronized void Startwatchvideosource (meetingparticipant participant, long mainsourceptr, long
        MINSOURCEPTR) {if (participant = = NULL | | | participant.isbeingwatched ()) {return; } if (Participant.equals (localparticipant)) {Meetingcontroller.setselfpreviewview (mainsourceptr, min
        SOURCEPTR); else {MeetingcontRoller.startwatchvideosource (Participant.geturi (), mainsourceptr, 0, 960, 720, 20, 0);
    } participant.setbeingwatched (True);
 }

}
@RunWith (mockitojunitrunner.class) public class Meetingpresenterimptest {@Mock Meetingcontract.meetingview Meeti

    Ngview;

    @Mock Meetingcontroller Meetingcontroller;

    @Mock Meetingaudiomanager Meetingaudiomanager;

    Meetingpresenterimp Meetingpresenterimp;

    Private Random Random = new Random (); @Before public void SetUp () throws Exception {meetingpresenterimp = new Meetingpresenterimp (Meetingview, meet
    Ingcontroller, Meetingaudiomanager); The//Simple authentication method executes @Test public void Togglecamera () throws Exception {Meetingpresenterimp.togglecamera (

        );
    Mockito.verify (Meetingcontroller). Togglecamera ();
        ///Use when (). Thenreturn The return value of the method that controls the mock object @Test public void Should_turn_off_speaker () throws Exception {

        Mockito.when (Meetingaudiomanager.isspeakeron ()). Thenreturn (True);

        Meetingpresenterimp.togglespeaker ();
    Mockito.verify (Meetingaudiomanager). Turnoffspeaker (); /ibid., but measuredTry a different branch, where Isspeakeron () returns false @Test public void Should_turn_on_speaker () throws Exception {mockito.whe

        N (Meetingaudiomanager.isspeakeron ()). Thenreturn (false);

        Meetingpresenterimp.togglespeaker ();
    Mockito.verify (Meetingaudiomanager). Turnonspeaker (); }//test method execution is not enough, test incoming parameters are consistent @Test public void should_set_orientation () throws Exception {Meetingpresen
        Terimp.setorientation (0);
    Verify (Meetingcontroller). setorientation (0);
        ///Use the Inorder () test method to perform @Test public void should_update_main_view_visibility () throws in a specific order Exception {

        Boolean isshowallbuttons = Random.nextboolean ();
        Meetingpresenterimp.updatemainviewvisibility (isshowallbuttons);
        Inorder inorder = inorder (Meetingview);
        Inorder.verify (Meetingview). updatebuttonsvisibility (isshowallbuttons);
        Inorder.verify (Meetingview). updateselfsurfacevisibility (isshowallbuttons); Inorder.verify (Meetingview). UpdateselficoNvisibility (isshowallbuttons); //test If the value of a string after format is the same as desired, @Test public void Should_update_meeting_time_30_seconds () throws Excepti
        on {meetingpresenterimp.updatemeetingtime (30 * 1000);
    Verify (Meetingview). UpdateTime ("00:00:30"); //test If the value of a string after format is the same as desired, @Test public void Should_update_meeting_time_61_seconds () throws Excepti
        On {meetingpresenterimp.updatemeetingtime (61 * 1000);
    Verify (Meetingview). UpdateTime ("00:01:01");  ///Use Never test a method is not executed once @Test public void Should_start_watch_video_source_when_participant_is_null () throws

        Exception {meetingpresenterimp.startwatchvideosource (null, 0, 0);
        Verify (Meetingcontroller, never ()). Setselfpreviewview (Anylong (), Anylong ()); Verify (Meetingcontroller, never ()). Startwatchvideosource (AnyString (), Anylong (), Anyint (), Anyint (), Anyint (),
    Anyint (), Anyint ()); }

}
Related Article

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.