Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized testing with Mockito by using JUnit @Rule?

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

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.

like image 234
mike rodent Avatar asked Nov 24 '16 19:11

mike rodent


People also ask

Can we use JUnit and Mockito together?

In this chapter, we'll learn how to integrate JUnit and Mockito together. Here we will create a Math Application which uses CalculatorService to perform basic mathematical operations such as addition, subtraction, multiply, and division. We'll use Mockito to mock the dummy implementation of CalculatorService.

What is rule in Mockito?

Mockito JUnit Rule helps keeping tests clean. It initializes mocks, validates usage and detects incorrect stubbing. Make sure to configure your rule with strictness(Strictness) which automatically detects stubbing argument mismatches and is planned to be the default in Mockito v3. Since Mockito 2.1.

Which annotation is used to run parameterized test in JUnit?

Here, we see that the test uses the @RunWith annotation to specify that the test will run with the Junit4 Parameterized runner. This runner knows to look for a method that will provide the value-set for the test (annotated with @Parameters), initialize the test properly, and run the tests with multiple rows.


1 Answers

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.

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.

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));
    }
}
like image 157
Jeff Bowman Avatar answered Oct 01 '22 08:10

Jeff Bowman