> For the complete documentation index, see [llms.txt](https://moluo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moluo.gitbook.io/notes/bian-cheng-xue-xi/ce-shi/2.3-dan-yuan-ce-shi-chang-yong-qing-jing.md).

# 2.3单元测试常用情景

使用反射模拟值

示例：模拟serviceManage类属性host

```java
ReflectionTestUtils.setField(serviceManage, "host", "http://10.110.25.75:57628");
```

重置模拟值

```java
reset(templateHelper);
```

针对异常情况的测试

```bash
try {
    ServiceResponse result3 = serviceManage.lockService("gaoss-8a81011a6e", eventMq3);
    fail("调用服务工厂-锁定虚机失败,此处应抛出异常");
} catch (SecResourceUnavailableException e) {
    Assert.assertEquals("调用服务工厂-锁定虚机失败,失败原因：'无法连接服务工厂，请排查服务工厂是否正常运行及网络是否畅通'", e.getMessage());
}
```

模拟同一类中的方法

```bash
ReflectionTestUtils.setField(serviceManage, "host", "http://10.110.25.75:57628");
```
