简体   繁体   中英

Spring Boot Test: Expected request don't persist using ExpectedCount.manyTimes()

I'm trying to run some integration tests on my code and I use a MockRestServiceServer from spring-boot-test in order to set up the expected requests.

I have one call that is called many times while running my test, but it seems not to persist during the test. My test looks like this:

@Test
    void getHealthStatus() {
        try {
            RequestBuilder request = get("/actuator/hc").contentType("application/json");

            MockServerBinder.bindPersistentThingworxPropertiesCall(
                    mockServer,
                    requestTo(new URI(String.format("%sProperties/TestProp", thingworxUrl))),
                    objectMapper.writeValueAsString(new PingResponse(DashboardIndicator.HEALTHY, 200))
            );

            DashboardStatusModel expectedResult = new DashboardStatusModel();
            expectedResult.addResult("spring",service.getAppHealth());
            expectedResult.addResult("thingworx", service.getThingworxAvailability());

            assertOpenUrl(request);
            MvcResult result = mockMvc.perform(get("/actuator/hc").contentType("application/json"))
                    .andExpect(status().isOk())
                    .andReturn();

            DashboardStatusModel actualResult = objectMapper.readValue(result.getResponse().getContentAsString(), DashboardStatusModel.class);

            assertEquals(expectedResult.getResults().get("spring"), actualResult.getResults().get("spring"));
            assertEquals(expectedResult.getResults().get("thingworx").getStatus(),actualResult.getResults().get("thingworx").getStatus());
            assertEquals(expectedResult.getResults().get("thingworx").getData().get("url"), actualResult.getResults().get("thingworx").getData().get("url"));
        } catch (Exception e) {
            fail("Unable to perform REST call on GDP-API", e);
        }
    }

As additional information:

  • mockServer is created in a superclass like this:
protected static MockRestServiceServer mockServer;
    @BeforeEach
    public void configureMockServer() {
        mockServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
    }
  • MockServerBinder.bindPersistentThingworxPropertiesCall() is a helper class that looks like this:
public static void bindPersistentThingworxPropertiesCall(MockRestServiceServer mockServer, RequestMatcher request, String responseJSONasString){
        mockServer.expect(ExpectedCount.once(), request)
                .andExpect(method(HttpMethod.GET))
                .andRespond(withStatus(HttpStatus.OK)
                        .contentType(MediaType.APPLICATION_JSON)
                        .body(responseJSONasString));
    }
  • assertOpenUrl(request); is a function that checks if a URL doesn't have any authentication by using a MockMVC :
public void assertOpenUrl(RequestBuilder request){
        try{
            mockMvc.perform(request).andExpect(status().isOk());
        } catch (Exception e) {
            fail("Unable to perform REST call on GDP-API", e);
        }
}

When I run this test, the expectedResult.addResult("thingworx", service.getThingworxAvailability()); will be able to use the MockRestServiceServer , but the assertOpenUrl(request); line will fail, because MockRestServiceServer doesn't expect anymore calls to the endpoint binded in MockServerBinder.bindPersistantThingworxPropertyCall() . This does not happen if I Copy & Paste MockServerBinder.bindPersistantThingworxPropertyCall() under the existing one, so I think it's a problem with how I binded the request in the first place.

From what I understand ExpectedCount.manyTimes() should keep this request during the test. Is this not true or is there another way I should bind my request so that it stays available during the entire test?

PEBCAK issue.

As you can see in the bindPersistentThingworxPropertiesCall() , I actually didn't use ExpectedCount.manyTimes() . I didn't catch that. Changed it and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM