Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Mockito根据调用次数更改模拟对象行为?_Java_Unit Testing_Mocking_Mockito - Fatal编程技术网

Java 使用Mockito根据调用次数更改模拟对象行为?

Java 使用Mockito根据调用次数更改模拟对象行为?,java,unit-testing,mocking,mockito,Java,Unit Testing,Mocking,Mockito,我正在模拟一个接口,用于向远程Http服务提交一些对象,逻辑如下:如果提交成功,请尝试提交对象5次,然后继续提交下一次,否则请尝试,直到达到5次,如果仍然失败,则放弃 interface EmployeeEndPoint { Response submit(Employee employee); } class Response { String status; public Response(String status) { this.stat

我正在模拟一个接口,用于向远程Http服务提交一些对象,逻辑如下:如果提交成功,请尝试提交对象5次,然后继续提交下一次,否则请尝试,直到达到5次,如果仍然失败,则放弃

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);
}
现在的问题是,我如何告诉mock在前两次返回“ERROR”,在第三次返回“SUCCESS”

标题告诉JMock,标签告诉JMockit

您的代码看起来像Mockito(而不是JMock或JMockit),所以我假设您使用Mockito,不管您在描述中写了什么

Mockito允许您按顺序枚举返回值或链接
。然后*()
方法:

// either this
when(employeeEndPoint.submit(employee)).thenReturn(
   new Response("ERROR"),
   new Response("ERROR"),
   new Response("SUCCESS") // returned at 3rd call and all following
);
// or that
when(employeeEndPoint.submit(employee))
    .thenReturn(new Response("ERROR"))
    .thenReturn(new Response("ERROR"))
    .thenReturn(new Response("SUCCESS"));// returned at 3rd call and all following
标题告诉JMock,标签告诉JMockit

您的代码看起来像Mockito(而不是JMock或JMockit),所以我假设您使用Mockito,不管您在描述中写了什么

Mockito允许您按顺序枚举返回值或链接
。然后*()
方法:

// either this
when(employeeEndPoint.submit(employee)).thenReturn(
   new Response("ERROR"),
   new Response("ERROR"),
   new Response("SUCCESS") // returned at 3rd call and all following
);
// or that
when(employeeEndPoint.submit(employee))
    .thenReturn(new Response("ERROR"))
    .thenReturn(new Response("ERROR"))
    .thenReturn(new Response("SUCCESS"));// returned at 3rd call and all following

我有点困惑:您谈论JMock,标记JMockit,但代码看起来像Mockito。你能澄清一下你使用的是哪种嘲弄框架吗?@TimothyTruckle哦,孩子,对不起。你是对的!是莫基托!我有点困惑:您谈论JMock,标记JMockit,但代码看起来像Mockito。你能澄清一下你使用的是哪种嘲弄框架吗?@TimothyTruckle哦,孩子,对不起。你是对的!是莫基托!