java.lang.IllegalStateException: Cannot add more expectations after actual requests are made.

Spring JIRA | Stéphane Nicoll | 6 months ago
  1. 0

    I am investigating the option of adding a {{@RestClientTest}} annotation in Spring Boot that would automatically configure a {{RestTemplate}} and {{MockRestServiceServer}}. The default expectation managers throw an exception if you try to add a request after you have validated one, something like: {noformat} java.lang.IllegalStateException: Cannot add more expectations after actual requests are made. at org.springframework.util.Assert.state(Assert.java:392) at org.springframework.test.web.client.AbstractRequestExpectationManager.expectRequest(AbstractRequestExpectationManager.java:60) at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:112) at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:95) {noformat} There is no way to "reset" the mock server at the end of the test so it looks like the only way to make this work is to create a new server instance per test. My next move was to try to share the builder and simply call build in a {{Before}} annotated method but that does not work either as the builder shares an expectation manager by default, which means that every created server using the same builder share the same expectation manager. Looking at the API, I find it weird that we can both use {{ignoreExpectOrder}} and {{expectationManager}} as those are mutually exclusive. Perhaps removing the latter and creating a fresh expectation manager would help? It also would be nice if a server instance could be reused for multiple tests with a public {{reset}} method that would reset the expectation manager.

    Spring JIRA | 6 months ago | Stéphane Nicoll
    java.lang.IllegalStateException: Cannot add more expectations after actual requests are made.
  2. 0

    I am investigating the option of adding a {{@RestClientTest}} annotation in Spring Boot that would automatically configure a {{RestTemplate}} and {{MockRestServiceServer}}. The default expectation managers throw an exception if you try to add a request after you have validated one, something like: {noformat} java.lang.IllegalStateException: Cannot add more expectations after actual requests are made. at org.springframework.util.Assert.state(Assert.java:392) at org.springframework.test.web.client.AbstractRequestExpectationManager.expectRequest(AbstractRequestExpectationManager.java:60) at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:112) at org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:95) {noformat} There is no way to "reset" the mock server at the end of the test so it looks like the only way to make this work is to create a new server instance per test. My next move was to try to share the builder and simply call build in a {{Before}} annotated method but that does not work either as the builder shares an expectation manager by default, which means that every created server using the same builder share the same expectation manager. Looking at the API, I find it weird that we can both use {{ignoreExpectOrder}} and {{expectationManager}} as those are mutually exclusive. Perhaps removing the latter and creating a fresh expectation manager would help? It also would be nice if a server instance could be reused for multiple tests with a public {{reset}} method that would reset the expectation manager.

    Spring JIRA | 6 months ago | Stéphane Nicoll
    java.lang.IllegalStateException: Cannot add more expectations after actual requests are made.
  3. 0

    Simple WebMvcTest fails with Spring Boot 1.4M3 due to missing SpringBootConfiguration

    GitHub | 7 months ago | jgoldhammer
    java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    How to use Spring Hatoas ControllerLinkBuilder for a Thymeleaf templated scheduled email

    Stack Overflow | 1 year ago | Romell
    java.lang.IllegalStateException: Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?
  6. 0

    jetty 9.3.1 and 9.3.2 can't find resource in /src/main/webapp

    GitHub | 1 year ago | zhanhb
    java.lang.IllegalStateException: Cannot find template location: /WEB-INF/templates (please add some templates or check your Thymeleaf configuration)

  1. tvrmsmith 2 times, last 3 weeks ago
  2. tvrmsmith 4 times, last 1 month ago
  3. schoetty 8 times, last 7 months ago
  4. Александр Пастухов 1 times, last 7 months ago
48 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.IllegalStateException

    Cannot add more expectations after actual requests are made.

    at org.springframework.util.Assert.state()
  2. Spring Core
    Assert.state
    1. org.springframework.util.Assert.state(Assert.java:392)
    1 frame
  3. Spring TestContext
    MockRestServiceServer.expect
    1. org.springframework.test.web.client.AbstractRequestExpectationManager.expectRequest(AbstractRequestExpectationManager.java:60)
    2. org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:112)
    3. org.springframework.test.web.client.MockRestServiceServer.expect(MockRestServiceServer.java:95)
    3 frames