gpt4 book ai didi

java - JUnit 测试在 IntelliJ 中成功但在 Maven 中失败

转载 作者:搜寻专家 更新时间:2023-11-01 03:52:30 24 4
gpt4 key购买 nike

我编写了一个在 Intellij 中成功执行并通过的 JUnit 测试。但是如果我运行 mvn clean test 那个特定的测试失败了。更具体地说,任务是检查请求是否已发送。因此,当我在 IntelliJ 中运行测试时,测试通过,状态代码为 201(成功)。但是当我运行 mvn clean install 时,它显示为状态代码 400(错误请求)。

我在网上搜索过这个问题,但找不到解决方案。请帮助我。

下面是代码。它在第一个断言时失败:assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());:

@Test
public void testUpdateMultiValueAttributes() throws URISyntaxException {


String createPayload =
"{\n" +
" \"id\": 9,\n" +
" \"email\": \"" + "ismith@zzz.com" + "\",\n" +
" \"profile\": {\"userAttrs\":[" +
" {\"CUST_ATTR_MULTI_VALUE\": \"CUST_ATTR_MULTI_VALUE_3\"}" +
" ]}" +
"}";
MockHttpRequest request = MockHttpRequest.put("/subscribers/9");
request.contentType(MediaType.APPLICATION_JSON);
request.content(createPayload.getBytes());
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
LOG.error(response.getContentAsString());
response.toString();


assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());
SubscriberDto subscriber = null ;
try {

subscriber = MarshallingUtils.unmarshallJSON(new TypeReference<SubscriberDto>() {
}, response.getContentAsString());
} catch (Exception e) {
fail(e.getMessage());
}

Collection<SubscriberAttributeDto> customAttributes = subscriber.getProfile().getUserAttrs();

if (customAttributes!=null)
assertTrue(customAttributes.contains(new SubscriberAttributeDto("CUST_ATTR_MULTI_VALUE", "CUST_ATTR_MULTI_VALUE_3")));

最佳答案

使用 createPayload.getBytes("UTF8") 可能会解决问题

关于java - JUnit 测试在 IntelliJ 中成功但在 Maven 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21686237/

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