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
17
votes
3 answers

Testing Spring asyncResult() and jsonPath() together

I'm using a restful url to kick off a long-running backend process (it is normally on a cron schedule, but we want the ability to kick it off manually). The code below works and I see the result in the browser when I test…
Matt Byrne
  • 4,908
  • 3
  • 35
  • 52
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
3 answers

Test maximum upload file size with MockMultipartFile

I create a file upload service with Spring Boot and test it with Spring Mock Mvc and MockMultipartFile. I want to test if an error is thrown when the maximum file size is exceeded. The following test fails because it receive a 200. RandomAccessFile…
fabwu
  • 642
  • 6
  • 19
12
votes
4 answers

How to test spring 5 controllers with Junit5

I'm trying to test my Spring 5 web controllers with JUnit 5. The two way to test controller (as mentionned in spring documentation) always give me null pointer. This is my test class import…
Lacunasaurus
  • 265
  • 1
  • 3
  • 9
12
votes
2 answers

How to mock Eureka when doing Integration Tests in Spring?

I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes =…
Keyhan
  • 121
  • 1
  • 1
  • 5
12
votes
3 answers

Spring4 MVC Unit test does not compile

I have strange behaviour when trying to compile sources after Spring 3.2.5 → 4.0.0 version update. Faulty code snippet from ApplicationControllerTest.java (it is equivalent to code from documentation): import…
dma_k
  • 10,431
  • 16
  • 76
  • 128
11
votes
3 answers

Spring Annotation @WebMvcTest does not work in an app that has Jpa repositories

I have a Spring App that uses JPA repositories (CrudRepository interfaces). When I try to test my controller using the new Spring test syntax @WebMvcTest(MyController.class), it fails coz it tries to instantiate one of my service class that uses JPA…
dickyj
  • 1,830
  • 1
  • 24
  • 41
11
votes
1 answer

Replacement of import org.springframework.test.context.transaction.TransactionConfiguration; in Spring Test 4.3.1 version?

I'm working on the JPA QueryDSL example. In this example I created PersonDaoTest.java. Earlier I was using the lower version of spring-test as client asked to update it its latest version so I used 4.3.1.RELEASE. When I used this version I see that…
user4821194
11
votes
1 answer

How to print request body using spring-test-mvc

I'm using spring-test-mvc to test my controller, but I can't find a way to print request body which is very inconvenient. with MockMvcResultHandlers.print() mvc.perform(put("/payment/1234") .content("{\"amount\":2.3") …
Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
11
votes
3 answers

@ModelAttribute controller spring-mvc mocking

I want to test a controller which is using @ModelAttribute for one of its method arguments. public String processSaveAction(@ModelAttribute("exampleEntity") ExampleEntity exampleEntity) @ModelAttribute method getExampleEntity is using…
Remik
  • 25
  • 1
  • 2
  • 11
10
votes
2 answers

Spring MockMvc - How to test delete request of REST controller?

I need to test my controller methods including a delete method. Here is partial controller code: @RestController @RequestMapping("/api/foo") public class FooController { @Autowired private FooService fooService; // other methods which…
Denis Stephanov
  • 4,563
  • 24
  • 78
  • 174
9
votes
1 answer

Spring Boot Testing @WebMvcTest for a Controller appears to load other controllers in the context

Here's my test case for a Spring Controller @RunWith(SpringRunner.class) @WebMvcTest(value = MyController.class) public class MyControllerTest { @MockBean private MyService myService; } So this is a unit test specifically for the methods…
user6123723
  • 10,546
  • 18
  • 67
  • 109
9
votes
5 answers

How to add basic auth to Autowired testRestTemplate in SpringBootTest; Spring Boot 1.4

My OAuth integration test before Spring Boot 1.4 looked as follows(updates just to not use deprecated features): @RunWith(SpringRunner.class) @SpringBootTest(classes = { ApplicationConfiguration.class }, webEnvironment =…
9
votes
3 answers

java.lang.AssertionError: Content type not set while junit Spring MVC Controller?

I am using JUnit to test my Spring MVC controller. Below is my method which returns a index.jsp page and shows Hello World on the screen - @RequestMapping(value = "index", method = RequestMethod.GET) public HashMap handleRequest()…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
9
votes
5 answers

Add user role to request in Spring MVC Test Framework

Started studying Spring Test MVC Framework at the office today, it looks handy, but facing some serious trouble right off the bat. Spent a few hours googling, but couldn't find anything related to my issue. Here's my very simple test class: import…
t0mppa
  • 3,983
  • 5
  • 37
  • 48
1
2
3
23 24