我正在尝试使用junit在服务类中测试void方法。我不确定如何测试。
代码:
public class Service{
private static final Logger LOGGER = LogManager.getLogger(Service.class);
private final ServiceDAO serviceDAO;
private final RtyDAO rtyDAO;
public Service(ServiceDAO serviceDAO,
RtyDAO rtyDAO) {
this.serviceDAO = serviceDAO;
this.rtyDAO = rtyDAO;
}
public void voidMethod(JSONObject object) {
try {
Rty rty = serviceDAO.getSomething();
String output = rtyDAO.persist(rty);
if("TRUE".equalsIgnoreCase(output)){
LOGGER.info("Success"):
}else{
LOGGER.info("Failure"):
}
}
}
由于上述方法无效,因此我不确定如何进行测试。我正在尝试如下:
public class ServiceTest {
@InjectMocks
Service service;
@Mock
RtyDAO RtyDAO;
@Mock
Rty rty;
@Mock
ServiceDAO serviceDAO;
@Test
public void testVoid(){
Mockito.when(serviceDAO.getSomething()).thenReturn(rty);
Mockito.when(rtyDAO.persist(rty)).thenReturn("Success");
//how to test result here if the persisting happened without any issues?
}
答案 0 :(得分:0)
正如您所说,该方法是Void,因此很难测试。但是有办法!您可以使用Mockito来证明两个调用均正确完成。检查以下答案:Mockito : how to verify method was called on an object created within a method?