Issue
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());
}
}
Solution
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();
Answered By - Absurd-Mind