mik*_*ent 12 java junit rule mockito parameterized-tests
这是从这个问题开始的:我被要求开始一个新问题.
问题是我对JUnit不够了解Rule,或者在这里发生了什么Runners等等,以Jeff Bowman提到的方式解决问题.
Jef*_*ica 26
在你后来的评论中,我找出了差距:你需要使用Mockito作为规则并参数化为跑步者,而不是相反.
原因是Runner负责报告测试次数,Parameterized根据测试方法的数量和参数化输入的数量来操作测试次数,因此参数化成为Runner过程的一部分非常重要.相比之下,使用亚军的Mockito或规则的仅仅是封装@Before和@After用于初始化注释的Mockito和验证使用的Mockito,它可以很容易实现的方法,@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));
}
}
Run Code Online (Sandbox Code Playgroud)