https://github.com/ghusta/test-spring-boot-mvc-mock
Demo project for Spring Boot + MockMvc + @WebMvcTest
https://github.com/ghusta/test-spring-boot-mvc-mock
spring-boot spring-mvc test
Last synced: 4 months ago
JSON representation
Demo project for Spring Boot + MockMvc + @WebMvcTest
- Host: GitHub
- URL: https://github.com/ghusta/test-spring-boot-mvc-mock
- Owner: ghusta
- Created: 2019-11-26T09:10:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-17T08:52:38.000Z (over 5 years ago)
- Last Synced: 2025-01-06T22:35:58.448Z (5 months ago)
- Topics: spring-boot, spring-mvc, test
- Language: Java
- Size: 85.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
README
= Demo project for Spring Boot + MockMvc + @WebMvcTest
:author: Guillaume HUSTA
:toc:== Overview
How to test Spring Boot + Spring MVC + Unit tests + MockMvc.
== Unit test for a Spring MVC controller
See https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-server[Server-Side Tests].
> You can write a plain unit test for a Spring MVC controller by using JUnit or TestNG. To do so, instantiate the controller, inject it with mocked or stubbed dependencies, and call its methods (passing MockHttpServletRequest, MockHttpServletResponse, and others, as necessary).
...
> The goal of Spring MVC Test is to **provide an effective way to test controllers** by performing requests and generating responses **through the actual DispatcherServlet**.
There is no need to run a Servlet container to run these unit tests.
It uses https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#mock-objects-servlet["mock" implementations of the Servlet API].== References
* https://spring.io/guides/gs/testing-web/[Testing the Web Layer] (Spring.io)
* https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-framework[Spring MVC Test Framework] (Spring.io)
* Spring Security :
** https://docs.spring.io/spring-security/site/docs/current/reference/html/test.html#test-mockmvc[Spring MVC Test Integration] (Spring.io)
** https://www.baeldung.com/spring-security-integration-tests[Spring Security for Spring Boot Integration Tests]
** https://blog.zenika.com/2016/11/25/spring-mvc-test-dans-un-contexte-securise/[Spring MVC test dans un contexte sécurisé] 🇫🇷
* Javadoc :
** https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.html[@WebMvcTest] (Spring Boot)
** https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/MockMvc.html[MockMvc] (Spring FWK)
** https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet/AutoConfigureMockMvc.html[MockMvcAutoConfiguration] (Spring Boot)
** https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.html[WebMvcProperties] (Spring Boot -- _spring.mvc.*_)
** https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletPath.html[DispatcherServletPath] (Spring Boot -- _spring.mvc.servlet.path_)
* StackOverflow :
** https://stackoverflow.com/questions/21837872/how-to-set-servlet-path-for-every-request-through-mockmvc[How to set servlet path for every request through MockMvc]
** https://stackoverflow.com/questions/21749781/why-i-received-an-error-403-with-mockmvc-and-junit[Why I received an Error 403 with MockMvc and JUnit?]
** https://stackoverflow.com/questions/32903024/why-is-the-configured-servlet-path-correctly-used-by-the-rest-controllers-but-i[Why is the configured servlet path correctly used by the REST controllers, but ignored in Spring Security features?] ⭐ (answer by Rob Winch)