Jmockit Study Notes

Source: Internet
Author: User

1 Basic Concepts 1.1 common annotations
    • @Mocked: The modified object will be mock, and the corresponding class and instance will be affected (in the same test case)
    • @Injectable: Mock-Modified objects only
    • @Capturing: You can mock interfaces and all of their implementation classes
    • @Mock: In mockup mode, specify the method to be fake
1.2 Common classes
    • Expectations: It is expected that the specified method must be called
    • Strictexpectations: Strict expectations, the specified method must be called in order
    • Nonstrictexpectations: Non-strict expectations, call and order not required
    • Verifications: verification, generally with nonstrictexpectations to use
    • Invocation: Tool class, can get call information
    • Delegate: Specify the return value yourself, suitable for the scenario that requires parameters to determine the return value, just specify the anonymous subclass.
    • Mockup: Simulation Function implementation
    • Deencapsulation: Reflection Tool Class
2 usage examples

The existing two classes are as follows:
Simpletool.java:

 Public  class simpletool {     PublicStringfun1(String str) {return "Real:public String fun1 ("+ str +")"; }PrivateStringfun2(String str) {return "Real:private String fun2 ("+ str +")"; } PublicStringFun3(String str) {return "Real:public String fun3 ("+ str +")"; } PublicStringFun4(String str) {returnFun2 (str); }}

Usesimpletool.java:

publicclass UseSimpleTool {    publicfun1(String name) {        new SimpleTool();        return simpleTool.fun1(name);    }}
2.1 status-based2.1.1 Instance 1:mocked
 @Mocked  Simpletool Simpletool; @Test  public  void  Span class= "Hljs-title" >testexpectation () {//non-mock function returns null  new  Expectations () {simpletool.fun1 (anystring); result =  "MOCK" ; } }; System.out.println (simpletool.fun1 ()); System.out.println (Simpletool.fun3 ()); System.out.println (new  usesimpletool (). FUN1 ( "param" )); new  Verifications () {{simpletool.fun1 (anystring); Times = 2 ; } };}

Output:

MOCK
Null
MOCK

2.1.2 Instance 2:injectable
 @Injectable  Simpletool Simpletool; @Test  public  void  testexpectation  () {//the non-mock function returns null  Span class= "Hljs-keyword" >new expectations () {{simpletool.fun1 (anystring); result =  "MOCK" ; } }; System.out.println (simpletool.fun1 ()); System.out.println (Simpletool.fun3 ()); System.out.println (new  usesimpletool (). FUN1 ( "param" )); new  Verifications () {{simpletool.fun1 (anystring); Times = 1 ; } };}

Output:

MOCK
Null
Real:public String fun1 (param)

2.1.3 Results Variable Delegate
@MockedSimpletool Simpletool;@Test Public void testexpectation() {NewExpectations () {{simpletool.fun1 (anystring); result =NewDelegate<string> () { PublicStringAdelegatemethod(String str) {returnStr.equals ("Param0") ?"MOCK0":"MOCK1";        }            };    }    }; System.out.println (SIMPLETOOL.FUN1 ("Param0")); System.out.println (Simpletool.fun3 ("param")); System.out.println (NewUsesimpletool (). FUN1 ("param1"));NewVerifications () {{simpletool.fun1 (anystring); Times =2; }    };}

Output:

MOCK0
Null
MOCK1

2.2 behavior-based2.2.1 Mock Object Public method
@Test Public void Testmockup() {//Affects all instances of this class    NewMockup<simpletool> () {//non-mock function not affected        @Mock         PublicStringfun1(String str) {return "(MOCK)";    }    }; Simpletool Simpletool =NewSimpletool (); System.out.println (SIMPLETOOL.FUN1 ("param")); System.out.println (Simpletool.fun3 ("param")); Usesimpletool Usesimpletool =NewUsesimpletool (); System.out.println (USESIMPLETOOL.FUN1 ("param"));}

Output:

(MOCK)
Real:public String fun3 (param)
(MOCK)

2.2.2 Mock Object Private method
@TestpublicvoidtestMockUp() {    new MockUp<SimpleTool>(){        //未mock函数不受影响        @Mock        publicfun2(String str) {            return"(MOCK)";        }    };    new SimpleTool();    System.out.println(simpleTool.fun1("param"));    System.out.println(simpleTool.fun3("param"));    System.out.println(simpleTool.fun4("param"));}

Output:

Real:public String fun1 (param)
Real:public String fun3 (param)
(MOCK)

2.2.3 Mock interface

The existing interfaces and their implementation classes are as follows:
Simleinterface.java:

publicinterface SimpleInterface {    String getCityName();    String getAreaName();}

Simpleinterfaceimpl.java:

publicclass SimpleInterfaceImpl implements SimpleInterface {    publicgetCityName() {        return"default city";    }    publicgetAreaName() {        return"default area";    }}

Instance:

//Use injectable and mocked here for the same effect@InjectableSimpleinterface Simpleinterface;@Test Public void Testupper()throwsException {Simpleinterface mockinstance =NewMockup<simpleinterface> () {@Mock         PublicStringGetcityname() {return "Beijing (MOCK)"; }@Mock         PublicStringGetareaname() {return "Huabei (MOCK)";    }}.getmockinstance ();    System.out.println (Mockinstance.getcityname ());    System.out.println (Mockinstance.getareaname ());    System.out.println (Simpleinterface.getcityname ());    System.out.println (Simpleinterface.getareaname ()); Simpleinterfaceimpl Simpleinterfaceimpl =NewSimpleinterfaceimpl ();    System.out.println (Simpleinterfaceimpl.getcityname ()); System.out.println (Simpleinterfaceimpl.getareaname ());}

Output:

Beijing (MOCK)
Huabei (MOCK)
Null
Null
Default City
Default Area

Further research is needed on how an interface's mock affects objects of all classes that implement the interface.

Jmockit Study Notes

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.