使用 Mockito 模拟 UriInfo 并且响应不符合预期 [英] Mocking UriInfo using Mockito and the response is not as expected

查看:71
本文介绍了使用 Mockito 模拟 UriInfo 并且响应不符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟 UriInfo 对象以传递给一个安静的服务方法,如果成功请求具有正确的 QueryParams,响应应该是 List,如果发生任何错误或失败,响应应该是 List,我正在发送一个正确的请求,但是控制器接收它,就像我发送了一个没有 QueryParams 的错误请求一样,并给了我一个状态响应对象.

I am trying to mock UriInfo object to pass to a restful service method , the response should be List if successul request with the correct QueryParams, and StatusResponse in case of any error or failure happened, I am sending a correct request but the controller receives it as If I sent a wrong request with no QueryParams , and gives me a Status Response Object.

RestController.java

RestController.java

服务方式:

public Response getObjectives(@Context UriInfo info) {
try {

return Response.ok(objectiveService.getObjectives(info.getQueryParameters())).build();

} catch (Exception e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(StatusResponse.EXPIRED_SESSION).build();
}
}

RestControllerTest.java

RestControllerTest.java

private UriInfo mockUriInfo(String uri) throws URISyntaxException {
UriInfo uriInfo = Mockito.mock(UriInfo.class);
Mockito.when(uriInfo.getRequestUri()).thenReturn(new URI(uri));
return uriInfo;
}


@Test
@DataSet({ "/datasets/object_target_type.xml", "/datasets/st_objective_levels.xml", "/datasets/st_objectives.xml", "/datasets/st_related_objectives.xml" })
@Transactional(TransactionMode.ROLLBACK)
public void testGetObjectives() throws Exception {
UriInfo uriInfo = mockUriInfo("http://localhost:8080/v1.0/objectives?planId=987&level=2");
Response response = restController.getObjectives(uriInfo);
Object objs = response.getEntity();

ReflectionAssert.assertLenientEquals(objs, Arrays.asList(
    new Objective(2L, "Obj1", new Level(2L), 987L, new TargetType(1L), 222d, 3),
    new Objective(3L, "Obj2", new Level(2L), 987L, new TargetType(1L), 333d, 3),
    new Objective(5L, "Obj5", new Level(2L), 987L, new TargetType(1L), 555d, 3),
    new Objective(6L, "Obj6", new Level(2L), 654L, new TargetType(1L), 666d, 3))
                    );

}

但实际的响应是一个 StatusResponse,就像在 RestController 方法中抛出了一个异常一样.

But the actual response is a StatusResponse as if an exception was thrown in the RestController method.

我认为它没有模拟 UriInfo 并且它没有看到 QueryParams.

I think it failed to mock the UriInfo and it doesn't see the QueryParams.

帮帮我,模拟 UriInfo 对象有什么问题

Help me, whats wrong with mocking the UriInfo object

推荐答案

看起来你得到了一个空指针异常,因为你没有模拟 getQueryParameters().

It looks like you get a null pointer exception because you didn't mock getQueryParameters().

你的模拟代码可能看起来像这样(为了清楚起见,我把事情做得不那么简洁):

Your mocking code might look something like this (I've made things a bit less concise for clarity):

private UriInfo mockUriInfo(String uri) throws URISyntaxException {
    UriInfo uriInfo = Mockito.mock(UriInfo.class);
    URI uriObject = new URI(uri);
    Mockito.when(uriInfo.getRequestUri()).thenReturn(uriObject);
    Map<String,String> queryParametersMap = this.parseQueryParameters(uriObject.getQuery());
    Mockito.when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap(queryParametersMap));
    return uriInfo;
}

private Map<String,String> parseQueryParameters(String query) {
    String[] params = query.split("&");
    Map<String, String> map = new HashMap<String, String>();
    for (String param : params)
    {
        String[] pair = param.split("=");
        String name = pair[0];
        String value = pair[1];
        map.put(name, value);
    }
    return map;
}

这篇关于使用 Mockito 模拟 UriInfo 并且响应不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆