# 7.9.2. 测试 Spring 应用程序

依赖注入的主要优点之一是它应该使您的代码更容易进行单元测试。您可以使用`new`运算符来实例化对象，甚至无需涉及 Spring。您还可以使用*模拟对象*而不是真正的依赖项。

通常，您需要超越单元测试并开始集成测试（使用 Spring `ApplicationContext`）。能够执行集成测试而不需要部署您的应用程序或需要连接到其他基础设施是很有用的。

Spring 框架包括一个用于此类集成测试的专用测试模块。你可以直接声明一个`org.springframework:spring-test`依赖或者使用`spring-boot-starter-test`“Starter”来传递它。

如果您以前没有使用过该`spring-test`模块，则应首先阅读 Spring Framework 参考文档的[相关部分](https://docs.spring.io/spring-framework/docs/5.3.22/reference/html/testing.html#testing)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/7.-he-xin-te-xing/7.9.-dan-yuan-ce-shi/7.9.2.-ce-shi-spring-ying-yong-cheng-xu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
