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

How to test if JSON path does not include a specific element, or if the element is present it is null?

I have been writing some simple unit testing routines for a simple spring web application. When I add @JsonIgnore annotation on a getter method of a resource, the resulting json object does not include the corresponding json element. So when my unit…
Zobayer Hasan
  • 2,187
  • 6
  • 22
  • 38
41
votes
4 answers

mockMvc - Test Error Message

Does anybody have any tips, or does anybody know how I can test the "error message" returned by the HTTP response object? @Autowired private WebApplicationContext ctx; private MockMvc mockMvc; @Before public void setUp() throws Exception { …
Vinchenzo
  • 652
  • 2
  • 8
  • 16
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
38
votes
5 answers

SpringBootTest : No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available:

Hey i have started learing spring-boot junit testing using spring boot Test framework at the time of creating the test case i am facing issues below . import static org.hamcrest.Matchers.containsString; import static…
Prabhat Yadav
  • 1,181
  • 6
  • 18
  • 29
33
votes
4 answers

Using Spring's mockMvc, how do I check if the returned data contains part of a string?

I’m using Spring 3.2.11.RELEASE and JUnit 4.11. Using the Spring mockMvc framework, how do I check if a method returning JSON data contains a particular JSON element? I have mockMvc.perform(get("/api/users/" + id)) …
Dave
  • 15,639
  • 133
  • 442
  • 830
32
votes
4 answers

Asserting array of arrays with JSONPath and spring mvc

I'm having difficulty figuring out how to assert with jsonPath in a JSON document response in spring mvc. Perhaps there's a better way of accomplishing this than using jsonPath for this particular scenario. I would like to validate that the links…
Magnus Lassi
  • 921
  • 2
  • 9
  • 21
28
votes
1 answer

What is the difference between MockMvc and WebTestClient?

When I tried to test at Spring 4.x, I used MockMvc web client, but I am reading and trying new features of Spring 5.x. I think, WebTestClient and MockMvc are same or very similar. What is the difference between MockMvc and WebTestClient ? I am…
devsh
  • 331
  • 1
  • 4
  • 10
27
votes
4 answers

How to prevent NestedServletException when testing Spring endpoints?

I am trying to test the security configuration of some of my endpoints which are secured with @PreAuthorize(#oauth2.hasScope('scope'). When accessing such an endpoint via Postman with a access token that does not have the required scope, the…
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
27
votes
6 answers

Register @ControllerAdvice annotated Controller in JUnitTest with MockMVC

My @ControllerAdvice annotated Controller looks like this: @ControllerAdvice public class GlobalControllerExceptionHandler { @ResponseStatus(value = HttpStatus.UNAUTHORIZED) @ExceptionHandler(AuthenticationException.class) public void…
Rudolf Schmidt
  • 271
  • 1
  • 3
  • 3
26
votes
3 answers

How to extract value from JSON response when using Spring MockMVC

I have an endpoint that accepts a POST request. I want to get the ID of the newly created entity from the JSON response. Below is a segment of my code where I'm attempting to do that. mockMvc.perform(post("/api/tracker/jobs/work") …
Chol Nhial
  • 1,327
  • 1
  • 10
  • 25
26
votes
4 answers

Spring Security, JUnit: @WithUserDetails for user created in @Before

In JUnit tests with Spring MockMVC, there are two methods for authenticating as a Spring Security user: @WithMockUser creates a dummy user with the provided credentials, @WithUserDetails takes a user's name and resolves it to the correct custom…
elaforma
  • 652
  • 1
  • 9
  • 29
25
votes
4 answers

MockMvc configure a header for all requests

In my tests I setup the MockMvc object in the @Before like this mockMvc = MockMvcBuilders.webAppContextSetup(context) .apply(springSecurity()) .build(); In every request I do I always need to send the same…
isADon
  • 3,433
  • 11
  • 35
  • 49
25
votes
5 answers

MockRestServiceServer simulate backend timeout in integration test

I am writing some kind of integration test on my REST controller using MockRestServiceServer to mock backend behaviour. What I am trying to achieve now is to simulate very slow response from backend which would finally lead to timeout in my…
dune76
  • 373
  • 1
  • 5
  • 10
24
votes
3 answers

Spring MockMvc: match a collection of JSON objects in any order

I have an API endpoint which, when called with GET, returns an array of JSON objects in the body, like this: [ {"id": "321", "created": "2019-03-01", "updated": "2019-03-15"}, {"id": "123", "created": "2019-03-02", "updated": "2019-03-16"} ] I…
Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
22
votes
3 answers

What's the difference between MockMvc, RestAssured, and TestRestTemplate?

For all I know, MockMvc is just testing the Controller, and mocking the Service layer. Whilst RestAssured and TestRestTemplate are testing the running instance of our API. Is that correct? And what's the difference between RestAssured and Spring…
Silly Sally
  • 1,027
  • 2
  • 10
  • 15
1
2 3
49 50