这可能是xy问题,但我想问:
我使用JUnitParams可以在不同的对象上运行我的测试方法10次。问题是注射不起作用(@Mock
和@InjectMocks
)。我可以用
PersonService personService = mock(PersonServiceImpl.class)
以模拟
PersonService
方法中的@Test
。但是如何模拟用@InjectMock
注释的“main”对象呢?也许这是不可能的,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());
}
}
最佳答案
不能同时拥有两个不同的跑步者,但可以使用Rule设置mockito。只需将以下内容添加到您的测试类中,同时保留junitparamsrunner:
@Rule
public MockitoRule rule = MockitoJUnit.rule();