<bdo id='zTlUY'></bdo><ul id='zTlUY'></ul>

<small id='zTlUY'></small><noframes id='zTlUY'>

<i id='zTlUY'><tr id='zTlUY'><dt id='zTlUY'><q id='zTlUY'><span id='zTlUY'><b id='zTlUY'><form id='zTlUY'><ins id='zTlUY'></ins><ul id='zTlUY'></ul><sub id='zTlUY'></sub></form><legend id='zTlUY'></legend><bdo id='zTlUY'><pre id='zTlUY'><center id='zTlUY'></center></pre></bdo></b><th id='zTlUY'></th></span></q></dt></tr></i><div id='zTlUY'><tfoot id='zTlUY'></tfoot><dl id='zTlUY'><fieldset id='zTlUY'></fieldset></dl></div>

  1. <legend id='zTlUY'><style id='zTlUY'><dir id='zTlUY'><q id='zTlUY'></q></dir></style></legend>

      <tfoot id='zTlUY'></tfoot>

      使用 JUnit @Rule 使用 Mockito 进行参数化测试?

      时间:2023-09-25

      <tfoot id='ODVuF'></tfoot>
    1. <legend id='ODVuF'><style id='ODVuF'><dir id='ODVuF'><q id='ODVuF'></q></dir></style></legend>
        <bdo id='ODVuF'></bdo><ul id='ODVuF'></ul>
          <tbody id='ODVuF'></tbody>
          • <small id='ODVuF'></small><noframes id='ODVuF'>

              • <i id='ODVuF'><tr id='ODVuF'><dt id='ODVuF'><q id='ODVuF'><span id='ODVuF'><b id='ODVuF'><form id='ODVuF'><ins id='ODVuF'></ins><ul id='ODVuF'></ul><sub id='ODVuF'></sub></form><legend id='ODVuF'></legend><bdo id='ODVuF'><pre id='ODVuF'><center id='ODVuF'></center></pre></bdo></b><th id='ODVuF'></th></span></q></dt></tr></i><div id='ODVuF'><tfoot id='ODVuF'></tfoot><dl id='ODVuF'><fieldset id='ODVuF'></fieldset></dl></div>
              • 本文介绍了使用 JUnit @Rule 使用 Mockito 进行参数化测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                这是继这个问题:问我的地方开始一个新问题.

                This follows on from this question: where I am asked to start a new question.

                问题是我对 JUnit Rule 或者 Runners 之类的东西了解不够,无法解决问题由 Jeff Bowman 提及.

                The problem is that I just don't know enough about JUnit Rule, or what's going on here with Runners and the like, to crack the problem in the way alluded to by Jeff Bowman.

                推荐答案

                在您后来的评论中,我发现了差距:您需要将 Mockito 用作 Rule 并将 Parameterized 用作 Runner,而不是相反.

                In your later comments, I figured out the gap: You need to use Mockito as a Rule and Parameterized as a Runner, not the other way around.

                p>

                原因是Runner负责报告测试次数,而Parameterized根据测试方法的数量和参数化输入的数量来操纵测试的数量,所以Parameterized成为其中的一部分真的很重要亚军进程.相比之下,使用 Mockito 运行器或规则只是简单地封装了初始化 Mockito 注解和验证 Mockito 使用的 @Before@After 方法,可以做到非常很容易作为一个 @Rule 与其他 @Rule 实例相邻工作 - 以至于 MockitoJUnitRunner 是 几乎不推荐使用.

                The reason is that the Runner is responsible for reporting the number of tests, and Parameterized manipulates the number of tests based on the number of test methods and the number of parameterized inputs, so it's really important for Parameterized to be a part of the Runner process. By contrast, the use of a Mockito runner or rule is simply to encapsulate the @Before and @After methods that initialize Mockito annotations and validate Mockito usage, which can be done very easily as a @Rule that works adjacent to other @Rule instances--to the point that the MockitoJUnitRunner is very nearly deprecated.

                直接从 JUnit4 Parameterized Test 文档页面和 a href="https://static.javadoc.io/org.mockito/mockito-core/2.2.22/org/mockito/junit/MockitoRule.html" rel="noreferrer">MockitoRule 文档页面:

                To crib directly from the JUnit4 Parameterized Test doc page and MockitoRule doc page:

                @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));
                    }
                }
                

                这篇关于使用 JUnit @Rule 使用 Mockito 进行参数化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何验证是否使用 Mockito 调用了两种方法之一? 下一篇:使用 Mockito 测试 Java 的行为增强

                相关文章

                最新文章

              • <tfoot id='n26ZA'></tfoot>

                  <bdo id='n26ZA'></bdo><ul id='n26ZA'></ul>

                1. <legend id='n26ZA'><style id='n26ZA'><dir id='n26ZA'><q id='n26ZA'></q></dir></style></legend>

                    <i id='n26ZA'><tr id='n26ZA'><dt id='n26ZA'><q id='n26ZA'><span id='n26ZA'><b id='n26ZA'><form id='n26ZA'><ins id='n26ZA'></ins><ul id='n26ZA'></ul><sub id='n26ZA'></sub></form><legend id='n26ZA'></legend><bdo id='n26ZA'><pre id='n26ZA'><center id='n26ZA'></center></pre></bdo></b><th id='n26ZA'></th></span></q></dt></tr></i><div id='n26ZA'><tfoot id='n26ZA'></tfoot><dl id='n26ZA'><fieldset id='n26ZA'></fieldset></dl></div>

                    <small id='n26ZA'></small><noframes id='n26ZA'>