I am using Mockito for unit testing. I am wondering if its possible to send Parametrized input parameters with as in Junit testing
e.g
@InjectMocks MockClass mockClass = new MockClass(); @Test public void mockTestMethod() { mockClass.testMethod(stringInput); // here I want to pass a list of String inputs // this is possible in Junit through Parameterized.class.. // wondering if its can be done in Mockito }
It has a single constructor that contains the test data. It has a static method which annotated with @parameters annotation and generates and returns test data. Each test data is used as a parameter for the test method. It needs a test method that is annotated with @test annotation.
In JUnit, Parameterized tests use a special runner that ensure that the test is instantiated multiple times, so each test method is called multiple times. Mockito is a tool for writing specific unit tests, so there is no built-in ability to run the same test multiple times with different Mockito expectations.
If you want your test conditions to change, your best bet is to do one of the following:
@Test
method for each mock you want.Note that there's no prohibition on using mock objects as @Parameterized
test parameters. If you're looking to parameterize based on mocks, you can do that, possibly creating the mock and setting the expectations in a static method on the test.
Note about runners: This Parameterized test runner conflicts with Mockito's MockitoJUnitRunner: Each test class can only have one runner. You'll want to switch to @Before and @After methods or a Mockito JUnit4 rule for your setup, if you use them both.
As an example, compressed from a different answer that explains more about Parameterized runners versus JUnit rules and lifting 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 the values */ } public YourComponentTest(Parameter parameter) { /* Save the parameter to a field */ } @Test public void test() { /* Use the field value in assertions */ } }
If you are stuck with an older version of mockito where MockitoRule
isn't available, the other possibility is to initialize the mocks explicitely with MockitoAnnotations.initMocks
:
@RunWith(Parameterized.class) public class YourComponentTest { @Mock YourDep mockYourDep; @Parameter public Parameter parameter; @Parameters public static Collection<Object[]> data() { /* Return the values */ } @Before public void init() { MockitoAnnotations.initMocks(this); } @Test public void test() { /* Use the field value in assertions */ } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With