执行JUnit测试用例时出现AssertionError

时间:2018-08-23 05:49:07

标签: rest junit resttemplate spring-rest mockserver

我正在执行JUnit测试用例以测试服务。而且我在运行测试用例时面临java.lang.AssertionError。下面给出的是测试类和url构建器代码。

@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端点的测试类也可以正常工作。我想念什么吗?请给我一些建议。

1 个答案:

答案 0 :(得分:1)

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

URI targetUri = ...

并找出为什么URL构建不正确的原因。