Jmockit:Expectations 通过 Delegate 定制返回值


#Java Jmockit


示例:

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
    }
}


( 本文完 )