Questions tagged [spring-test-mvc]

spring-test-mvc based on spring-test provides a unit testing framework for spring-mvc services.

spring-test-mvc based on provides a unit testing framework for services.

spring-test-mvc might be included in the Spring Framework version 3.2.x

Spring One DC 2012 slides

356 questions
4
votes
1 answer

Testing Spring 4.0.3 Controller (MVC) with MockMVC

I am trying to create Test Cases for a RestController. Here is a simple RestContorller @RestController @RequestMapping("/media") public class MediaListController { @RequestMapping(method = RequestMethod.GET) public String getAllMedia(){ …
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
4
votes
1 answer

@RequestHeader Map map default case?

In spring 3.2, we make use of this in our controllers: @RequestHeader Map headers The issue is that the map of header keys can be any case, so we have to re-build the header map, forcing the keys to lowercase. It would be nice if…
hansSolo
  • 61
  • 2
  • 7
3
votes
0 answers

How to test a BadRequest exception on a POST request

I made a test that checked whether a post method from my controller does what it's supposed to. It worked great! Now I'm supposed to make a test to see whether the right message pops up when I get an 400 error for that post method. Here's what I've…
3
votes
0 answers

Gradle test fails with OutOfMemory when run together BUT passes when run individually

I have tried everything to try at least understand why this is happening even if I can't solve it. I have the following integration test which is part of a larger test suite of 893…
ng.newbie
  • 2,807
  • 3
  • 23
  • 57
3
votes
3 answers

Spring - How to build a junit test for a soap service

I'm following the spring guide to create a hello world soap ws. The link below : https://spring.io/guides/gs/producing-web-service/ I successfully make it work. When i run this command line : curl --header "content-type: text/xml" -d …
Amdouni Mohamed Ali
  • 410
  • 1
  • 4
  • 18
3
votes
2 answers

How to invoke @BeforeMethod block before @PostConstruct

I am writing below Spring Unit test code. Unit test @Before method is not getting executed. Since it is directly running @PostConstruct i am getting erorrs Caused by: java.lang.IllegalArgumentException: rate must be positive because the default…
Kiran
  • 839
  • 3
  • 15
  • 45
3
votes
1 answer

MockMvcRequestBuilders.asyncDispatch providing an empty HTTP Response and contentType

I have a unit test, where i am attempting to check the response of a async request being made, after converting the method to return a StreamingResponseBody using Spring 4.3. The test method is below : final MockMvc mockMvc =…
Ben
  • 1,086
  • 3
  • 15
  • 30
3
votes
1 answer

Handling my custom exception in Spring MVC integration test

I have the following method in a controller class: @PostMapping("employees") @ResponseStatus(HttpStatus.CREATED) public Employee addEmployee(@Valid @RequestBody Employee employee) { try { return employeeRepository.save(employee); }…
logi0517
  • 813
  • 1
  • 13
  • 32
3
votes
3 answers

How to mock property source in spring test?

I'm writing unit tests for my controller and faced an issue that desktop.properties file doesn't exist on my build server and shouldn't exist there. I have this main SpringBoot…
Pasha
  • 1,768
  • 6
  • 22
  • 43
3
votes
1 answer

Spring Boot 2 @WebMvcTest and OAuth2 #oauth2.hasScope controller causes IllegalArgumentException

This is a spring boot 2.0.6 application using an MVC controller using @PreAuthorize and #oauth2.hasScope to secure a controller endpoint. I have written tests using @WebMvcTest to write controller tests. It appears that the requests are being made…
Rig
  • 1,276
  • 3
  • 22
  • 43
3
votes
3 answers

Is there a way to include a spring component in a WebMvcTest

Given production code classes: @RestController @RequiredArgsConstructor public class MyController { private final MyValidator validator; // annotations relating to request mapping excluded for brevity public void test(@Valid…
3
votes
1 answer

How to test spring Boot rest controllers with MockMvc by skipping repository classes?

I am trying to create Test class for Controller as below: Please note that we have created library for all repositories&domains (Using Spring DATA JPA) and added the dependency in actual application where UserController…
Krish
  • 1,804
  • 7
  • 37
  • 65
3
votes
2 answers

Spring 5.0.0 MockMvc: Unsupported media type, even with correct MediaType set

I am using JUnit4 with Spring Web MVC Test and I have test class for a Controller. The Controller handles a POST request to "/test" with JSON body content. I've tested this method manually using Postman and I get a 400 Bad Request response, as…
the duck wizard
  • 105
  • 2
  • 8
3
votes
1 answer

Why spring test is fail, does not work @MockBean

I try to create my first the test for a simple spring-boot controller but I get Handler: Type = null. In browser code is work but a test fails. My App use spring-security. Please help me fix it an issue and understand my mistake. Thank You. This is…
Pavel
  • 2,005
  • 5
  • 36
  • 68
3
votes
1 answer

spring session is not compatible with MockHttpSession

The following test fails when I try to integrate spring session. class WeChatOAuth2AuthenticationFilterTest extends AbstractWebMvcTest { @Test void it_should_redirect_user_to_origin_uri_when_wechat_oauth_is_finished() throws Exception { …