我有那种有效的测试:
Feature: TestAddition
Scenario Outline: "Addition"
Given A is <A> and B is <B>
Then A + B is <result>
Examples:
| A | B | result |
| 3 | 4 | 7 |
| 2 | 5 | 7 |
| 1 | 4 | 5 |
那就是胶水代码:
package featuresAdditions;
import org.junit.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import math.AdditionEngine;
public class step {
private AdditionEngine testAdditionEngine;
private double resultAddition;
@Given("^A is (\\d+) and B is (\\d+)$")
public void addition(int arg1, int arg2) throws Throwable {
testAdditionEngine = new AdditionEngine();
resultAddition = testAdditionEngine.calculateAdditionAmount(arg1, arg2);
}
@Then("^A + B is (.)$")
public void addition(double arg1) throws Throwable {
Assert.assertEquals(arg1, resultAddition, 0.01);
}
}
但是我想知道如何创建一个无效的表示例[其中??意味着我不知道下表中的内容]
Examples:
| A | B | result |
| "é3-3" | 5 | ?? |
| "é3-3" | "aB" | ?? |
这应该是java.lang.NumberFormatException
在纯粹的jUnit中,我会做类似下面代码的工作,就像魅力[@Test(expected = NumberFormatException.class)
]一样。但是,我必须使用Cucumber ...有人可以告诉我如何用Cucubmer进行这样的测试吗?
public class test {
AdditionEngine testAdditionEngine = new AdditionEngine();
@Test(expected = NumberFormatException.class)
public void test() {
testAdditionEngine.calculateAdditionAmount("é3-3", 5);
}
}
答案 0 :(得分:1)
Scenario Outline: "Invalid Addition"
Given A is <A> and B is <B>
Then A + B is <result>
Examples:
| A | B | result |
| "é3-3" | 5 | java.lang.NumberFormatException |
| "é3-3" | "aB" | java.lang.NumberFormatException |
更改stepdefinition,将String
作为参数而不是Integer
。
private Exception excep;
@Given("^A is (.*?) and B is (.*?)$")
public void addValid(String arg1, String arg2) {
try {
testAdditionEngine = new AdditionEngine();
testAdditionEngine.calculateAdditionAmount(arg1, arg2);
} catch (NumberFormatException e) {
excep = e;
}
};
@Then("^A \\+ B is (.*?)$")
public void validResult(String arg1){
assertEquals(arg1, excep.getClass().getName());
};
如果您使用的是Cucumber 2及以上版本,您将收到含糊不清的步骤消息。这将是因为有效的scenariooutline将匹配整数和字符串stepdefinitions。更改其中一个方案声明。