mechero/spring-boot-testing-strategies
关于
示例项目,展示使用 Spring Boot 时可采用的不同测试策略。
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Spring Boot Testing Strategies
Introduction
This sample application made with Spring Boot is intended to show the different approach for testing, from Unit Tests with MockMVC in Standalone mode to full @SpringBootTest as Integration tests between the modules.
The complete guide is available on The Practical Developer Blog.
The application
The logic behind the application is simple: it's a repository of superheroes that you can access through a REST API. It allows to read the available ones (which are hardcoded when the application starts up) and also add new members to the crew.
The architecture is simple: just the Controller layer (REST) and a SuperHeroRepository. To illustrate the differences when creating tests, there are two extra classes that work at a web layer level:
SuperHeroExceptionHandler. It's aControllerAdvicethat will transform aNonExistingHeroExceptioninto a404 NOT_FOUNDHTTP error code.SuperHeroFilter. This web filter adds a new header to the HTTP response.
Testing strategies
In the test sources you can find four different approaches to test the Controller. SuperHeroControllerMockMvcStandaloneTest. Uses a MockitoJUnitRunner and it's the most lightweight approach.

Then you can find two approaches using a Spring context, both use MockMVC and one of them already introduces the @SpringBootTest annotation.

Finally, SuperHeroControllerSpringBootTest shows how to write a @SpringBootTest based test mocking other layers but utilizing the web server with a RestTemplate.

To check conclusion and more information please visit the blog.
Changelog
2025/05 Update: Spring Boot 3.4.5 and JDK 24
- The trailing slash in some tests makes them fail. See this blog post
- Trailing slashes are now removed since they were irrelevant for this blog post
- Moved from
javax.packages tojakarta.where needed - Replaced
MockBean's deprecated annotation by its new versionMockitoBean
探索相关项目
加载中…