Test code with Jmockit programming

Source: Internet
Author: User

The main principle of the Jmockit test is to test the method by invoking different return values from other layered methods in the mock (virtual) test method. Use this mock method to test a hierarchy in a program individually

1, pom file introduced Jmockit package

<span style= "White-space:pre" >	</span><groupId>org.jmockit</groupId>
            < Artifactid>jmockit</artifactid>
            <version>1.19</version>
2. Adding Jmockit Annotations to the head of the test class

@Runwith (jmockit.class) Public
class test{

}

3. Mark the class to be tested with @Tested

    @Tested
    private useraction useraction;

4. Inject beans into the Useraction class with @Injectable annotations

@Injectable
    private UserService userservice;
    @Injectable
    private Loginservice loginservice;

5. Use @before annotations to mark the initialization data method used before running the test

@Before public
    void init () {
     user user=new User ();
     User.setname ("Test")
}

6. Write test function and annotate with @test

    @Test public
    void Test () {

}
The test () function is generally written in three steps:

1) Set the method and return value to be called in the method to be tested for the mock

New Expectations () {{
            loginservice.getbymemberid (MemberID); result = user;
            Logininfoservice.getpumamemberinfo (MEMBERUUID); result = Logininfo;
        }};

2) Execute the method to be tested

Useraction.login ();

3) Verify the results returned after the test method is run, mainly using the user = Withcapture () method to extract the parameters in the calling method
New Verifications () {{
   User user=null;
   Login.check (User=withcapture ());
   Assertequals ("Ce", User.getname ());}

}
Note: New verifications () {{}} is using double brackets here.

Follow these 6 steps to write a more complete Jmockit test class



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.