0
我正在嘲笑一個接口提交一些對象到遠程Http服務,邏輯如下:嘗試提交對象5次,如果提交成功,然後繼續下一個,否則嘗試,直到它達到5 - 然後丟棄如果仍然失敗。使用Mockito更改基於調用次數的模擬對象行爲?
interface EmployeeEndPoint {
Response submit(Employee employee);
}
class Response {
String status;
public Response(String status) {
this.status = status;
}
}
class SomeService {
private EmployeeEndPoint employeeEndPoint;
void submit(Employee employee) {
Response response = employeeEndPoint.submit(employee);
if(response.status=="ERROR"){
//put this employee in a queue and then retry 5 more time if the call succeeds then skip otherwise keep trying until the 5th.
}
}
}
@Mock
EmployeeEndPoint employeeEndPoint;
@Test
public void shouldStopTryingSubmittingEmployeeWhenResponseReturnsSuccessValue() {
//I want the first
Employee employee
= new Employee();
when(employeeEndPoint.submit(employee)).thenReturn(new Response("ERROR"));
when(employeeEndPoint.submit(employee)).thenReturn(new Response("ERROR"));
when(employeeEndPoint.submit(employee)).thenReturn(new Response("SUCCESS"));
//I want to verify that when call returns SUCCESS then retrying stops !
// call the service ..
verify(employeeEndPoint,times(3)).submit(employee);
}
現在的問題是我怎麼告訴模擬返回「ERROR」前兩次,並返回「SUCCESS」的第三次?
我有點困惑:請您談一下JMock的,標記JMockit,但代碼看起來像Mockito。你能否澄清一下你使用的模擬框架? –
@TimothyTruckle哦,男孩,我很抱歉。你是對的 !這是mockito! – Adelin