-
Notifications
You must be signed in to change notification settings - Fork 304
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
Trying to use MockMvc to test a GraphQL API #779
Comments
According to GraphQL HTTP protocol, the request body should be somelike. {
"query":"",
"variables": {}
} |
Hi @hantsy Thanks for the response. Indeed, I've changed my
And now I am getting 200 SUCCESS. Besides this change, I also needed to set Here is the implementation of the
As we can see, it's secured and requires the authority "BOOK-API-ADMIN". In my test case, I am building a request with a JWT that contains this authority
However, if I do not inform the JWT, it should return me UNAUTHORIZED. Furthermore, if I inform a JWT without the "BOOK-API-ADMIN" authority, it should return FORBIDDEN. I've tried those cases and, I am always getting 200 SUCCESS. Do you know what it can be? Thanks |
Do you check the response body, is there an |
Sorry for not checking that. The response body is empty.
|
Spring GraphQL does not support MockMvc for testing. |
Hi @bclozel , just one question:
How would I do that? Use MockMvc (but then without GraphQL-specific support)? Thanks, |
@nilshartmann I have an integration test example of using |
Thanks, @hantsy . In my "graphql-kickstart times" I built something similiar, but I'd like to know if there are any problems or drawbacks when using MockMvc with Spring for GraphQL (beside the fact the one would be responsible to create a valid request payload, which |
It's not only creating the request payload but also assertions on the response that Generally, in the documentation for Specifically for MockMvc, it may be possible to use It would help to understand your goals for testing rather than the outcome to use MockMvc. I should think that one of the available options would help, but if you could please clarify. |
It is better to let |
Hi @rstoyanchev , thanks a lot for your reply! I was just asking because in another project I'm using a REST API and thus testing heavily with MockMvc. Now with GraphQL, I have to use another testing API. While this is not a big deal (and actually the test features of HttpGraphQlTester are great) it has some (minor) drawbacks. One has to learn another testing api (esp. result matcher and that is my biggest pain point) and one cannot use the "extensions" that are available for MockMvc (in my case for example jwt PostRequestProcessor or also some self-written PostRequestProcessors). Please don't get me wrong. This should not be a "feature request" for a MockMvc-based GraphQL testing API, as I understand there are lot more situations that you would have to support than me in my concrete project (that doesn't have functional endpoints for example). I just try to understand what it would mean to use MockMvc or if there are some hidden gotchas that one could run into. In my (naive) imagination, I had to create the request object (with query, variables, etc), submit it with a POST request and could then inspect the (json) result with the regular |
@nilshartmann thanks for the additional context. You can use MockMvc if you want, and in the beginning we also wondered whether we need a separate test API for GraphQL. After all it's just a JSON request and response in a specific format. We could have left it at that but whatever "little" work is required to do that still adds a lot of noise and makes tests more difficult to read. What we've done instead is to create a dedicated API for GraphQL that also helps with swapping out the underlying transport as needed, or not having a transport at all. That allows tests to be at the level of the GraphQL protocol, rather than the underlying transport. So the underlying transport should be a detail, and we do allow using MockMvc. Boot's AutoConfigureHttpGraphQlTester brings in |
@rstoyanchev thanks for your clarification. Helps a lot! |
Hi, I have a Spring Boot app that exposes a GraphQL API, and it's secured using Okta. The app works fine.
Now, I am trying to implement some test cases. Instead of testing directly in an Okta endpoint, I was trying to use
MockMvc
. Also, I'd like to use theMockMvcRequestBuilders.jwt()
to create easily different JWT to test the Queries and Mutations.I've tried this setup
However, I am getting 404
I am missing something?
Best regards,
The text was updated successfully, but these errors were encountered: