{"id":107431,"date":"2020-11-06T07:00:00","date_gmt":"2020-11-06T05:00:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=107431"},"modified":"2020-11-04T15:36:01","modified_gmt":"2020-11-04T13:36:01","slug":"improving-spring-mock-mvc-tests","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html","title":{"rendered":"Improving Spring Mock-MVC tests"},"content":{"rendered":"<p><a href=\"https:\/\/docs.spring.io\/spring-framework\/docs\/current\/reference\/html\/testing.html#spring-mvc-test-framework\" rel=\"nofollow\">Spring Mock-MVC<\/a> can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server.<\/p>\n<p>I used Mock-MVC tests in various projects and in my experience they often become quite verbose. This doesn&#8217;t have to be bad. However, it often results in copy\/pasting code snippets around in test classes. In this post we will look at a couple of ways to clean up Spring Mock-MVC tests.<\/p>\n<h2 class=\"wp-block-heading\">Decide what to test with Mock-MVC<\/h2>\n<p>The first question we need to ask is what we want to test with Mock-MVC. Some example test scenarios are:<\/p>\n<ul class=\"wp-block-list\">\n<li>Testing only the web layer and mocking all controller dependencies.<\/li>\n<li>Testing the web layer with domain logic and mocked third party dependencies like Databases or message queues.<\/li>\n<li>Testing the complete path from web to database by replacing third party dependencies with embedded alternatives if possible (e.g. <a href=\"https:\/\/www.h2database.com\/html\/main.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">H2<\/a> or <a href=\"https:\/\/github.com\/embeddedkafka\/embedded-kafka\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">embedded-Kafka<\/a>)<\/li>\n<\/ul>\n<p>All these scenarios have their own up- and downsides. However, I think there are two simple rules we should follow:<\/p>\n<ul class=\"wp-block-list\">\n<li>Test as much in standard JUnit tests (without Spring) as possible. This improves test performance a lot and makes tests often easier to write.<\/li>\n<li>Pick the scenario(s) you want to test with Spring and be consistent in the dependencies you mock. This makes tests easier to understand and can speed them up as well. When running many different test configurations, Spring often has to re-initialize the application context which slows tests down.<\/li>\n<\/ul>\n<p>When using standard JUnit tests as much as possible the last scenario mentioned above is often a good fit. After we tested all logic with fast unit tests, we can use a few Mock-MVC tests to verify that all pieces work together, from controller to database.<\/p>\n<h2 class=\"wp-block-heading\">Cleaning up test configuration using custom annotations<\/h2>\n<p>Spring allows us to <a href=\"https:\/\/www.mscharhag.com\/spring\/annotation-composition\" target=\"_blank\" rel=\"noopener noreferrer\">compose multiple Spring annotations<\/a> to a single custom annotation.<\/p>\n<p>For example, we can create a custom @MockMvcTest annotation:<\/p>\n<div>\n<div id=\"highlighter_139931\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java color1\">@SpringBootTest<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java color1\">@TestPropertySource<\/code><code class=\"java plain\">(locations&nbsp;=&nbsp;<\/code><code class=\"java string\">\"classpath:test.properties\"<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java color1\">@AutoConfigureMockMvc<\/code><code class=\"java plain\">(secure&nbsp;=&nbsp;<\/code><code class=\"java keyword\">false<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java color1\">@Retention<\/code><code class=\"java plain\">(RetentionPolicy.RUNTIME)<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java keyword\">public<\/code>&nbsp;<code class=\"java color2\">@interface<\/code>&nbsp;<code class=\"java plain\">MockMvcTest&nbsp;{}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Our test now only needs a single annotation:<\/p>\n<div>\n<div id=\"highlighter_273413\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java color1\">@MockMvcTest<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java keyword\">public<\/code>&nbsp;<code class=\"java keyword\">class<\/code>&nbsp;<code class=\"java plain\">MyTest&nbsp;{<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">...<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This way we can clean up tests from various annotations. This is also useful to standardize Spring configuration for our test scenarios.<\/p>\n<h2 class=\"wp-block-heading\">Improving Mock-MVC requests<\/h2>\n<p>Let&#8217;s look at the following example Mock-MVC request and see how we can improve it:<\/p>\n<div>\n<div id=\"highlighter_646731\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">mockMvc.perform(put(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.contentType(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.accept(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.content(<\/code><code class=\"java string\">\"{\\\"name\\\":&nbsp;\\\"Cool&nbsp;Gadget\\\",&nbsp;\\\"description\\\":&nbsp;\\\"Looks&nbsp;cool\\\"}\"<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.header(<\/code><code class=\"java string\">\"Authorization\"<\/code><code class=\"java plain\">,&nbsp;getBasicAuthHeader(<\/code><code class=\"java string\">\"John\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"secr3t\"<\/code><code class=\"java plain\">)))<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk());<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This sends a PUT request with some JSON data and an Authorization header to <em>\/products\/42<\/em>.<\/p>\n<p>The first thing that catches someone&#8217;s eye is the JSON snippet within a Java string. This is obviously a problem as the double quote escaping required by Java strings makes it barely readable.<\/p>\n<p>Typically we should use an object that is then converted to JSON. Before we look into this approach, it is worth to mention Text blocks. <a href=\"https:\/\/www.mscharhag.com\/java\/text-blocks\" target=\"_blank\" rel=\"noopener noreferrer\">Java Text blocks<\/a> have been introduced in JDK 13 \/ 14 as preview feature. Text blocks are strings that span over multiple lines and require no double quote escaping.<\/p>\n<p>With text block we can format inline JSON in a prettier way. For example:<\/p>\n<div>\n<div id=\"highlighter_646660\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">mvc.perform(put(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.contentType(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.accept(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.content(<\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\"<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">{<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java string\">\"name\"<\/code><code class=\"java plain\">:&nbsp;<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">,<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java string\">\"description\"<\/code><code class=\"java plain\">:&nbsp;<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java string\">\"\"<\/code><code class=\"java plain\">\")<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.header(<\/code><code class=\"java string\">\"Authorization\"<\/code><code class=\"java plain\">,&nbsp;getBasicAuthHeader(<\/code><code class=\"java string\">\"John\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"secr3t\"<\/code><code class=\"java plain\">)))<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk());&nbsp;&nbsp;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>In certain situations this can be useful.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>However, we should still prefer objects that are converted to JSON instead of manually writing and maintaining JSON strings.<\/p>\n<p>For example:<\/p>\n<div>\n<div id=\"highlighter_904369\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">Product&nbsp;product&nbsp;=&nbsp;<\/code><code class=\"java keyword\">new<\/code>&nbsp;<code class=\"java plain\">Product(<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">mvc.perform(put(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.contentType(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.accept(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.content(objectToJson(product))<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.header(<\/code><code class=\"java string\">\"Authorization\"<\/code><code class=\"java plain\">,&nbsp;getBasicAuthHeader(<\/code><code class=\"java string\">\"John\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"secr3t\"<\/code><code class=\"java plain\">)))<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk());<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Here we create a product object and convert it to JSON with a small objectToJson(..) helper method. This helps a bit. Nevertheless, we can do better.<\/p>\n<p>Our request contains a lot of elements that can be grouped together. When building a JSON REST-API it is likely that we often have to send similar PUT request. Therefore, we create a small static shortcut method:<\/p>\n<div>\n<div id=\"highlighter_383449\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code>&nbsp;<code class=\"java keyword\">static<\/code>&nbsp;<code class=\"java plain\">MockHttpServletRequestBuilder&nbsp;putJson(String&nbsp;uri,&nbsp;Object&nbsp;body)&nbsp;{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">try<\/code>&nbsp;<code class=\"java plain\">{<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">String&nbsp;json&nbsp;=&nbsp;<\/code><code class=\"java keyword\">new<\/code>&nbsp;<code class=\"java plain\">ObjectMapper().writeValueAsString(body);<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code>&nbsp;<code class=\"java plain\">put(uri)<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.contentType(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.accept(MediaType.APPLICATION_JSON)<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.content(json);<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}&nbsp;<\/code><code class=\"java keyword\">catch<\/code>&nbsp;<code class=\"java plain\">(JsonProcessingException&nbsp;e)&nbsp;{<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">throw<\/code>&nbsp;<code class=\"java keyword\">new<\/code>&nbsp;<code class=\"java plain\">RuntimeException(e);<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This method converts the body parameter to JSON using a Jackson ObjectMapper. It then creates a PUT request and sets Accept and Content-Type headers.<\/p>\n<p>This reusable method simplifies our test request a lot:<\/p>\n<div>\n<div id=\"highlighter_642197\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">Product&nbsp;product&nbsp;=&nbsp;<\/code><code class=\"java keyword\">new<\/code>&nbsp;<code class=\"java plain\">Product(<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">mvc.perform(putJson(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">,&nbsp;product)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.header(<\/code><code class=\"java string\">\"Authorization\"<\/code><code class=\"java plain\">,&nbsp;getBasicAuthHeader(<\/code><code class=\"java string\">\"John\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"secr3t\"<\/code><code class=\"java plain\">)))<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk())<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The nice thing here is that we do not lose flexibility. Our putJson(..) method returns a MockHttpServletRequestBuilder. This allows us to add additional request properties within tests if required (like the Authorization header in this example).<\/p>\n<p>Authentication headers are another topic we often have to deal with when writing Spring Mock-MVC tests. However, we should not add authentication headers to our previous putJson(..) method. Even if all PUT requests require authentication we stay more flexible if we deal with authentication in a different way.<\/p>\n<p>RequestPostProcessors can help us with this. As the name suggests, RequestPostProcessors can be used to process the request. We can use this to add custom headers or other information to the request.<\/p>\n<p>For example:<\/p>\n<div>\n<div id=\"highlighter_427213\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code>&nbsp;<code class=\"java keyword\">static<\/code>&nbsp;<code class=\"java plain\">RequestPostProcessor&nbsp;authentication()&nbsp;{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code>&nbsp;<code class=\"java plain\">request&nbsp;-&gt;&nbsp;{<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">request.addHeader(<\/code><code class=\"java string\">\"Authorization\"<\/code><code class=\"java plain\">,&nbsp;getBasicAuthHeader(<\/code><code class=\"java string\">\"John\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"secr3t\"<\/code><code class=\"java plain\">));<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code>&nbsp;<code class=\"java plain\">request;<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">};<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java plain\">}&nbsp;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The authentication() method returns a RequestPostProcessor which adds Basic-Authentication to the request. We can apply this RequestPostProcessor in our test using the with(..) method:<\/p>\n<div>\n<div id=\"highlighter_305682\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">Product&nbsp;product&nbsp;=&nbsp;<\/code><code class=\"java keyword\">new<\/code>&nbsp;<code class=\"java plain\">Product(<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">mvc.perform(putJson(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">,&nbsp;product).with(authentication()))<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk())<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This does not only simplify our test request. If we change the request header format we now only need to modify a single method to fix the tests. Additionally putJson(url, data).with(authentication()) is also quite expressive to read.<\/p>\n<h2 class=\"wp-block-heading\">Improving response verification<\/h2>\n<p>Now let&#8217;s see how we can improve response verification.<\/p>\n<p>We start with the following example:<\/p>\n<div>\n<div id=\"highlighter_954470\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">mvc.perform(get(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk())<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(header().string(<\/code><code class=\"java string\">\"Cache-Control\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"no-cache\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(jsonPath(<\/code><code class=\"java string\">\"$.name\"<\/code><code class=\"java plain\">).value(<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(jsonPath(<\/code><code class=\"java string\">\"$.description\"<\/code><code class=\"java plain\">).value(<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><code class=\"java plain\">));<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Here we check the HTTP status code, make sure the Cache-Control header is set to no-cache and use JSON-Path expressions to verify the response payload.<\/p>\n<p>The Cache-Control header looks like something we probably need to check for multiple responses. In this case, it can be a good idea to come up with a small shortcut method:<\/p>\n<div>\n<div id=\"highlighter_293578\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code>&nbsp;<code class=\"java plain\">ResultMatcher&nbsp;noCacheHeader()&nbsp;{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code>&nbsp;<code class=\"java plain\">header().string(<\/code><code class=\"java string\">\"Cache-Control\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"no-cache\"<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>We can now apply the check by passing noCacheHeader() to andExpect(..):<\/p>\n<div>\n<div id=\"highlighter_15605\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">mvc.perform(get(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk())<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(noCacheHeader())<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(jsonPath(<\/code><code class=\"java string\">\"$.name\"<\/code><code class=\"java plain\">).value(<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(jsonPath(<\/code><code class=\"java string\">\"$.description\"<\/code><code class=\"java plain\">).value(<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><code class=\"java plain\">));<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The same approach can be used to verify the response body.<\/p>\n<p>For example we can create a small product(..) method that compares the response JSON with a given Product object:<\/p>\n<div>\n<div id=\"highlighter_218007\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code>&nbsp;<code class=\"java keyword\">static<\/code>&nbsp;<code class=\"java plain\">ResultMatcher&nbsp;product(String&nbsp;prefix,&nbsp;Product&nbsp;product)&nbsp;{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code>&nbsp;<code class=\"java plain\">ResultMatcher.matchAll(<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">jsonPath(prefix&nbsp;+&nbsp;<\/code><code class=\"java string\">\".name\"<\/code><code class=\"java plain\">).value(product.getName()),<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">jsonPath(prefix&nbsp;+&nbsp;<\/code><code class=\"java string\">\".description\"<\/code><code class=\"java plain\">).value(product.getDescription())<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Our test now looks like this:<\/p>\n<div>\n<div id=\"highlighter_620386\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">Product&nbsp;product&nbsp;=&nbsp;<\/code><code class=\"java keyword\">new<\/code>&nbsp;<code class=\"java plain\">Product(<\/code><code class=\"java string\">\"Cool&nbsp;Gadget\"<\/code><code class=\"java plain\">,&nbsp;<\/code><code class=\"java string\">\"Looks&nbsp;cool\"<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">mvc.perform(get(<\/code><code class=\"java string\">\"\/products\/42\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk())<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(noCacheHeader())<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(product(<\/code><code class=\"java string\">\"$\"<\/code><code class=\"java plain\">,&nbsp;product));<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Note that the prefix parameter gives us flexibility. The object we want to check might not always be located at the JSON root level of the response.<\/p>\n<p>Assume a request might return a collection of products. We can then use the prefix parameter to select each product in the collection. For example:<\/p>\n<div>\n<div id=\"highlighter_76995\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">Product&nbsp;product0&nbsp;=&nbsp;..<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">Product&nbsp;product1&nbsp;=&nbsp;..<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java plain\">mvc.perform(get(<\/code><code class=\"java string\">\"\/products\"<\/code><code class=\"java plain\">))<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(status().isOk())<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(product(<\/code><code class=\"java string\">\"$[0]\"<\/code><code class=\"java plain\">,&nbsp;product0))<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">.andExpect(product(<\/code><code class=\"java string\">\"$[1]\"<\/code><code class=\"java plain\">,&nbsp;product1));<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code>&nbsp;<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>With ResultMatcher methods you avoid scattering the exact response data structure over many tests. This again supports refactorings.<\/p>\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n<p>We looked into a few ways to reduce verbosity in Spring Mock-MVC tests. Before we even start writing Mock-MVC tests we should decide what we want to test and what parts of the application should be replaced with mocks. Often it is a good idea to test as much as possible with standard unit tests (without Spring and Mock-MVC).<\/p>\n<p>We can use custom test annotations to standardize our Spring Mock-MVC test setup. With small shortcut methods and RequestPostProcessors we can move reusable request code out of test methods. Custom ResultMatchers can be used to improve response checks.<\/p>\n<p>You can find the example code on <a href=\"https:\/\/github.com\/mscharhag\/blog-examples\/tree\/master\/mockmvc-testing\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a>.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>Published on Java Code Geeks with permission by Michael Scharhag, partner at our <a href=\"\/\/www.javacodegeeks.com\/join-us\/jcg\/\" target=\"_blank\" rel=\"noopener noreferrer\">JCG program<\/a>. See the original article here: <a href=\"https:\/\/www.mscharhag.com\/spring\/clean-mock-mvc-tests\" target=\"_blank\" rel=\"noopener noreferrer\">Improving Spring Mock-MVC tests<\/a><\/p>\n<p>Opinions expressed by Java Code Geeks contributors are their own.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I used Mock-MVC tests in various projects and in my experience they often become quite verbose. This doesn&#8217;t have to be bad. However, it often results in copy\/pasting code &hellip;<\/p>\n","protected":false},"author":514,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[30,2032,150],"class_list":["post-107431","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spring","tag-spring-mock","tag-spring-mvc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Improving Spring Mock-MVC tests - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Improving Spring Mock-MVC tests - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-06T05:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Scharhag\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/mscharhag\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Scharhag\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html\"},\"author\":{\"name\":\"Michael Scharhag\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/0f0f81e875d40e3f820392e0ffce65d1\"},\"headline\":\"Improving Spring Mock-MVC tests\",\"datePublished\":\"2020-11-06T05:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html\"},\"wordCount\":1154,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Spring\",\"spring Mock\",\"Spring MVC\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html\",\"name\":\"Improving Spring Mock-MVC tests - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2020-11-06T05:00:00+00:00\",\"description\":\"Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2020\\\/11\\\/improving-spring-mock-mvc-tests.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Improving Spring Mock-MVC tests\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/0f0f81e875d40e3f820392e0ffce65d1\",\"name\":\"Michael Scharhag\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/52283459bfc820fb6a66704b3eccc771a1d8a63a0bdbbe1651bb5cb383a42148?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/52283459bfc820fb6a66704b3eccc771a1d8a63a0bdbbe1651bb5cb383a42148?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/52283459bfc820fb6a66704b3eccc771a1d8a63a0bdbbe1651bb5cb383a42148?s=96&d=mm&r=g\",\"caption\":\"Michael Scharhag\"},\"description\":\"Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.\",\"sameAs\":[\"http:\\\/\\\/www.mscharhag.com\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/mscharhag\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/michael-scharhag\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Improving Spring Mock-MVC tests - Java Code Geeks","description":"Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html","og_locale":"en_US","og_type":"article","og_title":"Improving Spring Mock-MVC tests - Java Code Geeks","og_description":"Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I","og_url":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2020-11-06T05:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Michael Scharhag","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/mscharhag","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Michael Scharhag","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html"},"author":{"name":"Michael Scharhag","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/0f0f81e875d40e3f820392e0ffce65d1"},"headline":"Improving Spring Mock-MVC tests","datePublished":"2020-11-06T05:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html"},"wordCount":1154,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Spring","spring Mock","Spring MVC"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html","url":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html","name":"Improving Spring Mock-MVC tests - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2020-11-06T05:00:00+00:00","description":"Spring Mock-MVC can be a great way to test Spring Boot REST APIs. Mock-MVC allows us to test Spring-MVC request handling without running a real server. I","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2020\/11\/improving-spring-mock-mvc-tests.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Improving Spring Mock-MVC tests"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/0f0f81e875d40e3f820392e0ffce65d1","name":"Michael Scharhag","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/52283459bfc820fb6a66704b3eccc771a1d8a63a0bdbbe1651bb5cb383a42148?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/52283459bfc820fb6a66704b3eccc771a1d8a63a0bdbbe1651bb5cb383a42148?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/52283459bfc820fb6a66704b3eccc771a1d8a63a0bdbbe1651bb5cb383a42148?s=96&d=mm&r=g","caption":"Michael Scharhag"},"description":"Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.","sameAs":["http:\/\/www.mscharhag.com\/","https:\/\/x.com\/https:\/\/twitter.com\/mscharhag"],"url":"https:\/\/www.javacodegeeks.com\/author\/michael-scharhag"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/107431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/514"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=107431"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/107431\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=107431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=107431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=107431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}