示例:
package demo;
import mockit.Delegate;
import mockit.Expectations;
import org.junit.Test;
public class CalculatorTest {
@Test
public void test_add_01() {
Calculator calculator = new Calculator();
new Expectations(Calculator.class) {{
calculator.add(anyInt, anyInt);
result = new Delegate<Calculator>() {
public int delegate(int a, int b) {
if (a == 1) {
return 1;
}
return 100;
}
};
}};
System.out.println(calculator.add(1, 2)); // 1
System.out.println(calculator.add(2, 2)); // 100
}
}