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

时间:2014-02-10 19:32:52

标签: java maven junit intellij-idea

我写了一个JUnit测试,它在Intellij中成功执行并传递。但是,如果我运行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")));

1 个答案:

答案 0 :(得分:0)

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