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

MockMVC in junit tests - checking result for List and Map
I have problems with mockMVC and test written with that. My tests fails. I have two tests methods: @Test public void getPersonsForApiConsumerTest() throws Exception { mockMvc.perform(get(getUri("/consumers/1/persons"))) …
xross
  • 597
  • 4
  • 9
  • 25
8
votes
4 answers

How to test server sent events with Spring?

I've implemented a controller with a method returning an SseEmitter and now I want to test it. The only way I could find so far is the following: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes =…
Andras Hatvani
  • 4,346
  • 4
  • 29
  • 45
7
votes
2 answers

spring validation for requestparam not working

I am trying to validate my parameter in controller using @Min(1) annotation. When I test it in unit test, it comes as 200. I am not sure what I am doing wrong. Here is my code: Controller: @GetMapping(produces = "application/json") public…
Jonathan Hagen
  • 580
  • 1
  • 6
  • 29
7
votes
2 answers

MockHttpServletRequestBuilder - how to change remoteAddress of remoteHost of HttpServletRequest?

I'm trying to create mock request for integration test (@SpringBootTest). //given MockHttpServletRequestBuilder requestBuilder = get("/users/register/user1"); What I want to check is the remote of this request. In my controller Im getting…
7
votes
3 answers

Unit Testing 'Location' header of Spring REST Controller

After creating a resource in Spring REST Controller , I am returning it's location in header as below. @RequestMapping(..., method = RequestMethod.POST) public ResponseEntity createResource(..., UriComponentsBuilder ucb) { ... URI…
Bilal Mirza
  • 2,576
  • 4
  • 30
  • 57
7
votes
2 answers

Why mockMVC and mockito don't work together?

I have restful services and I want to unit test them without connecting to database, therefore I have written this piece of code: @Before public void setup() throws Exception { this.mockMvc = webAppContextSetup(webApplicationContext).build(); …
Shilan
  • 813
  • 1
  • 11
  • 17
7
votes
2 answers

Spring MVC testing (security Integration test), JSESSIONID is not present

I have created custom login form for my spring boot app. In my form integration test, I want to check that received cookies contain JSESSIONID and XSRF-TOKEN. But, I received only XSRF-TOKEN. Here is my…
Hutsul
  • 1,535
  • 4
  • 31
  • 51
7
votes
2 answers

Using Spring's MockMvc framework, how do I test the value of an attribute of an attribute of my model?

I’m using Spring 3.2.11.RELEASE and JUnit 4.11. I’m using Spring’s org.springframework.test.web.servlet.MockMvc framework to test a controller method. In one test, I have a model that is populated with the following object: public class…
Dave
  • 15,639
  • 133
  • 442
  • 830
7
votes
1 answer

How to test with MockMVC a Map parameter in Spring Rest

In Spring Rest, I have a RestController exposing this method: @RestController @RequestMapping("/controllerPath") public class MyController{ @RequestMapping(method = RequestMethod.POST) public void create(@RequestParameter("myParam")…
Rémi Doolaeghe
  • 2,262
  • 3
  • 31
  • 50
7
votes
3 answers

MockMVC Integrate test controller with session scoped bean

I am trying to integrate test a Spring Controller method that uses a spring session scoped bean which is injected into the controller. In order for my test to pass I must be able to access my session bean to set some values on it before I make my…
6
votes
0 answers

How can I test gzip with mockMvc?

I'm testing spring boot compression property: server.compression.enabled=true I would like to validate that after sending a request with a header "Accept-Encoding":"gzip", I would receive a response with header "Content-Encoding":"gzip". The test is…
Evandro
  • 137
  • 1
  • 1
  • 13
6
votes
2 answers

Spring Test Wants To Connect to Database

I am trying to write some tests for my application and encountered following problem: I defined a application-test.yml with folling content: server: port: 8085 spring: security: oauth2: resourceserver: …
R. Polito
  • 544
  • 6
  • 21
6
votes
1 answer

How to POST multipart/form-data in MockMVC?

I've created a controller that would consume a 'multipart/form-data' @PostMapping(value="/sample") public void sample( @ModelAttribute("request") SampleRequest request){ // codes } SampleRequest object @NotNull private MultipartFile…
jawsh
  • 163
  • 2
  • 11
6
votes
1 answer

Is there any proper matcher to parse and compare LocalDateTime field in Json response from MockMvc

I'm testing get method of my SpringBoot controller, which is providing objects, written in base in particular time range. I realize that I can get json after mockMvc performing and parse it with object mapper, use some streams and one assert, but I…
Ludov Dmitrii
  • 425
  • 5
  • 9
6
votes
1 answer

Unit Test for Spring Asyncronous post request is returning exception(async started expected as true but false)

My POST request is Asynchronous because I am using spring-data-rest. Now I am having issue when trying to write a test case for the POST request. The exception is "Async started expected: true but was: false" I have read many articles and they all…
anand
  • 166
  • 1
  • 10