Date: February 18, 2025
Testing and deployment are the final critical steps to ensure your Spring Boot application is reliable, maintainable, and production-ready. In this tutorial, we’ll cover:
- Writing unit and integration tests with JUnit 5 and Mockito
- Testing Spring MVC controllers and service layers
- Using Spring Boot Test utilities for application context and slices
- Best practices for automated testing
- Preparing your app for deployment
- Packaging as a Docker container
- Basic deployment strategies
1. Writing Unit Tests with JUnit 5 and Mockito
Unit tests validate small, isolated components like services or utilities.
Example: Testing a UserService
@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
void whenFindUserById_thenReturnUser() {
User user = new User(1L, "test@example.com", "Test User");
Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(user));
User found = userService.findById(1L);
assertNotNull(found);
assertEquals("test@example.com", found.getEmail());
}
}
2. Integration Testing Controllers
Integration tests load the Spring context and test controllers, repositories, and services working together.
Example: Testing UserController with MockMvc
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
void whenGetUser_thenReturnsJson() throws Exception {
mockMvc.perform(get("/api/users/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email").value("test@example.com"));
}
}
Use @MockBean
to mock dependencies if needed.
3. Using @SpringBootTest and @WebMvcTest
@SpringBootTest
: loads full application context, good for end-to-end tests@WebMvcTest
: loads only web layer, good for testing controllers in isolation
4. Best Practices for Testing
- Write tests alongside development
- Use assertions liberally to check behavior
- Mock external dependencies (DB, external APIs)
- Automate tests in CI/CD pipeline (GitHub Actions, Azure Pipelines)
- Keep tests fast and independent
5. Packaging the Application for Deployment
Spring Boot apps are packaged as fat JARs:
./mvnw clean package
This generates an executable JAR (e.g., demoapp-0.0.1-SNAPSHOT.jar
).
Run it with:
java -jar demoapp-0.0.1-SNAPSHOT.jar
6. Containerizing with Docker
Create a simple Dockerfile
:
FROM eclipse-temurin:17-jre-alpine
ARG JAR_FILE=target/demoapp-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
Build and run your container:
docker build -t demoapp .
docker run -p 8080:8080 demoapp
7. Deployment Options
- Deploy to cloud providers like AWS Elastic Beanstalk, Azure App Service, Google Cloud Run
- Use container orchestration with Kubernetes for scalable deployments
- Automate deployments with CI/CD pipelines
Summary
- Developed unit and integration tests with JUnit 5, Mockito, and Spring Boot Test
- Explored packaging and running Spring Boot applications
- Created Docker containers for easy deployment
- Highlighted modern deployment strategies
Congratulations!
You’ve built, secured, tested, and deployed a full-featured Spring Boot 3 app using modern Java 17 features. Stay tuned for more advanced topics and deep dives!