-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Using MockMvc to test Flux<ServerSentEvent> endpoints? #22544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note: I don't seem to be the only one strugling with this. See: https://stackoverflow.com/questions/35499655/how-to-test-server-sent-events-with-spring There are two answers there, neither of which seem very satisfactory. |
Thanks Kris. Indeed |
How can we use WebTestClient with Spring Boot MVC (starter-web)? So if WebTestClient is for WebFlux only, and MockMvc is for MVC only, and we want to test Flux/Mono responses, what should we do? I'm trying to find an answer to this for multiple hours at this point... and each of such answers is contradictory or confusing or incorrect, because things have changed. |
@daliborfilus, indeed In addition in 5.3 we updated That said, for testing SSE and streaming responses, full end-to-end tests through the |
@rstoyanchev Thank you. Seem like I tried to use WebTestClient as autowired component, which doesn't work in this case? The WebTestClient link you mention links to sample project https://github.com/spring-projects/spring-framework/tree/master/spring-test/src/test/java/org/springframework/test/web/client/samples which doesn't use WebTestClient, at least not directly? One of the tests mocks the server side completely, which I don't want - I just want to mock the servlet part and still do end-to-end integration test. (Or even expose random port and test it using rest template - which is maybe what this sample project does? But that's another topic.) The part 3.6.1 - Setup explains how to create instance of WebTestClient - or more precisely MockMvcWebTestClient - bound to a controller or the application context. I don't want to instantiate the controller here, because then I would need to pass all autowired dependencies to it, so the ApplicationContext is preferrable I think. So following this, I now have this code: @ActiveProfiles("test")
// tried with RANDOM_PORT and MOCK
@SpringBootTest(classes = [BackendApplication::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc(printOnlyOnFailure = false)
@AutoConfigureRestDocs(outputDir = "build/generated-snippets")
internal class MonitoringControllerTest {
@Autowired
lateinit var context: WebApplicationContext
lateinit var client: WebTestClient
@BeforeEach
fun setUp() {
client = WebTestClient.bindToApplicationContext(context).build()
}
@Test
fun indexAcceptJsonAndDocument() {
client.get().uri("$contextPath/entities/export")
// .with(SecurityMockMvcRequestPostProcessors.user("user").authorities(allAuthorities())) // FIXME: find replacement for this
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("$.totalElements").isEqualTo(3)
.jsonPath("$.totalPages").isEqualTo(1)
// .andDo(MockMvcRestDocumentation.document("monitored-entities-export")) // FIXME: find replacement for this to get restdocs working; use consumeWith?
}
} which throws and I don't know why:
Some relevant links: https://stackoverflow.com/questions/60792015/springboot-test-fails-with-no-bean-named-webhandler-available Tried adding @AutoConfigureWebTestClient - didn't help. |
@rstoyanchev Uh. I tried to at least solve the todos in the comments above (mock user, apply rest docs) and I found out that I cannot use So even if I could get that "webHandler" dependency error go away, I still cannot use the WebTestClient in Kotlin. |
As you already found out, you need to use MockMvcWebTestClient.bindToApplicationContext(this.wac).build(); As for #20606 yes that is an issue. I believe there is some effort to resolve it but I don't have the details. |
@rstoyanchev Thank you, I will stick to MVC + MockMvc for now and we'll see in the future. |
No problem. For streaming and Server-Sent Events, you'll need the |
I'm strugling with writing tests for rest endpoints that return Flux. I'd like to use mockMvc for this. Is this even possible?
Note that, allthough I have some rest endpoints returning Flux and/or Mono I am not using WebFllux but just regular MVC. The framework handles those types fine and for my Flux example, these are received on the browser side as a 'stream' of ServerSentEvents. This all works fine.
The trouble is. I can't seem to find a good way to write tests agains these endpoints using mockmvc.
The best I came up with is here:
https://github.com/kdvolder/mvc-flux-testing/blob/master/src/test/java/com/example/demo/MvcFluxDemoApplicationTests.java
Is turning the stream of events into String via `getResponse().getContentAsString()' really the best I can do here? (Note: @rstoyanchev has told me it is, mockMvc doesn't provide good ways to test this sort of thing. He recommends to use WebTestClient).
So I submit for consideration the following:
It may be useful to expand mockMvc apis to handle endpoints returning streamed ServerSentEvents better. Perhaps, something that can get the content as a Flux? Then we can use reactive testing support such as StepVerifier to validate the content in our tests.
Add a pointer in the docs on using WebTestClient instead for these scenarios.
The text was updated successfully, but these errors were encountered: