【发布时间】:2018-05-15 20:21:54
【问题描述】:
我开始使用 Gauge Framework 进行一些测试。 当测试的值无效时,我有一些验证方法会返回一个名为 FieldFunctionException 的自定义异常。
为了在 Junit 中进行测试,我使用了类似的东西:
@Test(expected = FieldFunctionException.class)
public void testAllowedValuesValidation_fail_1() throws FieldFunctionException {
...
}
使用 Gauge 框架:
@Step("Scenario failing ")
public void testAllowedValuesValidation_fail_1() throws FieldFunctionException {
...
}
但我没有找到如何通知 Gauge 这个测试抛出 FieldFunctionException 是正常行为。
解决方法:
就我而言,我想测试异常。换句话说,异常抛出是一个成功的测试场景。我为测试异常所做的解决方法是这样的:
@Step("Scenario failing")
public void testAllowedValuesValidation_fail_1() throws FieldFunctionException {
AllowedValuesValidation f = new AllowedValuesValidation().allowedValues(new String[] { "Yes", "No", "Maybe" });
try {
String returned = f.execute("Maybes", null);
}catch (Exception e) {
assertTrue(e instanceof FieldFunctionException);
}
}
【问题讨论】: