Skip to content
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

Fix support of @ControllerAdvice registrations in the MockMvcBuilders standalone setup [SPR-17542] #22074

Closed
spring-projects-issues opened this issue Nov 27, 2018 · 4 comments
Assignees
Labels
in: test status: invalid

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Nov 27, 2018

Benoit BERAUD opened SPR-17542 and commented

MockMvcBuilders is supposed to support any kind of @ControllerAdvice (see #17348, especially last comment from Rossen Stoyanchev).

Indeed it seems to work only with ExceptionHandlers. 

See attached file for a test which succeeds (ExceptionHandlerTests, copied from Spring source code) and a failing test (GenericControllerAdviceTests, creation of my own). Files are also present here : https://gist.github.com/benoit74/651a9348120613e42b0e2ee4894a6e5e

Bug seems to be present since the implementation of #17348 (for instance bug seen in Spring Boot 1.5.17, e.g. Spring 4.3.20). A backport to 4.3 would be greatly appreciated (even if EOL is quite close ...).


Affects: 5.1.2

Attachments:

Issue Links:

  • #17348 Support @ControllerAdvice registrations in the MockMvcBuilders standalone setup
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 28, 2018

Rossen Stoyanchev commented

Thanks for the sample test. However @ControllerAdvice is working as expected. I don't believe there is an issue. The problem rather is that the controller is neither @RestController, nor has @ResponseBody and therefore the ResponseBodyAdvice does not apply. The test is green if @RestController or @ResponseBody is used.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 29, 2018

Benoit BERAUD commented

So obvious. Sorry for your time lost on such a mistake.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 30, 2018

Benoit BERAUD commented

Indeed, looking back at my original project (not the test sample I provided), I realised that I was already using a @RestController.

My issue was indeed that I returned a ResponseEntity without any body to "simplify" my test :

return ResponseEntity.ok().build();

If change this to return a body, the test is green:

return ResponseEntity.ok("whatever");

Too bad ...

 

 

@metodski
Copy link

@metodski metodski commented Apr 16, 2020

What if i have @ControllerAdvice on a whole package and don't want to add the controller classes one by one in the standaloneSetup ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test status: invalid
Projects
None yet
Development

No branches or pull requests

3 participants