我有这个模拟某些东西的单元测试代码:
import org.junit.Assert;
import org.junit.Test;
import mockit.*;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;
@RunWith(ZohhakRunner.class)
public class PersonTest{
@Test
public void testGetName(@Capturing final Person p) {
// I need P object for mocking;
Assert.assertTrue(true);
}
@TestWith({
"1, 1",
"2, 2"
})
public void test_is_euqal(int input, int expected, @Capturing final Person p){
// ... some code here ...
// I need P Object for mocking but don't want to use it as parametrized
Assert.assertEquals(Integer.valueOf(expected), Integer.valueOf(input));
}
}
第一个测试用例 (testGetName
) 没问题,但第二个测试抛出 ArrayIndexOutOfBoundsException
异常,因为我在参数中使用了 @capturing
。有没有一种方法可以向测试用例发送参数并且不对其进行参数化
而不抛出任何ArrayIndexOutOfBoundsException
异常?
最佳答案
简短回答:你不能那样做。
更长的答案:
事实上,您尝试自动组合 2 个不同的运行者,这通常在 junit 中是不可能的。我能想到的最简单的解决方法是:
- 在没有
@Capturing
注释的情况下手动在测试方法中创建模拟 - 在字段而不是参数上使用
@Capturing
(我知道,它会变得困惑) - 以上两者的结合
关于java - 如何使用额外的 mockito 参数测试参数化单元测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993166/