Spring Boot REST Test Using Embedded Container

Spring Boot REST Test Using Embedded Container

In this REST example we will use same project that we created earlier.To create project you can follow below link.

REST API Example Using Spring Boot


Or you can download above project from given link.

Spring Boot REST Example


To test any spring web application, we need to test that method is properly handling Request methods like GET, POST, so to test it we need to request HTTP and need to check that it process correctly.

Spring provides two ways by which we can test above scenario.

  • Mock MVC : Server is not started in it and it is mocked
  • Web Integration Test : Application is started in embedded server.So it is slower but actual test

In this example we are using Web Integration

Web Integration has deprecated but still we can test our application in embedded container.@SpringBootTest provided this facility. To test GET API we are using following class which contains a GET API

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestService {	
	
	@RequestMapping(value = "/api", method = RequestMethod.GET)
	public String getMessage(@RequestParam("name") String name){
		return "Hello" + " " + name;		
	}
}

If we have RestService on separate package then we need to include @Import({ RestService.class }) in spring boot class , in our example


 
@SpringBootApplication
@Import({ RestService.class })
public class FirstSpringBootPreparationApplication {

Now create a class to test above API say FirstSpringBootPreparationApplicationTestsByEmbeddedServer and copy code

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FirstSpringBootPreparationApplicationTestsByEmbeddedServer {

	@Value("")
	private int port;

	@Test
	public void pageNotFound() {
		try {
			RestTemplate rest = new RestTemplate();
			String response = rest.getForObject(
					"http://localhost:{port}/preparationforinterview/api?name=developer", String.class,
					port);
			Assert.assertEquals("Hello developer", response);
		} catch (HttpClientErrorException e) {
			throw e;
		}
	}
}

Right click on FirstSpringBootPreparationApplicationTestsByEmbeddedServer and run as Junit. It will pass test case.

Explain :

When we execute above test ,it starts tomcat on any random port because we have provided SpringBootTest.WebEnvironment.RANDOM_PORT.We provided this because by default tomcat runs on 8080 port and if any application is already running then our embedded tomcat can pick any random port.That is the reason we had to keep porn in variable.This property is provided by spring boot, we don't need to declare it.

After that we create an instance of RestTemplate and call our API and assert output with expected

Download source code

Spring Boot REST API Test with Embedded server



Visit Others Links

Spring Boot Introduction
Spring Boot Setup
REST API Example Using Spring Boot
Spring Boot REST API Test with Mock MVC Using standaloneSetup
Spring Boot REST API Test with Mock MVC Using webAppContextSetup
Spring Boot REST API Test with Embedded Server
Actuator In Spring Boot
Deployment Of Spring Boot In External Tomcat
Spring Boot with OAuth
Top Spring Boot Interview Question