2.3单元测试常用情景

使用反射模拟值

示例:模拟serviceManage类属性host

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

重置模拟值

reset(templateHelper);

针对异常情况的测试

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

模拟同一类中的方法

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

Last updated

Was this helpful?