gpt4 book ai didi

java - RestEasy:如何验证 ClientResponse 主体?

转载 作者:行者123 更新时间:2023-11-28 20:56:37 25 4
gpt4 key购买 nike

我有一个 REST端点为

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getVariables(@QueryParam("_activeonly") @DefaultValue("no") @Nonnull final Active active) {
switch(active){
case yes:
return Response.ok(VariablePresentation.getPresentationVariables(variableManager.getActiveVariables())).build();
case no:
return Response.ok(VariablePresentation.getPresentationVariables(variableManager.getVariables())).build();
}
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}

返回 JSONListVariablePresentation . VariablePresentaion看起来像

@XmlRootElement
public class VariablePresentation {

private final UUID id;
private final String name;
private final VariableType type;

public VariablePresentation(@Nonnull final Variable variable) {
id = variable.getId();
name = variable.getName();
type = variable.getType();
}

public String getId() {
return id.toString();
}

@Nonnull
public String getName() {
return name;
}

@Nonnull
public VariableType getType() {
return type;
}

注释为 JAXBXmlRoot返回 JSON .我的集成测试如下

@Test
public void testGetAllVariablesWithoutQueryParamPass() throws Exception {
final ClientRequest clientCreateRequest = new ClientRequest("http://localhost:9090/variables");
final MultivaluedMap<String, String> formParameters = clientCreateRequest.getFormParameters();
final String name = "testGetAllVariablesWithoutQueryParamPass";
formParameters.putSingle("name", name);
formParameters.putSingle("type", "String");
formParameters.putSingle("units", "units");
formParameters.putSingle("description", "description");
formParameters.putSingle("core", "true");
final ClientResponse<String> clientCreateResponse = clientCreateRequest.post(String.class);
assertEquals(201, clientCreateResponse.getStatus());
}

我想测试返回 List<VariablePresentation> 的请求正文作为字符串。如何将响应正文(字符串)转换为 VariablePresentation对象?

更新

添加以下内容后

        final GenericType<List<VariablePresentation>> typeToken = new GenericType<List<VariablePresentation>>() {
};
final ClientResponse<List<VariablePresentation>> clientCreateResponse = clientCreateRequest.post(typeToken);
assertEquals(201, clientCreateResponse.getStatus());
final List<VariablePresentation> variables = clientCreateResponse.getEntity();
assertNotNull(variables);
assertEquals(1, variables.size());

它因不同的错误而失败

testGetAllVariablesWithoutQueryParamPass(com.myorg.project.market.integration.TestVariable): Unable to find a MessageBodyReader of content-type application/json and type java.util.List<com.myorg.project.service.presentation.VariablePresentation>

我该如何解决?

最佳答案

我发现的最简单的方法是将 ClientResponse 与 Gson 一起使用

一个简单的例子可以是

  @Test
public void testGetAllVariablesWithoutQueryParamNoPass() throws Exception {
final ClientRequest clientCreateRequest_var0 = new ClientRequest("http://localhost:9090/variables?_activeonly=no");
final MultivaluedMap<String, String> formParameters = clientCreateRequest_var0.getFormParameters();
final String name0 = "testGetAllVariablesWithoutQueryParamNoPass_0";
formParameters.putSingle("name", name0);
formParameters.putSingle("type", "String");
formParameters.putSingle("units", "units");
formParameters.putSingle("description", "description");
formParameters.putSingle("core", "true");

final ClientResponse<String> clientCreateResponse_var0 = clientCreateRequest_var0.post(String.class);
assertEquals(201, clientCreateResponse_var0.getStatus());

final ClientRequest clientCreateRequest_var1 = new ClientRequest("http://localhost:9090/variables");
final MultivaluedMap<String, String> formParameters1 = clientCreateRequest_var1.getFormParameters();
final String name1 = "testGetAllVariablesWithoutQueryParamNoPass_1";
formParameters1.putSingle("name", name1);
formParameters1.putSingle("type", "String");
formParameters1.putSingle("units", "units");
formParameters1.putSingle("description", "description");
formParameters1.putSingle("core", "true");

final ClientResponse<String> clientCreateResponse_var1 = clientCreateRequest_var1.post(String.class);
assertEquals(201, clientCreateResponse_var1.getStatus());

final ClientRequest clientCreateRequest2 = new ClientRequest("http://localhost:9090/variables");
final ClientResponse<String> clientCreateResponse2 = clientCreateRequest2.get(String.class);

final Gson gson = new Gson();
final Type variableType = new TypeToken<List<VariablePresentation>>() {
}.getType();
List<VariablePresentation> variables = gson.fromJson(clientCreateResponse2.getEntity(), variableType);
assertNotNull(variables);

assertEquals(2, variables.size());

// clean test data
removeTestDataVariableObject(name0);
removeTestDataVariableObject(name1);
}

关于java - RestEasy:如何验证 ClientResponse 主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14774800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com