Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

+1 vote
22.6k views
in Technique[技术] by (71.8m points)

JMockit对可变参数接口mock时无法正确的返回预设值的问题记录

最近在写测试用例时发现,如果测mock的接口中使用了可变参数,且mock时又使用了any来判定可变参数时,Jmockit的返回结果将不符合预期。 如果将可变参数改成显示的数组格式,Jmockit又会正常 举例如下:

public class TestUtil {
    public static String testVariablePara(int index, String... columns) {
        return "testVariablePara";
    }

    /////////////////////////////////////////////
    private TestUtil() {
    }
}


public class TestBiz {
    public String getCalcuValByColume() {
        String val1 = TestUtil.testVariablePara(1, "a", "b", "c");
        String val2 = TestUtil.testVariablePara(2, "a", "b", "c");
        return val1 + "_" + val2;
    }
} 

import mockit.Expectations;
import mockit.Mocked;
import org.junit.Assert;
import org.junit.Test;

public class TestBizTest {
    private TestBiz testBiz = new TestBiz();
    @Mocked
    private TestUtil testUtil;

    private void expectGetCal(int index, String returnVal) {
        new Expectations() {
            {
                TestUtil.testVariablePara(index, (String) any, (String) any, (String) any);
                result = returnVal;
            }
        };
    }

    @Test
    public void testVariablePara() {
        expectGetCal(1, "val1");
        expectGetCal(2, "val2");
        Assert.assertEquals("val1_val2", testBiz.getCalcuValByColume());
    }
}

运行结果:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (1.4k points)
 
Best answer
有收获,谢谢分享。。。。

这个人很懒,什么也没留下...
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...