在您以后的评论中,我指出了差距:您需要使用Mockito作为规则,而将参数化为Runner,而不是相反。
原因是Runner负责报告测试数量,而Parameterized则根据测试方法的数量和参数化输入的数量来操纵测试的数量,因此Parameterized成为Runner流程的一部分非常重要。相比之下,使用Mockito运行器或规则只是封装用于初始化Mockito批注和验证Mockito使用情况的
@Before和
@After方法,因为
@Rule与其他
@Rule实例相邻工作时,可以很容易地做到这一点-
直到MockitoJUnitRunner是几乎已弃用。
要直接从“ JUnit4参数化测试”文档页面和MockitoRule文档页面婴儿床:
@RunWith(Parameterized.class)public class YourComponentTest { @Rule public MockitoRule rule = MockitoJUnit.rule(); @Mock YourDep mockYourDep; @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] {{ 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } }); } private int fInput; private int fExpected; public YourComponentTest(int input, int expected) { fInput = input; fExpected = expected; } @Test public void test() { // As you may surmise, this is not a very realistic example of Mockito's use. when(mockYourDep.calculate(fInput)).thenReturn(fExpected); YourComponent yourComponent = new YourComponent(mockYourDep); assertEquals(fExpected, yourComponent.compute(fInput)); }}


