我刚刚开始使用JBehave进行BDD,我遇到了一个大问题。 每次断言失败时,都会抛出我的Netbeans环境中的异常,并且不执行其他测试。 但我希望它将此测试标记为失败并执行其他测试。 我经常搜索,但我的问题无法解决。
故事:
Narrative:
In order to manage our stock efficiently
As a logistic employee
I would like to handle returns and changes in a proper way
Scenario: Return of a phone
Given there is an empty stock
And there are 2 A phone(s) in stock
When a customer returns the not damaged A phone
Then there should be 2 A phone(s) in stock
Scenario: Exchange of a phone
Given there is an empty stock
And there are 5 A phone(s) in stock
And there are 5 B phone(s) in stock
When a customer changes his A phone for a B phone
Then there should be 6 A phone(s) in stock
And there should be 4 B phone(s) in stock

ScenarioLoader
public class LogisticScenarios extends JUnitStory {
@Override
public Configuration configuration() {
URL storyURL = null;
try {
// This requires you to start Maven from the project directory
storyURL = new URL("file://" + System.getProperty("user.dir")
+ "/src/main/resources/stories/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
return new MostUsefulConfiguration()
.usePendingStepStrategy(new FailingUponPendingStep())
.useStoryLoader(
new LoadFromRelativeFile(storyURL)).useStoryReporterBuilder(
new StoryReporterBuilder().withFormats(Format.HTML));
}
@Override
public List<CandidateSteps> candidateSteps() {
return new InstanceStepsFactory(configuration(), new LogisticSteps())
.createCandidateSteps();
}
@Override
@Test
public void run() {
try {
super.run();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
步骤
public class LogisticSteps extends Embedder {
List<Phone> stock;
@Given("there is an empty stock")
public void initializeStock() {
stock = new LinkedList();
}
@Given("there are $quantity $type phone(s) in stock")
public void fillStock(String quantity, String type) {
stock = stock == null ? new LinkedList() : stock;
PhoneType pType = type.equals("A") ? PhoneType.A : PhoneType.B;
addPhonesToStock(pType, Integer.parseInt(quantity));
}
@When("a customer returns the not damaged $type phone")
public void customerReturnsPhone(String type) {
PhoneType ptype = type.equals("A") ? PhoneType.A : PhoneType.B;
addPhonesToStock(ptype, 1);
}
@When("a customer changes his $type1 phone for a $type2 phone")
public void customerExchangesPhone(String type1, String type2) {
PhoneType pType1 = type1.equals("A") ? PhoneType.A : PhoneType.B;
PhoneType pType2 = type2.equals("A") ? PhoneType.A : PhoneType.B;
stock.add(new Phone(pType1));
removePhoneFromStock(pType2);
}
@Then("there should be $quantity $type phone(s) in stock")
public void thePositionReturnedShouldBe(String quantity, String type) {
PhoneType pType = type.equals("A") ? PhoneType.A : PhoneType.B;
Assert.assertEquals(Integer.parseInt(quantity), countPhonesFromType(pType));
}
private void removePhoneFromStock(PhoneType type) {
for (int i = 0; i < stock.size() - 1; i++) {
if (stock.get(i).getType() == type) {
stock.remove(i);
break;
}
}
}
private int countPhonesFromType(PhoneType type) {
int count = 0;
for (Phone p : stock) {
if (p.getType() == type) {
count++;
}
}
return count;
}
private void addPhonesToStock(PhoneType type, int quantity) {
for (int i = 0; i < quantity; i++) {
stock.add(new Phone(type));
}
}
}
感谢您的帮助
答案 0 :(得分:0)
JBehave的一般行为。在某个情况下,如果某个步骤失败(异常被抛出),它就不会执行该方案的后续步骤(将它们标记为未执行),但之后的第二个方案将是执行。现在,如果您的下一个场景取决于第一个场景的结果,那么它也将失败,正确。
现在,如果你看到你有:
try {
super.run();
} catch (Throwable e) {
e.printStackTrace();
}
要完成此设置,您可以尝试捕获代码中的异常,并根据您的选择采取必要的步骤。
你正在做断言的地方使用try catch这样:
try{
Assert.assertEquals();
} catch (Exception e){
<your steps> <record the result or just move forward>
}