> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/43.-testing/43.2-testing-spring-applications.md).

# 43.2 测试Spring应用

依赖注入主要优势之一就是它能够让你的代码更容易进行单元测试。你只需简单的通过`new`操作符实例化对象，甚至不需要涉及Spring，也可以使用模拟对象替换真正的依赖。

你常常需要超出单元测试，开始集成测试（涉及到Spring的`ApplicationContext`）。在执行集成测试时，不需要部署应用或连接到其他基础设施是非常有用的。Spring框架为实现这样的集成测试提供了一个专用的测试模块。通过声明`org.springframework:spring-test`的依赖，或使用`spring-boot-starter-test`“Starter”就可以使用它了。

如果以前没有使用过`spring-test`模块，可以查看Spring框架参考文档中的[相关章节](https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/testing.html#testing)。
