单元测试@Suspended AsyncResponse 控制器
unit testing @Suspended AsyncResponse controller
我正在升级为完全异步,并且我有一个现有测试,该测试使用模拟依赖项对控制器进行单元测试并测试各种路径。我不知道如何转换这个单元测试。我正在使用 dropwizard/jersey,我想测试的方法现在看起来像这样
@POST
public void getPostExample(final Map body, @Suspended final AsyncResponse
asyncResponse){}
旧测试使用 mockito/junit 并使用 @InjectMocks
作为控制器,然后调用 getPostExample
上的方法并在响应中断言一些信息。它调用的服务是模拟的,但是当我搜索如何手动将其获取到 return 数据时,我找不到太多。我可以访问 AsyncResponse,但在调用带有结果的简历的真实代码中。我应该在测试中调用 resume 吗?
"Should I be calling resume in my test"。不,您应该做的是测试是否使用预期参数调用了 resume
方法。这就是您如何测试方法的行为。
您可以做的是使用 Mockito 的 ArgumentCaptor 捕获传递给 resume
方法的 Response
,然后对该 Response
进行断言。您需要模拟 AsyncResponse
才能正常工作。下面是一个例子
@RunWith(MockitoJUnitRunner.class)
public class AsyncMockTest {
@Mock
private AsyncResponse response;
@Captor
private ArgumentCaptor<Response> captor;
@Test
public void testAsyncResponse() {
final TestResource resource = new TestResource();
resource.get(this.response);
Mockito.verify(this.response).resume(this.captor.capture());
final Response res = this.captor.getValue();
assertThat(res.getEntity()).isEqualTo("Testing");
assertThat(res.getStatus()).isEqualTo(200);
}
@Path("test")
public static class TestResource {
@GET
@ManagedAsync
public void get(@Suspended AsyncResponse response) {
response.resume(Response.ok("Testing").build());
}
}
}
添加 mockito after()
方法调用以等待其他线程完成;这应该解决异常问题,f.e:
Mockito.verify(response, after(10000)).resume(this.captor.capture());
我正在升级为完全异步,并且我有一个现有测试,该测试使用模拟依赖项对控制器进行单元测试并测试各种路径。我不知道如何转换这个单元测试。我正在使用 dropwizard/jersey,我想测试的方法现在看起来像这样
@POST
public void getPostExample(final Map body, @Suspended final AsyncResponse
asyncResponse){}
旧测试使用 mockito/junit 并使用 @InjectMocks
作为控制器,然后调用 getPostExample
上的方法并在响应中断言一些信息。它调用的服务是模拟的,但是当我搜索如何手动将其获取到 return 数据时,我找不到太多。我可以访问 AsyncResponse,但在调用带有结果的简历的真实代码中。我应该在测试中调用 resume 吗?
"Should I be calling resume in my test"。不,您应该做的是测试是否使用预期参数调用了 resume
方法。这就是您如何测试方法的行为。
您可以做的是使用 Mockito 的 ArgumentCaptor 捕获传递给 resume
方法的 Response
,然后对该 Response
进行断言。您需要模拟 AsyncResponse
才能正常工作。下面是一个例子
@RunWith(MockitoJUnitRunner.class)
public class AsyncMockTest {
@Mock
private AsyncResponse response;
@Captor
private ArgumentCaptor<Response> captor;
@Test
public void testAsyncResponse() {
final TestResource resource = new TestResource();
resource.get(this.response);
Mockito.verify(this.response).resume(this.captor.capture());
final Response res = this.captor.getValue();
assertThat(res.getEntity()).isEqualTo("Testing");
assertThat(res.getStatus()).isEqualTo(200);
}
@Path("test")
public static class TestResource {
@GET
@ManagedAsync
public void get(@Suspended AsyncResponse response) {
response.resume(Response.ok("Testing").build());
}
}
}
添加 mockito after()
方法调用以等待其他线程完成;这应该解决异常问题,f.e:
Mockito.verify(response, after(10000)).resume(this.captor.capture());