> For the complete documentation index, see [llms.txt](https://ethan-lin.gitbook.io/refactoring/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ethan-lin.gitbook.io/refactoring/xiang-jin-mei-gui/4-task-2.md).

# 任务2：添加安全网

作为向祖传代码发起挑战的第一步，**永远是先搭建一张测试的安全网**，这张安全网需要有这样的特点：

* **必须很容易使用**。最好是按一个快捷键就能整个运行它，这样我们才可能频繁执行它
* **必须非常直观**。我们不想做人工的比对，最好是由它快速告诉我们测试是否失败，前后差别是什么
* **必须覆盖全面**。姑且不说 100%的覆盖率，如果没有 90%以上的覆盖率，怎么好意思说自己是一张“安全”网呢？

如果你想的是先添加单元测试，你可能会遇到挫折，一是因为难以找到一个合适的“单元”切入进行测试，二是难以保证全面的覆盖率。你需要开动脑筋，找到系统的入口点和输出点，仔细寻找有没有可以用来记录系统行为的用例。

此外，你还需要严格遵循这个规则：**在安全网搭建好之前不能修改产品代码**。我们可不想意外地修改代码的行为。

![](/files/-LrtW0nj2rUYC40bFn0J)

## 你的任务

Java：

```
git clone git@github.com:linesh-simplicity/gildedrose-java.git
git checkout task-2-safety-net
./gradlew clean build
```

JavaScript：

```
git clone git@github.com:linesh-simplicity/gildedrose.git
git checkout task-2-safety-net
npm i
npm test
```

1. 给代码编写自动化的测试安全网
2. 不允许对产品代码做任何修改
3. 测试覆盖率不低于 95%
4. 能够通过一个快捷键运行测试
5. 能够自动化报告测试结果

## 反思

* 过程中有没有遇到纠结的情况？


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://ethan-lin.gitbook.io/refactoring/xiang-jin-mei-gui/4-task-2.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.
