【问题标题】:AssertionError while executing JUnit test case执行 JUnit 测试用例时出现 AssertionError
【发布时间】:2019-01-29 10:28:26
【问题描述】:

我正在执行一个 JUnit 测试用例来测试服务。我在运行测试用例时面临 java.lang.AssertionError 。下面给出的是测试类和 url builder 代码。

@Test
    public void testSuccess() throws Exception
    {
        final String mockedResponseJson = rawJsonFromFile("com/cnanational/preferences/client/rule-sets/getRuleSetsResponse.json");

        MockRestServiceServer mockServer = mockServer();

        mockServer.expect(requestTo(dummyUri()))
                .andExpect(queryParam("ruleSetDescription", RULE_DESCRIPTION))
                .andExpect(method(HttpMethod.GET))
                .andRespond(withSuccess(
                        mockedResponseJson,
                        MediaType.APPLICATION_JSON));

        ServiceClientResponse<GetRuleSetsResponse> response = executeDummyRequest();

        mockServer.verify();

        assertThat(response.isSuccessful(), equalTo(true));

    }

实际要测试的服务代码如下:

    URI targetUri = UriComponentsBuilder.fromUri(this.preferencesServiceUri).path(this.rulesSetsPath)
                .queryParam("ruleSetDescription", params).build()
                .toUri();

下面是我得到的断言错误的堆栈跟踪:

java.lang.AssertionError: Unexpected request expected:<http://localhost:8039> but was:<http://localhost:8039?ruleSetDescription=TestRuleDescription>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
    at org.springframework.test.web.client.match.MockRestRequestMatchers$5.match(MockRestRequestMatchers.java:121)

具有类似 JUnit 案例和类似 GET 端点的其他测试类工作正常。我错过了什么吗?请给我一些建议。

【问题讨论】:

  • 您的response 似乎失败了。您能否发布AssertionError 完整的堆栈跟踪?
  • @gokareless 我已经发布了 AssertionError 堆栈跟踪。

标签: rest junit resttemplate spring-rest mockserver


【解决方案1】:

请使用调试器,在该行设置断点

URI targetUri = ...

并找出未正确构建 URL 的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多