最近在写测试用例时发现,如果测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());
}
}
运行结果:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…