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
22
votes
2 answers

Upload file using Spring mvc and MockMVC

I have successfully uploaded a image file to WebContent\resources\uploads\image.jsp. But I am facing a problem in testing this using MockMvc. When I run the test case I am getting the exceptions file not found and access denied. The controller looks…
spider
  • 517
  • 1
  • 4
  • 8
22
votes
4 answers

Hamcrest with MockMvc: check that key exists but value may be null

I'm doing some tests with MockMvc, and I want to validate the structure of a JSON response. Specifically, I want to make sure that the key to an attribute exists, and that the value is of a certain type or null. { "keyToNull": null, # This may…
nebulabrot
  • 465
  • 1
  • 6
  • 13
21
votes
4 answers

Spring Controller testing with MockMvc post method

I am trying to test a method of my controller in a Spring Boot application. This is a post endpoint, which gets an id in a request and it passes on this id to a service: @Slf4j @Controller public class AdministrationController { private final…
handris
  • 1,999
  • 8
  • 27
  • 41
20
votes
3 answers

Why does Spring MockMvc result not contain a cookie?

I am trying to unit-test login and security in my REST API, so I try to mock real-life request sequences as close as possible. My first request would be: this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac). …
P.Péter
  • 1,527
  • 16
  • 39
18
votes
3 answers

Unit test Springboot MockMvc returns 403 Forbidden

I wrote one unit test that tests UsersController. UsersControllerTest.findUser is working fine, but UsersControllerTest.insertGetModifyDelete it doesn't. In the log of the test I can see that the POST request doesn't match any method of…
ravenskater
  • 702
  • 1
  • 7
  • 20
17
votes
3 answers

Getting httpServletRequest attribute with MockMvc

I have a really simple controller defined in this way: @RequestMapping(value = "/api/test", method = RequestMethod.GET, produces = "application/json") public @ResponseBody Object getObject(HttpServletRequest req, HttpServletResponse res) { …
Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98
16
votes
1 answer

Test HTTP status code of redirected URL with MockMvc

I want to test the login process in a Spring Boot Application using MockMvc. After the successful login, the user gets redirected to /home. To test this, I use: @Test public void testLogin() throws Exception { RequestBuilder requestBuilder =…
Ben
  • 1,579
  • 4
  • 20
  • 34
16
votes
2 answers

RestFuse vs Rest Assured vs MockMVC Rest Service Unit Test Framework

I've been trying to find a simple all purpose unit test framework for Spring MVC based Rest Services I've written. I've been searching online and narrowed it down to: RestFuse (http://developer.eclipsesource.com/restfuse/) Rest Assured…
JackDev
  • 11,003
  • 12
  • 51
  • 68
15
votes
1 answer

Can I use Spring's MockMvc with Jersey resources?

I have successfully integrated Spring and Jersey via Spring Boot' starter POMs, and I have a couple Jersey endpoints. Now I'd like to unit test the resources. I can't seem to get MockMvc working. I get a 404 error when attempting to GET a resource…
John O'Conner
  • 465
  • 1
  • 6
  • 14
15
votes
2 answers

Integration Testing Spring Boot With MockMVC

I'm having some trouble testing a Spring Boot application with MockMvc. I have the following test class: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {SpringConfiguration.class,…
David
  • 7,652
  • 21
  • 60
  • 98
13
votes
1 answer

How to map mock mvc GET call into java POJO?

I have a rest controller method that returns an object CompositeObject that contains within it several other objects and structures (Maps and Lists). I want to write a test that tests whether the rest get call returns that object along with the…
ITWorker
  • 965
  • 2
  • 16
  • 39
13
votes
3 answers

Add csrf token with mockmvc and junit

I have a view with two metas (I'm using thymeleaf): In my test controller I do this: HttpSessionCsrfTokenRepository…
oscar
  • 1,636
  • 6
  • 31
  • 59
13
votes
2 answers

how to treat controller exception with mockmvc

I am using MockMVC to test my controller. I have the following controller: public class A{ ... @RequestMapping("/get") public List get(@RequestParam(defaultValue = "15", required = false) Integer limit) throws IOException { …
tzortzik
  • 4,993
  • 9
  • 57
  • 88
12
votes
2 answers

MockMvc HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

I receive the following exception: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported at…
dinamix
  • 651
  • 8
  • 22
12
votes
2 answers

Why use Spring's Mock MVC when you have unit test of your controller classes

I have seen people around me using Spring MVC in unit tests for controller classes, which is not helpful in what a unit test is for. Unit tests are supposed to test your actual implementation of the controller class, and this can be achieved more…
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1
2
3
49 50