早上好,我是Mockito的新手,我正在尝试测试对象列表。
@Mock
private AudienceCodeTypeRepository repository;
@Mock
private Page<AudienceCodeType> page;
private AudienceCodeTypeService underTest;
@Mock
private List<AudienceCodeType> listOfObject;
List<GenericReferenceCodeInput> testObj;
public void testBulkSave() throws Exception {
AudienceCodeType obj = new AudienceCodeType();
obj.setInternalid(1L);
obj.setCode("1");
obj.setLegend("Test");
obj.setStatus("A");
obj.setAudit(new Audit());
listOfObject.add(obj);
listOfObject = Arrays.asList(obj);
doReturn(obj).when(repository).saveAll(listOfObject);
GenericReferenceCodeInput actualProduct = new GenericReferenceCodeInput();
actualProduct.setCode("1");
actualProduct.setLegend("Test");
actualProduct.setStatus("A");
testObj.add(actualProduct);
testObj = Arrays.asList(actualProduct);
List<AudienceCodeType> testObject = (List<AudienceCodeType>) underTest.bulkInsert(testObj);
assertEquals(true, obj.getClass().isInstance(actualProduct));
assertEquals(listOfObject.get(0), testObject.get(0));
assertEquals(listOfObject.get(1), testObject.get(1));
assertEquals(listOfObject.get(2), testObject.get(2));
assertEquals(listOfObject.get(3), testObject.get(3));
}
}
但我遇到错误:
org.mockito.exceptions.misusing.WrontTypeOfReturnValue saveAll()无法返回AudienceCodeType,saveAll应该返回 列表
答案 0 :(得分:0)
您应该替换
AudienceCodeType obj = new AudienceCodeType();
与
List<AudienceCodeType> obj = Collections.singletonList(newAudienceCodeType());
答案 1 :(得分:0)
编辑后的代码.....
@SuppressWarnings("unchecked")
@Test
public void testBulkSaveAllPass() throws Exception {
List<AudienceCodeType> result = new ArrayList<AudienceCodeType>() {
private static final long serialVersionUID = -2557717330663162468L;
{
add(new AudienceCodeType() {{
setInternalid(1L);
setCode("1");
setLegend("Test1");
setDescription("JUnit Test 1");
setStatus("A");
setAudit(new Audit());
}});
add(new AudienceCodeType() {{
setInternalid(2L);
setCode("2");
setLegend("Test2");
setDescription("JUnit Test 2");
setStatus("A");
setAudit(new Audit());
}});
}};
doReturn(result).when(repository).saveAll(any(List.class));
doReturn(Optional.of(new AudienceCodeType()),
Optional.of(new AudienceCodeType()))
.when(repository).findOne(any(Example.class));
MultipleEntitiesReport<AudienceCodeType> testObject =
underTest.bulkInsert(new ArrayList<GenericReferenceCodeInput>(){
private static final long serialVersionUID = -6802324625468779673L;
{
add(new GenericReferenceCodeInput() {{
setCode("1");
setLegend("Test1");
setDescription("JUnit Test 1");
setStatus("A");
}});
add(new GenericReferenceCodeInput() {{
setCode("2");
setLegend("Test2");
setDescription("JUnit Test 2");
setStatus("A");
}});
}});
assertEquals("Test Return", false, testObject.getPass().isEmpty());
assertEquals("Test First Object Code", result.get(0).getCode(), testObject.getPass().get(0).getCode());
assertEquals("Test Second Object Code", result.get(1).getCode(), testObject.getPass().get(1).getCode());
Mockito.verify(repository, times(2)).findOne(any(Example.class));
Mockito.verify(repository, times(1)).saveAll(any(List.class));
}
这是我所采用的方法,希望这将来可能对其他人有所帮助。