Search code examples
restjunitresttemplatespring-restmockserver

AssertionError while executing JUnit test case


I am executing a JUnit test case to test the service. And I am facing java.lang.AssertionError while running the test case. Given below is the test class and the url builder piece of code.

@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));

    }

Actual service code to be tested is shown below:

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

Given below is the stack trace of the assertion error that i am getting:

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)

Other test classes with similar JUnit cases and similar GET endpoints are working fine. Am i missing something? Please give me some suggestions.


Solution

  • Please use a debugger, set a break point at the line

    URI targetUri = ...
    

    and find out why the URL is not build correctly.