Jmockit is a mock framework for Java unit tests above Google code.
Introduction to the elements involved in Jmockit: @Tested and @injectable
The @tested object is judged to be null and is initialized by the appropriate constructor, and an instance of @Injectable is automatically injected into the @tested.
@Mocked
@Mocked decorated instance, will mock all the behavior of all instances of the class for each instance (whether the constructor method or the Private,protected method).
Expectations
A block that is expected to occur, typically defined by the expectations class and the Nonstrictexpectations class, defined by the expectations class, is run with a mock object that is only The order defined in the expectations block calls the method in turn, can not be called more or less, so it is possible to omit the verifications block, while the Nonstrictexpectations class does not have these limitations, so if validation is required, You are adding a verifications block. Result and times are their default member variables. Result can redefine the return value of the recording behavior and even redefine the behavior by delegate, which is the number of times the recording behavior is expected to occur. Calls that occur in expectations are mocks.
Deencapsulation
Used to mock private methods and private properties.
Simple example:
Package Com.zero.jmockit;
public class Utils {
private static String url_string = "http://www.sina.com.cn";
private static void Deal () {
System.out.println ("Deal ()" + url_string);
}
public static String GetObject (Jmockitobject object, String str) {
deal ();
System.out.println ("GetObject ()");
return url_string + str;
}
}
Package Com.zero.jmockit;
public class UserService {public
String getusername (int id) {return
"Hello";
}
}
package Com.zero.jmockit;
Import Javax.annotation.Resource;
public class Jmockitobject {@Resource private userservice userservice;
public void Dealuser () {String name = Userservice.getusername (1);
System.out.println ("Userservice.getusername ():" + name);
SYS (name);
private void Sys (String name) {System.out.println ("sys ():" + name);
Public String getString () {GetObject (this, "/zero");
Return Utils.getobject (This, "/zero");
} private void GetObject (Jmockitobject object, String str) {System.out.println ("--------"); }
}
Package Com.zero.jmockit;
Import org.junit.*;
Import Org.junit.runner.RunWith;
Import mockit.*;
Import Mockit.integration.junit4.JMockit;
@RunWith (jmockit.class) public final class Jmockittest {@Tested @Mocked private jmockitobject jmockitobject;
@Injectable private UserService UserService; @Test public void Testdealuser () {new nonstrictexpectations () {///recording expected impersonation behavior {userservice.getusername (anyint);//
Mock public method result = "zero";//can also make returns ("zero");
times = 1; Deencapsulation.invoke (jmockitobject, "sys", anystring);//mock Private method/sys () has no return value and no result is defined, then the results of the call return empty ull;
Can write does not write result = null;
}
}; Jmockitobject.dealuser ()//playback} @Test public void Testsys () {Deencapsulation.invoke (jmockitobject, "sys", "Hello Wo Rld! ");
/test Private method} @Test public void Teststaticfield () {//mock static private property new Nonstrictexpectations (Utils.class) {{
Deencapsulation.setfield (Utils.class, "url_string", "www.baidu.com"); }
}; The second way://New Mockup<utils> () {//{///////////Deencapsulation.setfield (Utils.class, "url_string")
,//"www.baidu.com");
// }
// };
String str = Deencapsulation.getfield (Utils.class, "url_string");
Assert.assertequals ("www.baidu.com", str); @Test public void Teststaticfunction () {//mock static Private method new Mockup<utils> () {@Mock private void deal ()
{//Note that the method in mockup has the same signature as the mock method, except that it does not contain a static keyword, and uses @mock callout System.out.println ("Mock Deal ()");
}
};
Deencapsulation.invoke (Utils.class, "deal"); @Test public void testgetstring () {new nonstrictexpectations () {Deencapsulation.invoke () {jmockitobject, "G Etobject ", Withany (Jmockitobject.class), Withany (String.class));//You cannot use a parameter such as any, or you must use the correct type of argument;//Withany (Jmocki Tobject.class), anystring);
can};
New Nonstrictexpectations (Utils.class) {{utils.getobject (jmockitobject) any, anystring);
Returns ("Zero");
}
}; Assert.assertEquals ("Zero", jmockitobject.getstring ());
}
}