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
3
votes
1 answer

Unit testing Placeholder defaultValue in @RequestParam

I'm trying to test my controller that has a handler with a RequestParam configured with a default value pointed to a placeholder: @Controller public class AgeController { @GetMapping("/age") public String…
3
votes
1 answer

Mockmvc unit testing with String[] as request body

I'm trying to create a unit test for the PUT api, as seen below, with a String[] as the request body. @RequestMapping(value = "/test/id", method = RequestMethod.PUT) public ResponseEntity updateStatus(@RequestBody String[]…
kns
  • 43
  • 1
  • 7
3
votes
3 answers

MockMvc test POST request

I have the following post route in my REST controller: @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json") public ResponseEntity saveMovie(@RequestBody Movie movie){ …
user2693135
  • 1,206
  • 5
  • 21
  • 44
3
votes
1 answer

Can not attach body to my POST request using Spring MockMvc

I'm trying to test my rest controller. No issues with GETs, but when I try to test a POST method I'm unable to attach the body. private static final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), …
anat0lius
  • 2,145
  • 6
  • 33
  • 60
3
votes
1 answer

REST Assured: How to find element using nested property in JSON and validate other properties

I have limited experience with rest assured. We have a number of tests that I can usually find examples within, or failing that google, but I am stuck when trying to match on a nested property for an element in an anonymous array, and verify…
Clarkey
  • 1,553
  • 5
  • 22
  • 34
3
votes
1 answer

Empty content body in checking exception scenarios with mockmvc

Overview: I am going to test bad request (400) with a customized error message in Spring MVC Test. The test gets 400 as status; however the content body is empty. The code snippets are as follows: @Before public void setup() { …
saeedj
  • 2,179
  • 9
  • 25
  • 38
3
votes
1 answer

MockMvc - status expected:<200> but was:<302>

The 302 error occurred during mockmvc test junit. The redirect issue of insertBoard class,what should i do. status expected:<200> but was:<302> @RequestMapping(value="/sample/insertBoard.do") public ModelAndView insertBoard(CommandMap…
kim
  • 31
  • 1
  • 4
3
votes
2 answers

How to inject Authentication in mvcmock unit testing

Overview I have a REST service with injected authentication and I want to create a unit test for it by using mockmvc. My RestController class is as follows: import java.util.ArrayList; import java.util.Collection; import java.util.Set; import…
saeedj
  • 2,179
  • 9
  • 25
  • 38
3
votes
0 answers

Test ExceptionHandling with MockMvc and Mockito

I want to test my RestController with MockMvc and Mockito. I have providerController with injected service and providerService throw ProviderNotFoundException if prover with this id not found. Also i have ExceptionHandler for this…
eugene-karanda
  • 760
  • 1
  • 6
  • 9
3
votes
2 answers

I mockMVC, can I loop round results which are being tested by .andExpect()?

I am testing an endpoint which returns a list of objects. I am currently testing each element in sequence as per the code below. The code is abbreviated for clarity. There may be many elements and many attributes. This code can get long and ugly …
mikec
  • 155
  • 1
  • 17
3
votes
0 answers

SpringMVC/MockMVC/ jsonpath compare list of Integers

I have searched online but can't find why or how to fix it. Problem: My JUnit test pulls a list of user id's from a REST-API. However, I can't seem to assert the contents of the response... API Response: [ {"userId":1458548702}, …
NeilA
  • 1,450
  • 2
  • 12
  • 20
3
votes
2 answers

Do I need to create a new MockMvc for each test?

I’m attempting to add Cucumber to my Spring Web MVC project which is already using spring-test and JUnit. The non-Cucumber integration tests I’ve already written have the WebApplicationContext autowired in, then created a MockMvc for each test. The…
Jason Whittle
  • 751
  • 4
  • 23
3
votes
0 answers

Integration tests work separately but not run together in Spring Boot app

Seems like I am missing something essential here, but I have not managed to find it on my own. I have a Spring Boot app together with Hibernate that I am trying to test with some integration tests. I am using a HSQL-database while testing. The tests…
Ole-M
  • 821
  • 2
  • 13
  • 27
3
votes
2 answers

Spring MockMvcResultMatchers jsonPath lower/greater than

I'm using MockMvcResultMatchers to test my controller classes. Here is a sample code RequestBuilder request = get("/employee/") .contentType(MediaType.APPLICATION_JSON) .accept(APPLICATION_JSON_UTF8); …
silent-box
  • 1,649
  • 3
  • 21
  • 40
3
votes
1 answer

MockMvc and User Principal

I try to test the access of mvcControllers injecting the UserPrincipal : restPockMockMvc .perform(get("/pocs").principal(authToken)) .andReturn(); and here's the method of the controller I try to…