{"id":593,"date":"2011-10-31T19:59:00","date_gmt":"2011-10-31T19:59:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/java-restful-api-integration-testing.html"},"modified":"2012-10-21T20:22:16","modified_gmt":"2012-10-21T20:22:16","slug":"java-restful-api-integration-testing","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html","title":{"rendered":"Java RESTful API integration testing"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">This post will focus on basic principles and mechanics of writing <span style=\"font-weight: bold\">Java integration tests<\/span> for a RESTful API (with a JSON payload).<\/p>\n<p>The goal is to provide an introduction to the technologies and to write some tests for basic correctness. The examples will consume the latest version of the GitHub REST API.<\/p>\n<p>For an internal application, this kind of testing will usually run as a late step in a Continuous Integration process, consuming the REST API after it has already been deployed.<\/p>\n<p>When testing a REST resource, there are usually a few orthogonal responsibilities the tests should focus on:<\/p>\n<ul style=\"text-align: left\">\n<li>the HTTP <span style=\"font-weight: bold\">response code<\/span><\/li>\n<li>other HTTP <span style=\"font-weight: bold\">headers<\/span> in the response<\/li>\n<li>the <span style=\"font-weight: bold\">payload<\/span> (JSON, XML)<\/li>\n<\/ul>\n<p>Each test should only focus on a single responsibility and include a single assertion. Focusing on a clear separation always has benefits, but when doing this kind of black box testing it\u2019s even more important, as the general tendency is to write complex test scenarios in the very beginning.<\/p>\n<p>Another important aspect of the integration tests is adherence to the Single Level of Abstraction Principle \u2013 the logic within a test should be written at a high level. Details such as creating the request, sending the HTTP request to the server, dealing with IO, etc should not be done inline but via utility methods.<\/p>\n<p><span style=\"font-weight: bold\">Testing the HTTP response code<\/span><\/p>\n<pre class=\"brush:java\">@Test\r\npublic void givenUserDoesNotExists_whenUserInfoIsRetrieved_then404IsReceived()\r\n      throws ClientProtocolException, IOException{\r\n   \/\/ Given\r\n   String name = randomAlphabetic( 8 );\r\n   HttpUriRequest request = new HttpGet( \"https:\/\/api.github.com\/users\/\" + name );\r\n   \r\n   \/\/ When\r\n   HttpResponse httpResponse = httpClient.execute( request );\r\n   \r\n   \/\/ Then\r\n   RestAssert.assertResponseCodeIs( httpResponse, 404 );\r\n}\r\n<\/pre>\n<p>This is a rather simple test, which verifies that a basic happy path is working, without adding to much complexity to the test suite. If, for whatever reason it fails, then there is no need to look at any other test for this URL until this is fixed. Because verifying the response code is one of the most common assertions of the integration testing suite, a <span style=\"font-weight: bold\">custom assertion<\/span> is used.<\/p>\n<pre class=\"brush:java\">public static void assertResponseCodeIs\r\n      ( final HttpResponse response, final int expectedCode ){\r\n   final int statusCode = httpResponse.getStatusLine().getStatusCode();\r\n   assertEquals( expectedCode, statusCode );\r\n}\r\n<\/pre>\n<p><span style=\"font-weight: bold\">Testing other headers of the HTTP response<\/span><\/p>\n<pre class=\"brush:java\">@Test\r\npublic void givenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson()\r\n      throws ClientProtocolException, IOException{\r\n   \/\/ Given\r\n   String jsonMimeType = \"application\/json\";\r\n   HttpUriRequest request = new HttpGet( \"https:\/\/api.github.com\/users\/eugenp\" );\r\n   \r\n   \/\/ When\r\n   HttpResponse response = this.httpClient.execute( request );\r\n   \r\n   \/\/ Then\r\n   String mimeType = EntityUtils.getContentMimeType( response.getEntity() );\r\n   assertEquals( jsonMimeType, mimeType );\r\n}\r\n<\/pre>\n<p>This ensures that the response when requesting the details of the user is actually JSON. There is a logical progression of the functionality under test \u2013 first the response code, to ensure that the request was OK, then the mime type of the request, and only then the verification that the actual JSON is correct.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"font-weight: bold\">Testing the JSON payload of the HTTP response<\/span><\/p>\n<pre class=\"brush:java\">@Test\r\npublic void givenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect()\r\n      throws ClientProtocolException, IOException{\r\n   \/\/ Given\r\n   HttpUriRequest request = new HttpGet( \"https:\/\/api.github.com\/users\/eugenp\" );\r\n   \r\n   \/\/ When\r\n   HttpResponse response = new DefaultHttpClient().execute( request );\r\n   \r\n   \/\/ Then\r\n   GitHubUser resource =\r\n      RetrieveUtil.retrieveResourceFromResponse( response, GitHubUser.class );\r\n   assertThat( \"eugenp\", Matchers.is( resource.getLogin() ) );\r\n}\r\n<\/pre>\n<p>In this case, I know the default representation of GitHub resources is JSON, but usually the Content-Type header of the response should be tested alongside the Accept header of the request \u2013 the client asks for a particular type of representation via Accept, which the server should honor.<\/p>\n<p><span style=\"font-weight: bold\">The Utilities for testing<\/span><\/p>\n<p>Here are the utilities that enable the tests to remain at a higher level of abstraction:<\/p>\n<p>&#8211; decorate the HTTP request with the JSON payload (or directly with the POJO):<\/p>\n<pre class=\"brush:java\">public static &lt; T &gt;HttpEntityEnclosingRequest decorateRequestWithResource\r\n      ( final HttpEntityEnclosingRequest request, final T resource )\r\n      throws IOException{\r\n   Preconditions.checkNotNull( request );\r\n   Preconditions.checkNotNull( resource );\r\n   \r\n   final String resourceAsJson = JsonUtil.convertResourceToJson( resource );\r\n   return JsonUtil.decorateRequestWithJson( request, resourceAsJson );\r\n}\r\n\r\npublic static HttpEntityEnclosingRequest decorateRequestWithJson\r\n      ( final HttpEntityEnclosingRequest request, final String json )\r\n      throws UnsupportedEncodingException{\r\n   Preconditions.checkNotNull( request );\r\n   Preconditions.checkNotNull( json );\r\n   \r\n   request.setHeader( HttpConstants.CONTENT_TYPE_HEADER, \"application\/json\" );\r\n   request.setEntity( new StringEntity( json ) );\r\n   \r\n   return request;\r\n}\r\n<\/pre>\n<p>&#8211; retrieve the JSON payload (or directly the POJO) from the HTTP response:<\/p>\n<pre class=\"brush:java\">public static String retrieveJsonFromResponse( final HttpResponse response )\r\n      throws IOException{\r\n   Preconditions.checkNotNull( response );\r\n   \r\n   return IOUtils.toString( response.getEntity().getContent() );\r\n}\r\n\r\npublic static &lt; T &gt;T retrieveResourceFromResponse\r\n      ( final HttpResponse response, final Class&lt; T &gt; clazz ) throws IOException{\r\n   Preconditions.checkNotNull( response );\r\n   Preconditions.checkNotNull( clazz );\r\n   \r\n   final String jsonFromResponse = retrieveJsonFromResponse( response );\r\n   return ConvertUtil.convertJsonToResource( jsonFromResponse, clazz );\r\n}\r\n<\/pre>\n<p>&#8211; conversion utilities to and from java object (POJO) to JSON:<\/p>\n<pre class=\"brush:java\">public static &lt; T &gt;String convertResourceToJson( final T resource )\r\n      throws IOException{\r\n   Preconditions.checkNotNull( resource );\r\n  \r\n  return new ObjectMapper().writeValueAsString( resource );\r\n}\r\n\r\npublic static &lt; T &gt;T convertJsonToResource\r\n      ( final String json, final Class&lt; T &gt; clazzOfResource ) throws IOException{\r\n  Preconditions.checkNotNull( json );\r\n  Preconditions.checkNotNull( clazzOfResource );\r\n  \r\n  return new ObjectMapper().readValue( json, clazzOfResource );\r\n}\r\n<\/pre>\n<p><span style=\"font-weight: bold\">Dependencies<\/span><\/p>\n<p>The utilities and tests make use of of the following libraries, all available in Maven central:<\/p>\n<ul style=\"text-align: left\">\n<li><a href=\"http:\/\/hc.apache.org\/httpcomponents-core-ga\/\">Apache HttpCore<\/a> and <a href=\"http:\/\/hc.apache.org\/httpcomponents-client-ga\/index.html\">HttpClient<\/a><\/li>\n<li><a href=\"http:\/\/commons.apache.org\/io\/\">Apache Commons IO<\/a><\/li>\n<li><a href=\"http:\/\/commons.apache.org\/lang\/\">Apache Commons Lang<\/a><\/li>\n<li><a href=\"http:\/\/jackson.codehaus.org\/Home\">Jackson<\/a><\/li>\n<li><a href=\"http:\/\/code.google.com\/p\/guava-libraries\/\">Guava<\/a><\/li>\n<li><a href=\"http:\/\/code.google.com\/p\/hamcrest\/\">Hamcrest<\/a><\/li>\n<\/ul>\n<p><span style=\"font-weight: bold\">Conclusion<\/span><\/p>\n<p>This is only one part of what the complete integration testing suite should be. The tests focus on ensuring basic correctness for the REST API, without going into more complex scenarios, discoverability of the API, consumption of different representations for the same resource or other more advanced areas. I will address these in a further post, in the meantime checkout the <a href=\"https:\/\/github.com\/eugenp\/rest-testing\">full project on github<\/a>.<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/www.baeldung.com\/2011\/10\/13\/integration-testing-a-rest-api\/\">Introduction to Java integration testing for a RESTful API<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Eugen Paraschiv at the <a href=\"http:\/\/www.baeldung.com\/\">baeldung blog<\/a>.<\/p>\n<div style=\"margin: 0px\"><strong><i>Related Articles :<\/i><\/strong><\/div>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/01\/restful-web-services-with-resteasy-jax.html\">RESTful Web Services with RESTeasy JAX-RS on Tomcat 7 \u2013 Eclipse and Maven project<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2010\/06\/spring-3-restful-web-services.html\">Spring 3 RESTful Web Services<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/spring-3-testing-with-junit-4.html\">Spring 3 Testing with JUnit 4 &#8211; ContextConfiguration and AbstractTransactionalJUnit4SpringContextTests<\/a> <\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/10\/code-coverage-with-unit-integration.html\">Code coverage with unit &amp; integration tests<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/07\/jqgrid-rest-ajax-spring-mvc-integration.html\">jqGrid, REST, AJAX and Spring MVC Integration<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/09\/rules-in-junit-49-beta-3.html\">Rules in JUnit 4.9 (beta 3)<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/p\/java-tutorials.html\">Java Tutorials and Android Tutorials list<\/a> <\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide an introduction to the technologies and to write some tests for basic correctness. The examples will consume the latest version of the GitHub REST API. For an internal &hellip;<\/p>\n","protected":false},"author":104,"featured_media":176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[274,54],"class_list":["post-593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-junit","tag-restful-web-services"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java RESTful API integration testing - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide\" \/>\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\/2011\/10\/java-restful-api-integration-testing.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java RESTful API integration testing - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.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=\"2011-10-31T19:59:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:22:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.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=\"Eugen Paraschiv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/baeldung\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Eugen Paraschiv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html\"},\"author\":{\"name\":\"Eugen Paraschiv\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7a8ad27f4bb34bb3664fda07d3142bc4\"},\"headline\":\"Java RESTful API integration testing\",\"datePublished\":\"2011-10-31T19:59:00+00:00\",\"dateModified\":\"2012-10-21T20:22:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html\"},\"wordCount\":638,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"keywords\":[\"JUnit\",\"RESTful Web Services\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html\",\"name\":\"Java RESTful API integration testing - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"datePublished\":\"2011-10-31T19:59:00+00:00\",\"dateModified\":\"2012-10-21T20:22:16+00:00\",\"description\":\"This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/10\\\/java-restful-api-integration-testing.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\":\"Java RESTful API integration testing\"}]},{\"@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\\\/7a8ad27f4bb34bb3664fda07d3142bc4\",\"name\":\"Eugen Paraschiv\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g\",\"caption\":\"Eugen Paraschiv\"},\"sameAs\":[\"http:\\\/\\\/www.baeldung.com\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/in\\\/eugenparaschiv\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/twitter.com\\\/baeldung\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Eugen-Paraschiv\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java RESTful API integration testing - Java Code Geeks","description":"This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide","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\/2011\/10\/java-restful-api-integration-testing.html","og_locale":"en_US","og_type":"article","og_title":"Java RESTful API integration testing - Java Code Geeks","og_description":"This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide","og_url":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-10-31T19:59:00+00:00","article_modified_time":"2012-10-21T20:22:16+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","type":"image\/jpeg"}],"author":"Eugen Paraschiv","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/baeldung","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Eugen Paraschiv","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html"},"author":{"name":"Eugen Paraschiv","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7a8ad27f4bb34bb3664fda07d3142bc4"},"headline":"Java RESTful API integration testing","datePublished":"2011-10-31T19:59:00+00:00","dateModified":"2012-10-21T20:22:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html"},"wordCount":638,"commentCount":3,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","keywords":["JUnit","RESTful Web Services"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html","url":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html","name":"Java RESTful API integration testing - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","datePublished":"2011-10-31T19:59:00+00:00","dateModified":"2012-10-21T20:22:16+00:00","description":"This post will focus on basic principles and mechanics of writing Java integration tests for a RESTful API (with a JSON payload). The goal is to provide","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/10\/java-restful-api-integration-testing.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":"Java RESTful API integration testing"}]},{"@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\/7a8ad27f4bb34bb3664fda07d3142bc4","name":"Eugen Paraschiv","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g","caption":"Eugen Paraschiv"},"sameAs":["http:\/\/www.baeldung.com\/","http:\/\/www.linkedin.com\/in\/eugenparaschiv","https:\/\/x.com\/http:\/\/twitter.com\/baeldung"],"url":"https:\/\/www.javacodegeeks.com\/author\/Eugen-Paraschiv"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/593","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\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/176"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}