Testing is a crucial aspect of any software development lifecycle. With Spring Boot, testing becomes easier thanks to its robust testing support out-of-the-box. In this post, weβll cover the essentials of Spring Boot application testing, including how to write unit tests, integration tests, and utilize the Spring TestContext framework effectively.
Whether you’re new to testing or just new to Spring Boot, this guide will help you set up and write your first test cases quickly and cleanly.

π¦ Maven Dependencies
To get started make sure your pom.xml includes the following dependencies:
org.springframework.boot
spring-boot-starter-test
test
org.mockito
mockito-core
test
The spring-boot-starter-test includes JUnit 5, Hamcrest, Mockito, AssertJ, and Spring Test libraries, which are all commonly used for application testing.
π§© Project Structure
src
βββ main
β βββ java
β βββ com
β βββ kscodes
β βββ springboot
β βββ controller
β βββ service
β βββ model
βββ test
βββ java
βββ com
βββ kscodes
βββ springboot
βββ controller
βββ service
π§ͺ Unit Testing with JUnit 5 and Mockito
Letβs test a simple service method using Mockito.
π¨βπ» Example Service Class
package com.kscodes.springboot.service;
import org.springframework.stereotype.Service;
@Service
public class CalculatorService {
public int add(int a, int b) {
return a + b;
}
}
β Test Class for CalculatorService
package com.kscodes.springboot.service;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculatorServiceTest {
private final CalculatorService calculatorService = new CalculatorService();
@Test
public void testAdd() {
int result = calculatorService.add(2, 3);
assertEquals(5, result);
}
}
This is a basic unit test with JUnit 5, part of Spring Boot testing best practices. It tests individual components without requiring Springβs ApplicationContext.
π Integration Testing with @SpringBootTest
When you want to test your full application context, use @SpringBootTest.
π Controller Example
package com.kscodes.springboot.controller;
import com.kscodes.springboot.service.CalculatorService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class CalculatorController {
private final CalculatorService calculatorService;
public CalculatorController(CalculatorService calculatorService) {
this.calculatorService = calculatorService;
}
@GetMapping("/add")
public int add(@RequestParam int a, @RequestParam int b) {
return calculatorService.add(a, b);
}
}
π§ͺ Test Using TestRestTemplate
package com.kscodes.springboot.controller;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CalculatorControllerIntegrationTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testAddEndpoint() {
int result = this.restTemplate.getForObject("/api/add?a=5&b=10", Integer.class);
assertThat(result).isEqualTo(15);
}
}
Using TestRestTemplate allows you to test REST endpoints end-to-end, a powerful part of Spring Boot application testing strategies.
π― Best Practices for Spring Boot Application Testing
- Use @MockBean to mock dependencies inside integration tests.
- Prefer unit tests for logic-heavy components and services.
- Use @WebMvcTest when you want to test controllers without loading the full Spring context.
- Group tests by feature or module.
- Avoid testing framework-specific behavior unless absolutely necessary.
π Summary
In this post, we introduced the basics of Spring Boot application testing, including:
- Adding the right test dependencies.
- Writing unit tests with JUnit 5.
- Mocking components with Mockito.
- Writing integration tests with
@SpringBootTestandTestRestTemplate.
Testing is not just about coverageβit’s about confidence in your application. The better your tests, the faster you can iterate and deploy.
Stay tuned for our upcoming deep-dives into mocking, testcontainers, and performance testing in Spring Boot!