In this, Spring Boot RestTemplate GET request example, learn to use RestTemplate to invoke REST GET API verify api response status code and response entity body.
To create the rest apis, use the sourcecode provided in spring boot 2 rest api example.
1. Maven dependencies
Make sure to have spring-boot-starter-test dependency in the project to be able to execute unit tests.
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > |
2. Spring boot Test Class
A test class in Spring boot application can be written like this.
@RunWith (SpringRunner. class ) @SpringBootTest (webEnvironment=WebEnvironment.RANDOM_PORT) public class SpringBootDemoApplicationTests { @LocalServerPort int randomServerPort; @Test public void testGetEmployeeListSuccess() throws URISyntaxException { } } |
3. Spring boot resttemplate get api examples
In given below example, I will first write the rest api code and then unit test which invoke the rest api and verify api response.
3.1. Simple GET request
3.1.1. REST API Code
REST API which return the list of all employees in list.
@GetMapping (path= "/employees" , produces = "application/json" ) public Employees getEmployees() { return employeeDao.getAllEmployees(); } |
3.1.2. Junit Test
Junit test with RestTemplate which invoke above API and verify the api response code as well as response body.
@Test public void testGetEmployeeListSuccess() throws URISyntaxException { RestTemplate restTemplate = new RestTemplate(); URI uri = new URI(baseUrl); ResponseEntity<String> result = restTemplate.getForEntity(uri, String. class ); //Verify request succeed Assert.assertEquals( 200 , result.getStatusCodeValue()); Assert.assertEquals( true , result.getBody().contains( "employeeList" )); } |
3.2. GET request with request headers
3.2.1. REST API Code
GET API which accepts request headers. In this example, X-COM-PERSIST header is mandatory and X-COM-LOCATION is optional.
@GetMapping (path= "/" , produces = "application/json" ) public Employees getEmployees ( @RequestHeader (name = "X-COM-PERSIST" , required = true ) String headerPersist, @RequestHeader (name = "X-COM-LOCATION" , defaultValue = "ASIA" ) String headerLocation ) { return employeeDao.getAllEmployees(); } |
3.2.2. Junit test – success scenario
Junit test with RestTemplate which invoke GET API with mandatory headers and verify the api response code as well as response body.
Note: RestTemplate getForEntity() method does not support request headers. Please use exchange() method if headers are necessary.
@Test public void testGetEmployeeListSuccessWithHeaders() throws URISyntaxException { RestTemplate restTemplate = new RestTemplate(); URI uri = new URI(baseUrl); HttpHeaders headers = new HttpHeaders(); headers.set( "X-COM-PERSIST" , "true" ); headers.set( "X-COM-LOCATION" , "USA" ); HttpEntity<Employee> requestEntity = new HttpEntity<>( null , headers); ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String. class ); //Verify request succeed Assert.assertEquals( 200 , result.getStatusCodeValue()); Assert.assertEquals( true , result.getBody().contains( "employeeList" )); } |
3.2.3. Junit test – error scenario
To produce error scenario, let’s do not add the mandatory header in request entity.
@Test public void testGetEmployeeListSuccessWithHeaders() throws URISyntaxException { RestTemplate restTemplate = new RestTemplate(); URI uri = new URI(baseUrl); HttpHeaders headers = new HttpHeaders(); headers.set( "X-COM-LOCATION" , "USA" ); HttpEntity<Employee> requestEntity = new HttpEntity<>( null , headers); try { restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String. class ); Assert.fail(); } catch (HttpClientErrorException ex) { //Verify bad request and missing header Assert.assertEquals( 400 , ex.getRawStatusCode()); Assert.assertEquals( true , ex.getResponseBodyAsString().contains( "Missing request header" )); } } |
Let me know if you have any query in this spring boot resttemplate get example.
No comments:
Post a Comment