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

Spring JIRA | Stéphane Nicoll | 11 months ago
tip
Do you find the tips below useful? Click on the to mark them and say thanks to rafael . Or join the community to write better ones.
  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 | 11 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 | 11 months ago | Stéphane Nicoll
    java.lang.IllegalStateException: Cannot add more expectations after actual requests are made.
  3. 0
    samebug tip
    If you're using boot 1.4.x you need to modify the @springBootTest annotation to @SpringBootTest(classes = DataFlowServerConfiguration.class). 1.3.8 doesn't have this problem as well.
    via GitHub by trisberg
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0
    samebug tip
    Spring Boot 1.4 changed their annotation system a little. Try using the @SpringBootApplication annotation for better results.
  6. 0

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

    GitHub | 11 months ago | jgoldhammer
    java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

  1. tvrmsmith 2 times, last 2 weeks ago
  2. Dunmer 1 times, last 1 month ago
  3. SteVio1989 8 times, last 3 months ago
  4. jpvee 2 times, last 4 months ago
  5. Hronom 3 times, last 4 months ago
3 more registered users
49 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