简体   繁体   中英

JMockit mock methods of the target class

I am new to JMockit. How can I mock method2() while writing tests for method1() using Expectations or any better approach. I used expectation to mock dependency classes.

public class A {

Dependency dep = new Dependency();
public int method1(int val){
    //some business logic
    //....
    //.....
    dep.someMethod();
    int ret = method2(val);
    return ret;
}

public int method2(int val) {
    //some business logic
    //....
    //.....
    return val;
}

}

Create Mockup of method2 and return based on the test case you testing for.

new MockUp<A>() {

            @Mock
           int method2(int val) // no access modifier required
            {
                return yourRequiredOutput;
            }
}
 new Expectations() {

            {
                depMock.someMethod();
            }
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM