1

我正在执行一个 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 端点的测试类工作正常。我错过了什么吗?请给我一些建议。

4

1 回答 1

1

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

URI targetUri = ...

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

于 2018-08-23T19:47:40.057 回答