【问题标题】:JUnit test is successful in IntelliJ but failed in MavenJUnit 测试在 IntelliJ 中成功,但在 Maven 中失败
【发布时间】:2014-03-08 07:36:57
【问题描述】:

我编写了一个 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")));

【问题讨论】:

  • 您能提供更多信息吗?代码sn-ps等
  • 您的 %PATH% 中的 Java 版本是否与您在 IDE 中使用的版本不同?
  • 如果您还需要什么,请告诉我
  • 你的代码哪里出错了?我会说这是第一次断言,但是...
  • 第一个断言失败 assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode());在 intellij 中,状态非常令人满意。但在 mvn clean 测试响应显示为 400 而不是 201

标签: java maven junit intellij-idea


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多