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