Questions tagged [mockmvc]

MockMVC is the key part of the Spring MVC Test framework is. It simulates the internals of Spring MVC.

MockMVC is the key part of the Spring MVC Test framework is. It simulates the internals of Spring MVC.

742 questions
6
votes
2 answers

Testing rest controller with PageableDefault

Im trying to test a rest service that has @getmapping one param and PageableDefault @GetMapping(path = "/api/{serial}/gender") public ResponseEntity task( @PathVariable(value = "serial") String Serial, @PageableDefault(size…
6
votes
1 answer

How to enable controller parameter validation for standalone MockMvc

Controller @RestController @Validated class MyController { @GetMapping("/foo") public String unwrapped(@Min(1) @RequestParam("param") int param) { return Integer.toString(param); } @GetMapping("/bar") public String…
Bax
  • 4,260
  • 5
  • 43
  • 65
6
votes
1 answer

Access request body and request header in spring mvc test

I have created a spring boot application and this is how my controller looks like. I am using postman to send json in request body and a string in request header, then further hashing the json and comparing it with the string got by request header.…
Sunil
  • 63
  • 1
  • 1
  • 4
6
votes
1 answer

Springs MockMvc returning empty content in one of two only slightly different approaches

I'm testing a Spring MVC @RestController which in turn makes a call to an external REST service. I use MockMvc to simulate the spring environment but I expect my controller to make a real call to the external service. Testing the RestController…
Shady
  • 794
  • 2
  • 8
  • 23
6
votes
0 answers

Add Interceptors for MockMvcBuilders webAppContextSetup

I am writing unit test for controller. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MyControllerWebConfig.class) @WebAppConfiguration public class MyControllerTest{ @Autowired private…
Nish
  • 137
  • 1
  • 10
6
votes
1 answer

JsonPath OR condition using MockMVC

I am practicing MockMVC for rest call unit testing. how can we test the Boolean values so that whether the result is true or false I need to pass the test, I tried as follows, mockMvc.perform(get("/student/{Id}",…
Vinod
  • 2,263
  • 9
  • 55
  • 104
6
votes
2 answers

MockMvc Test Spring throws org.springframework.web.HttpMediaTypeNotSupportedException

I am getting the follow exception org.springframework.web.HttpMediaTypeNotSupportedException trying to test a Json Controller. The method in controller is: @RequestMapping(value = "/report", method = RequestMethod.PUT) public @ResponseBody…
Paula
  • 81
  • 1
  • 6
6
votes
1 answer

"Content type not set" when Unit Testing Spring RESTful controller

I have a Rest controller similar to this one: @RestController public class UserRestController { @Autowired private UserService userService; @RequestMapping(value = "/user/activate", method = RequestMethod.POST) public…
Sobrino
  • 531
  • 1
  • 6
  • 14
6
votes
2 answers

Spring Boot MockMVC Test does not load Yaml file

I have my configuration in application.yml file in the root of classpath (src/main/resources/). The configuration gets loaded fine when I start the application normally. However in my test the application.yml file gets not loaded at all. The header…
leo
  • 3,677
  • 7
  • 34
  • 46
6
votes
3 answers

Spring MockMvc and async controller's HTTP status code

How do i validate/test the 500 internal server error in MockMvc, when my controller is of Async servlet nature? I am writing unit test cases for my REST endpoint as part of a test cases i need to validate that the server sends 500 internal error as…
Naveen Kumar
  • 893
  • 11
  • 19
6
votes
1 answer

org.springframework.web.servlet.DispatcherServlet noHandlerFound when testing through MockMvc

I have defined a rest service using Spring Mvc 4 and then testing the same through MockMvc. Correct response is returned when I run the service using Tomcat 7 through following…
Niharika G.
  • 333
  • 2
  • 3
  • 14
6
votes
0 answers

Spring mockmvc test and interceptors

I initially implemented a controller to handle OPTIONS requests and apply the headers required for CORS to the response. Recently I have been cleaning up code and created an interceptor for the CORS stuff. Part of that cleanup would be to remove…
hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
6
votes
1 answer

MockMvc in Spock not working

I have a setup of a simple controller: @Controller @RequestMapping("/test") public class TestController { @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody…
tehras
  • 741
  • 1
  • 7
  • 19
5
votes
1 answer

How to junit test a method with Mono return type

What is the proper way to junit mono and get a body response? I expected "Single Item Only" to be in the body response of MockHttpServletResponse. But instead I see it's returned in Async. Sample Method: public Mono getData(final String…
JayC
  • 2,144
  • 8
  • 42
  • 77
5
votes
3 answers

Why does MockMvc in return 404 error on valid path with Spring Security enabled?

I'm trying to test my Spring Boot REST Controller with MockMvc. Following is my test class: UserControllerImplTest (version 1) @SpringBootTest @AutoConfigureMockMvc class UserControllerImplTest { @Autowired private MockMvc mockMvc; …
Maks Babarowski
  • 652
  • 6
  • 16