The Spring Test Context Framework has a behavior analysis problem when given to test cases.
Ie.: With a Jakarta Servlet Filter implementation in Combination with Spring Boot Registration, into doFilter after a if/else, the chain should follow or be redirected. With this implementation and execution, with curl command line is possible check the behavior of authorization (ok/unathorized). But, if the same are executed with MockMvc, the filter is ignored and the Spring do more things against the initial resource lookup. So, in the first time, with curl the response are ok/unauthorized, but with MockMvc are ok/ok/bad_request/ok.
Regards!
Comment From: bclozel
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: ejpg-sys
It's a bug because the behavior of the framework should be the same as well spected in another hand.
Comment From: bclozel
@ejpg-sys please provide a minimal sample application created using start.spring.io showing the problem and we'll reconsider the issue.
Comment From: ejpg-sys
@bclozel follow-up the sample application as described on this issue: https://github.com/ejpg-sys/issue-spring-mockmvc
Comment From: bclozel
Thanks for the sample, I understand better now.
MockMvc
is not meant to replicate the entire experience of running in a Servlet container. There are some limitations explained in the reference documentation. Such an enhancement has been considered in the past and declined.
There are several ways to solve this problem:
- in your tests, assert that the result has been forwarded to the unauthorized URL
- rewrite some tests to be actual integration tests with a live server. This can be done with
WebTestClient
which works with both mockmvc and live server tests.
I'm closing this issue. I also managed to find a related question on StackOverflow, but maybe contributing your own case there could help the community.