简体   繁体   English

Spring 启动测试:预期请求不会使用 ExpectedCount.manyTimes()

[英]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.我正在尝试对我的代码运行一些集成测试,并使用spring-boot-test中的MockRestServiceServer来设置预期的请求。

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: mockServer是在这样的超类中创建的:
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: MockServerBinder.bindPersistentThingworxPropertiesCall()是一个帮助器 class,如下所示:
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 :是一个 function ,它使用MockMVC检查 URL 是否没有任何身份验证:
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());当我运行这个测试时, expectedResult.addResult("thingworx", service.getThingworxAvailability()); will be able to use the MockRestServiceServer , but the assertOpenUrl(request);将能够使用MockRestServiceServer ,但assertOpenUrl(request); line will fail, because MockRestServiceServer doesn't expect anymore calls to the endpoint binded in MockServerBinder.bindPersistantThingworxPropertyCall() .行将失败,因为MockRestServiceServer不再期望调用绑定在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.如果我将MockServerBinder.bindPersistantThingworxPropertyCall()复制并粘贴到现有的下面,则不会发生这种情况,所以我认为这是我如何首先绑定请求的问题。

From what I understand ExpectedCount.manyTimes() should keep this request during the test.据我了解, ExpectedCount.manyTimes()应该在测试期间保留此请求。 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. PEBCAK 问题。

As you can see in the bindPersistentThingworxPropertiesCall() , I actually didn't use ExpectedCount.manyTimes() .正如您在bindPersistentThingworxPropertiesCall()中看到的那样,我实际上没有使用ExpectedCount.manyTimes() I didn't catch that.我没听懂。 Changed it and now it works.改变了它,现在它可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 春季启动:@GetMapping与Pageable作为请求参数无法正常工作 - Spring Boot: @GetMapping with Pageable as request parameter don't work as expected 使用@EnableAsync时,Spring Boot不响应任何请求 - Spring Boot don't respond to any request when using @EnableAsync 春季启动测试-使用2个数据库时EntityManager不持久 - Spring boot test - EntityManager does not persist when using 2 databases @ConfigurationProperties 在 Spring Boot 2.4.2 的测试中不起作用 - @ConfigurationProperties don't work in test in Spring Boot 2.4.2 使用Spring Boot和JPA时如何保持 - How to persist when using Spring Boot with JPA Spring Boot和Spring Data Rest集成测试无法持久化数据 - Spring boot & Spring data rest integration test fails to persist data Spring Boot JPA不会在单个测试范围内保留更改 - Spring boot JPA does not persist changes in single test scope @RequestMapping spring 启动没有按预期 function - @RequestMapping spring boot doesn't function as expected 使用Flyway w / Spring Boot Test的H2 SQL语法错误`expected“。,COMMENT,(”` - H2 SQL Syntax Error `expected “., COMMENT, (”` using Flyway w/ Spring Boot Test 我无法在 Spring boot Authentication Provider 中保留数据 - I can't Persist data within Spring boot Authentication Provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM