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
372
votes
15 answers

How to check String in response body with mockMvc

I have simple integration test @Test public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception { mockMvc.perform(post("/api/users").header("Authorization",…
pbaranski
  • 22,778
  • 19
  • 100
  • 117
137
votes
6 answers

How to count members with jsonpath?

Is it possible to count the number of members using JsonPath? Using Spring MVC test I'm testing a controller that generates {"foo": "oof", "bar": "rab"} with: standaloneSetup(new FooController(fooService)).build() …
NA.
  • 6,451
  • 9
  • 36
  • 36
82
votes
5 answers

How to write a unit test for a Spring Boot Controller endpoint

I have a sample Spring Boot app with the following Boot main class @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); …
user6123723
  • 10,546
  • 18
  • 67
  • 109
80
votes
5 answers

How to check JSON in response body with mockMvc

This is my method inside my controller which is annotated by @Controller @RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8") @ResponseBody public JSONObject…
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
43
votes
3 answers

Spring MockMvc verify body is empty

I have a simple Spring test @Test public void getAllUsers_AsPublic() throws Exception { doGet("/api/users").andExpect(status().isForbidden()); } public ResultActions doGet(String url) throws Exception { return…
isADon
  • 3,433
  • 11
  • 35
  • 49
40
votes
4 answers

How to PUT multipart/form-data using Spring MockMvc?

I have a controller's method with a PUT method, which receives multipart/form-data: @RequestMapping(value = "/putIn", method = RequestMethod.PUT) public Foo updateFoo(HttpServletRequest request, @RequestBody Foo…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
35
votes
2 answers

Using Spring mockMvc to test optional path variables

I have a method in Spring MVC with optional path variable. I am trying to test it for a scenario when optional path variable is not provided. Snippet from Controller, resource URI to invoke- @RequestMapping(value = "/some/uri/{foo}/{bar}", method…
thisdotnull
  • 812
  • 1
  • 7
  • 20
30
votes
3 answers

Custom test of http header with spring-test-mvc

I'm testing my MVC service with spring-test-mvc I used something like: MockMvc mockMvc = standaloneSetup(controller).build(); mockMvc.perform(get("")).andExpect(content().bytes(expectedBytes)).andExpect(content().type("image/png")) …
Avi
  • 21,182
  • 26
  • 82
  • 121
26
votes
2 answers

Spring MVC integration tests with Spring Security

I'm trying to test my login page using mvc-test. I was working pretty good before I added spring security. My code is: mockMvc.perform( post("j_spring_security_check") .param(LOGIN_FORM_USERNAME_FIELD, testUsernameValue) …
wojtek
  • 493
  • 1
  • 7
  • 12
22
votes
5 answers

Spring boot disable @EnableAsync for integration tests

I would like to disable @EnableAsync when I run my integration tests. I tried to override the configuration file which is annotated with @EnableAsync with a class with the same name in my test package but it does not work. In this topic: Is it…
psv
  • 3,147
  • 5
  • 32
  • 67
22
votes
2 answers

Spring Boot 1.4 testing with Security enabled?

I'm wondering how I should go about authenticating a user for my tests? As it stands now all tests I will write will fail because the endpoints require authorization. Test code: @RunWith(SpringRunner.class) @WebMvcTest(value =…
Lithicas
  • 3,793
  • 7
  • 23
  • 32
20
votes
3 answers

Use @WithMockUser (with @SpringBootTest) inside an oAuth2 Resource Server Application

Environment: I have a spring boot based microservice architecture application consisting of multiple infrastructural services and resource services (containing the business logic). Authorization and authentication is handled by an oAuth2-Service…
David
  • 260
  • 1
  • 5
  • 13
19
votes
4 answers

Issue with testing Spring MVC slice in SpringBoot 1.4

I'm trying out the new Spring Boot 1.4 MVC testing features. I have the following controller. @Controller public class ProductController { private ProductService productService; @Autowired public void setProductService(ProductService…
user2693135
  • 1,206
  • 5
  • 21
  • 44
19
votes
2 answers

How to Mock the security context in Spring MVC for testing

I need to test some protected urls, therefore I need to set up a mock security context in my tests (junit). In particular I need perform some gets and post against my web application, using an authenticated user. Below there is my code, I am able to…
cloudy_weather
  • 2,837
  • 12
  • 38
  • 63
17
votes
2 answers

Are Spring's MockMvc used for unit testing or integration testing?

Spring has 2 setups for the MockMvc: Standalone setup WebApplicationContext setup In general what kind of testing is MockMvc used for? Unit or Integration? or Both? Am i right in saying that using the standalone setup (running outside the Spring's…
SoftwareDeveloper
  • 1,094
  • 2
  • 15
  • 26
1
2 3
23 24