JMockit:使用 MockUp mock 函数行为


#Java Jmockit


JMockit 配置和运行环境

示例

要测试的类

package demo;

public class Calculator {

    // 非静态函数
    public int add(int a, int b) {
        return a+b;
    }

    // 静态函数
    public static int staticAdd(int a, int b) {
        return a+b;
    }

    // 空函数
    public void noop() {
        System.out.println("没有被 mock");
    }

}

测试:mock 非静态函数返回值

package demo;

import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void test_add_01() {
        System.out.println("--test_add_01--");
        Calculator calculator = new Calculator();
        System.out.println("mock 之前");
        System.out.println(calculator.add(1, 2));

        // mock
        MockUp<Calculator> mock = new MockUp<Calculator>() {
            @Mock
            public int add(int a, int b) {
                return -1;
            }
        };

        System.out.println("mock 之后");
        System.out.println(calculator.add(1, 2));

    }

    @Test
    public void test_add_02() {
        System.out.println("--test_add_02--");
        Calculator calculator = new Calculator();
        System.out.println(calculator.add(1, 2));
    }

}

单测执行结果:

--test_add_01--
mock 之前
3
mock 之后
-1
--test_add_02--
3

测试:mock 静态函数返回值

和 非静态函数一样,但在 MockUp 中不要加 static 关键词。

package demo;

import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void test_staticAdd_01() {
        System.out.println("--test_add_01--");
        System.out.println("mock 之前");
        System.out.println(Calculator.staticAdd(1, 2));

        // mock
        new MockUp<Calculator>() {
            @Mock
            public int staticAdd(int a, int b) {
                return -1;
            }
        };

        System.out.println("mock 之后");
        System.out.println(Calculator.staticAdd(1, 2));
    }

    @Test
    public void test_staticAdd_02() {
        System.out.println("--test_add_02--");
        System.out.println(Calculator.staticAdd(1, 2));
    }

}

执行结果:

--test_staticAdd_01--
mock 之前
3
mock 之后
-1
--test_staticAdd_02--
3

测试:一个单测内多次 mock

package demo;

import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void test_add_01() {
        Calculator calculator = new Calculator();
        System.out.println("mock 之前");
        System.out.println(calculator.add(1, 2));

        // mock
        new MockUp<Calculator>() {
            @Mock
            public int add(int a, int b) {
                return -1;
            }
        };

        System.out.println("mock 之后");
        System.out.println(calculator.add(1, 2));

        // 再次 mock
        new MockUp<Calculator>() {
            @Mock
            public int add(int a, int b) {
                return -100;
            }
        };
        System.out.println("再次 mock 之后");
        System.out.println(calculator.add(1, 2));
    }

}

执行结果:

mock 之前
3
mock 之后
-1
再次 mock 之后
-100

测试:mock 无参函数

package demo;

import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void test_noop_01() {
        System.out.println("--test_noop_01--");
        System.out.println("mock 之前");
        Calculator calculator = new Calculator();
        calculator.noop();

        // mock
        new MockUp<Calculator>() {
            @Mock
            public void noop() {
                System.out.println("被 mock 了");
            }
        };

        System.out.println("mock 之后");
        calculator.noop();
    }

    @Test
    public void test_noop_02() {
        System.out.println("--test_noop_02--");
        Calculator calculator = new Calculator();
        calculator.noop();
    }

}

执行结果:

--test_noop_01--
mock 之前
什么都不做
mock 之后
被 mock 了
--test_noop_02--
什么都不做

测试:mock 抛出异常

package demo;

import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void test_noop_01() {
        System.out.println("--test_noop_01--");
        System.out.println("mock 之前");
        Calculator calculator = new Calculator();
        calculator.noop();

        // mock
        new MockUp<Calculator>() {
            @Mock
            public void noop() {
                throw new RuntimeException("xxx");
            }
        };

        System.out.println("mock 之后");
        calculator.noop();
    }

}

执行结果:

--test_noop_01--
mock 之前
没有被 mock
mock 之后

xxx
java.lang.RuntimeException: xxx
	at demo.CalculatorTest$1.noop(CalculatorTest.java:20)
	at demo.CalculatorTest.test_noop_01(CalculatorTest.java:25)
	at java.base/java.lang.Thread.run(Thread.java:829)

测试:特定情况下调用原始逻辑

package demo;

import mockit.Invocation;
import mockit.Mock;
import mockit.MockUp;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void test_add_01() {
        Calculator calculator = new Calculator();

        // mock
        new MockUp<Calculator>() {
            @Mock
            public int add(Invocation invocation, int a, int b) {
                // 当 a == 1 时,调用原始逻辑
                if (a == 1) {
                    return invocation.proceed(a, b);
                }
                return 1;
            }
        };

        System.out.println(calculator.add(1, 2));
        System.out.println(calculator.add(2, 1));
    }

}

执行结果:

3
1


( 本文完 )