Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnitParams with Mockito

This could be a XY problem but I want to ask:

I am using JUnitParams to be able to run my test method 10 times with different objects. The problem is that the injections don't work (@Mock and @InjectMocks). I can use

PersonService personService = mock(PersonServiceImpl.class)

to mock the PersonService in the @Test method. But how do mock the "main" object that is annotated with @InjectMock? Maybe it is not possible and JUnitParams was only created for simple test cases that they exemplify with on their site.

If I can't use JUnitParams, what can I do instead? I have tried

@RunWith(value = Parameterized.class)

but that doesn't work either.. I think..

What I have:

@RunWith(JUnitParamsRunner.class)
public class SkapaKundMasterDataTest {

    @InjectMocks
    private SkapaKundMasterData batch;

    @Mock
    private PersonService personService;

    @Mock
    private UtbetalningService utbetalningService;

    @Mock
    private Användare user;

    public Collection<PersonA[]> paramz() {

        List<PersonA[]> params = new ArrayList<>();
        params.add(new PersonA[] {new PersonA(new PersonId(1111111111111L), new Personnummer(195001019999L), Datum.skapaDatum(1950, 1, 1), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(2222222222222L), new Personnummer(195102029999L), Datum.skapaDatum(1951, 2, 2), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null),});
        params.add(new PersonA[] {new PersonA(new PersonId(3333333333333L), new Personnummer(195203039999L), Datum.skapaDatum(1952, 3, 3), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(4444444444444L), new Personnummer(195304049999L), Datum.skapaDatum(1953, 4, 4), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(5555555555555L), new Personnummer(195405059999L), Datum.skapaDatum(1954, 5, 5), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(6666666666666L), new Personnummer(195506069999L), Datum.skapaDatum(1955, 6, 6), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(7777777777777L), new Personnummer(195607079999L), Datum.skapaDatum(1956, 7, 7), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(8888888888888L), new Personnummer(195708089999L), Datum.skapaDatum(1957, 8, 8), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(9999999999999L), new Personnummer(195809099999L), Datum.skapaDatum(1958, 9, 9), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});
        params.add(new PersonA[] {new PersonA(new PersonId(1234567891234L), new Personnummer(195910109999L), Datum.skapaDatum(1959, 10, 10), AllaArbetstagarFörEnPerson.utanArbetstagare(), null, null, null, null, null, false, null)});

        return params;
    }

    @Test
    @Parameters(method = "paramz")
    public void alltOk(PersonA dummy) throws BaseException {
        MockitoAnnotations.initMocks(SkapaKundMasterDataTest.class);
        //PersonService personService = mock(PersonServiceImpl.class); //this works
        when(personService.HämtaPersonAFörKundMasterdata(any(), any())).thenReturn(Optional.of(dummy));

        Properties p = new Properties();
        p.put("skummisar", "Ja");
        JobbParametrar params = JobbParametrar.skapa(p);

        batch.uppgift(dummy.getPersonId(), params, new KörningsId(0), any());
    }
}
like image 304
Jack Flamp Avatar asked Sep 20 '17 07:09

Jack Flamp


People also ask

Is Mockitoannotations initMocks deprecated?

initMocks. Deprecated. Use openMocks(Object) instead. This method is equivalent to openMocks(testClass).

What is Mockitorule?

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.


1 Answers

You cannot have two different runners simultaneously, but you can setup Mockito with a Rule. Just add the following to your test class while keeping the JUnitParamsRunner:

@Rule
public MockitoRule rule = MockitoJUnit.rule();
like image 108
Absurd-Mind Avatar answered Sep 29 '22 12:09

Absurd-Mind