![]() |
The Spring Framework is an open-source framework for creating applications, providing many annotations and features to make development easier. Spring Boot simplifies the setup and development of the Spring applications. One useful utility for testing RESTful web services in Spring Boot is TestRestTemplate, which provides additional features such as auto-configuration and simplified initialization. Configure Spring Boot TestRestTemplatedependencies { @SpringBootTest Functionality of TestRestTemplate
Where It Is Used:The TestRestTemplate primarily used in integration testing where we need to interact with our RESTful web services. It is useful in scenarios such as,
Prerequisites:
Tools and Technologies:
Dependencies:dependencies { Project Folder Structure:![]() Steps to Configure Spring Boot TestRestTemplateHere,we created a simple Spring Project with Gradle tool with required gradle dependencies for the project. Step 1: Create a Rest ControllerOnce Project is successfully created, create one class in the main package of the project with name MyRestController and This class annotated with @RestController. This class is used for defining the API end points. Here we created a sayHello() which has return type has ResponseEntity. This API return a ResponseEntity object as output while hitting this api endpoint. MyRestController.java:
Step 2: Create the Test ClassNow go to testing folder in the spring project. It is located at src/test/java by default the spring framework create a test class in this package. In this class we write our logic. First Setup Web Environment port in the @SpringBootTest annotation. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) After that We Autowired the TestRestTemplate by using @Autowired annotation. RestTemplate that simplifies testing by providing additional features such as auto-configuration and simplified initialization. After this we created a test method testSayHello() by using @Test annotation. In that method we define the required logic. ResponseEntity<String> response = testRestTemplate.getForEntity("/api/hello", String.class); This method first get response from the /api/hello api, Then check the HttpStatus code If It is equal then we return ResponseEntity object as output. TestRestTemplateApplicationTests.java:
Step 3: Run the ApplicationOnce all the test cases and required business logic is developed, then run this project as Spring Boot App. By default this project runs on port number 8080 with Tomcat server. ![]() Step 4: Run the TestsNow, run this project as Junit Test. If there is no errors then we will get the below output, otherwise we will get error in the Java console. ![]() Step 5: Test the APIAPIs Testing:The Hello API is a GET Mapping HTTP request. While the end user while hit this API, this API returns a ResponseEntity as output. http://localhost:8080/api/hello @GetMapping("/hello") When we hit this API, we will get the below output. Output:![]()
|
Reffered: https://www.geeksforgeeks.org
Advance Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |