这可能是一个 XY 问题,但我想问:
我正在使用JUnitParams能够使用不同的对象运行我的测试方法 10 次。问题是注射不起作用(@Mock
和@InjectMocks
)。我可以用
PersonService personService = mock(PersonServiceImpl.class)
模拟方法PersonService
中的@Test
。但是如何模拟带有注释的“主要”对象@InjectMock
?也许这是不可能的,JUnitParams 只是为他们在他们的网站上举例说明的简单测试用例创建的。
如果我不能使用 JUnitParams,我能做些什么呢?我试过了
@RunWith(value = Parameterized.class)
但这也不起作用..我想..
是)我有的:
@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());
}
}