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

Spring MVC Junit testing MultipartFile always empty

I'm using spring-mvc version 4.1.6-RELEASE with Junit 4.12 and java 1.7, I have a controller for file uploading who works when I've tested it on server with Browser. But when I try to test it with junit the mockfilemultipart is always empty and I'm…
theMan80
  • 83
  • 1
  • 7
5
votes
4 answers

Autowired HttpServletRequest in Spring-test integration tests

I am trying to do a test to cover login functionality. Version of Spring is 3.2.12. I have a session bean, declared as: @Service @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) public class ClientSessionServiceImpl implements…
George Lezhava
  • 53
  • 1
  • 1
  • 3
4
votes
2 answers

Spring Boot 2.5.0 - REST controller, MockMvc not UTF-8

In my REST controller I use @PostMapping, @GetMapping, etc. without any other specification. The default must be therefore JSON, for example for @GetMapping. Also there is no specification of the character encoding, it must be UTF-8 I assume, I…
neblaz
  • 683
  • 10
  • 32
4
votes
3 answers

No mapping for request with mockmvc

Currently struggling with problem when I get 'mapping error for request' with following controller/test configuration. Controller: @Slf4j @Validated @RestController @RequiredArgsConstructor public class AdtechController { private final…
Miklosh
  • 139
  • 2
  • 10
4
votes
3 answers

How do I specify a mediatype of text/plain;charset=UTF-8 in a Spring Boot Test

Here's my test : @Test fun `test config properties`() { mockMvc.request(HttpMethod.GET,"someUrl") { accept = MediaType.TEXT_PLAIN }.andExpect { status { isOk } content { contentType(MediaType.TEXT_PLAIN) } …
PaulNUK
  • 4,774
  • 2
  • 30
  • 58
4
votes
1 answer

NPE when loading custom SecurityConfig for Keycloak in WebMvcTest

In the webapp itself everything runs without any problem. The auth works as expected. However, since I have Spring Security on my classpath, all my controller tests are now failing because the default Spring Security configuration is being loaded.…
Lyannic
  • 307
  • 1
  • 2
  • 10
4
votes
1 answer

Spring MockMvc - getting java.time.Instant from REST

I have a resource that returns me an object with java.time.Instant property. class X { ... private Instant startDate; ... } And I am testing it with: mockMvc.perform(get("/api/x")) .andExpect(status().isOk()) …
Krzysztof Majewski
  • 2,494
  • 4
  • 27
  • 51
4
votes
1 answer

MockMvc post junit test returning 415 when ResponseBody have collection

I am using mockmvc for my rest services integration test. I have a post rest service, called it from postman and getted 200 but when i call it with mockmvc porform method returned 415 unsupported media type exception. Rest…
mertaksu
  • 535
  • 1
  • 9
  • 26
4
votes
1 answer

RestAssured vs Mockmvc for unit and integration testing

I am asked to choose one and the best from these two for unit and integration tests- 1. RestAssured 2. Mockmvc The application is in Spring and Spring Boot. I have read many blogs and no clear distinction is given or I couldn't find one. For rest…
a p
  • 1,121
  • 6
  • 26
  • 51
4
votes
1 answer

Bearer token failure MockMvc test Java Spring Boot

I'm failing to understand why this does not work. I'm assuming it's something simple that I'm overlooking. All other other test methods not utilizing a token work fine. There is no expiration currently on the token, and I can use it fine with…
Leo Williams
  • 147
  • 1
  • 11
4
votes
1 answer

Does spring @GetMapping work with MockMvc

HelloController.java @RestController class HelloController { @GetMapping(value = "{id}/hello") public ModelAndView listAPI(@PathVariable("id") String profileId) { ModelAndView mav = new ModelAndView(); return mav; } …
user3451476
  • 297
  • 1
  • 4
  • 17
4
votes
5 answers

Validate LocalDate in JSON response with Spring MockMVC

I'm trying to validate a LocalDate object in a JSON result returned by a Spring MVC webservice but I can't figure out how. At the moment I always run into assertion errors like the following one: java.lang.AssertionError: JSON path…
GreenTurtle
  • 1,144
  • 2
  • 21
  • 35
4
votes
0 answers

How to Unit Test Spring Boot App with Spring Security

I have a simple application that I have setup with spring security using a custom MySql Database. You can check out the complete app on github. Now the problem is I'm writing test cases for it and they seems to fail on login page and anything that…
zhaider
  • 595
  • 2
  • 9
  • 26
4
votes
1 answer

Unittesting secured rest controller with spring

I have a very small REST application written using spring boot. I want to write a unit test for the authentication, but even if i add @MockWithUser to a test, i receive a 401 error. The importent files are the security…
AsconX
  • 161
  • 1
  • 15
4
votes
0 answers

NPE at JSON parsing when testing controller with MockMVC

I'm writing tests for my controller and I'm getting NPE when performing post with JSON. The same JSON works fine with Postman (only added escape slashes) but jackson (JSON parser I'm using) can't parse it in my test. Here is UserControllerTest…
doublemc
  • 3,021
  • 5
  • 34
  • 61