
Play Store Application link β Spring Boot in 13 steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Spring-Boot-Testing
Step 1: Setting Up a Spring Boot Project
Using Spring Initializr
- Go to https://start.spring.io/.
- Fill in the details:
- Project: Maven
- Language: Java
- Spring Boot: 3.x.x
- Dependencies: Spring Web, Spring Boot Starter Test
- Click Generate, and extract the downloaded ZIP file.
Import into IDE
- Open your IDE (e.g., IntelliJ or Eclipse).
- Import the extracted project as a Maven project.
Step 2: Writing Unit Tests with JUnit 5
Example: Testing a Simple Service
Create a service class:
package com.example.demo;
import org.springframework.stereotype.Service;
@Service
public class CalculatorService {
public int add(int a, int b) {
return a + b;
}
}
Write a unit test for the service:
package com.example.demo;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorServiceTest {
private final CalculatorService calculatorService = new CalculatorService();
@Test
void testAdd() {
int result = calculatorService.add(2, 3);
assertEquals(5, result, "2 + 3 should equal 5");
}
}
Run the test to verify functionality.
Step 3: Integration Testing with @SpringBootTest
Example: Testing a REST Controller
Create a controller:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Write an integration test:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HelloControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
void testHelloEndpoint() {
ResponseEntity<String> response = restTemplate.getForEntity("/hello", String.class);
assertThat(response.getBody()).isEqualTo("Hello, World!");
}
}
Step 4: Mocking with Mockito
Example: Mocking Dependencies in a Service
Create a dependent service:
package com.example.demo;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
public String getMessage() {
return "Hello from MessageService";
}
}
Update CalculatorService
to depend on MessageService
:
package com.example.demo;
import org.springframework.stereotype.Service;
@Service
public class CalculatorService {
private final MessageService messageService;
public CalculatorService(MessageService messageService) {
this.messageService = messageService;
}
public String addWithMessage(int a, int b) {
return messageService.getMessage() + ": " + (a + b);
}
}
Write a test using Mockito:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class CalculatorServiceMockTest {
@Mock
private MessageService messageService;
@InjectMocks
private CalculatorService calculatorService;
public CalculatorServiceMockTest() {
MockitoAnnotations.openMocks(this);
}
@Test
void testAddWithMessage() {
when(messageService.getMessage()).thenReturn("Mocked Message");
String result = calculatorService.addWithMessage(2, 3);
assertEquals("Mocked Message: 5", result);
verify(messageService, times(1)).getMessage();
}
}
Step 5: Testing REST APIs with TestRestTemplate
Example: Testing Error Responses
Update the controller:
package com.example.demo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ErrorController {
@GetMapping("/error")
public ResponseEntity<String> error() {
return new ResponseEntity<>("Something went wrong!", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Write a test for the error response:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ErrorControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
void testErrorEndpoint() {
ResponseEntity<String> response = restTemplate.getForEntity("/error", String.class);
assertThat(response.getStatusCodeValue()).isEqualTo(500);
assertThat(response.getBody()).isEqualTo("Something went wrong!");
}
}
Conclusion
In this guide, we covered:
- Writing unit tests with JUnit 5.
- Performing integration tests with
@SpringBootTest
. - Mocking dependencies using Mockito.
- Testing REST APIs with
TestRestTemplate
.