我在JBehave中有一个“什么时候”,在某些情况下应该抛出异常。但是,我无法找到有关如何处理此问题的任何文档。这是我的情景:
给出一个有6个现有投注的游戏,游戏的最大赌注为6 当用户下注时
没有,因为我想在用户下注时抛出异常。
请记住,我并不总是希望何时抛出异常。例如。当现有投注小于最大投注时。在那种情况下,我想在“然后”做一些确保。
答案 0 :(得分:11)
我对BDD的理解是利益相关者 - 聚焦,允许非技术人员使用(或多或少)自然来编写系统应该做的事情语言,开发人员可以编写代码的方式:
通过编写测试用例来扩展TDD:
用自然语言表示 非程序员可以阅读。
[wikipedia]
也就是说,利益相关者永远不会在“然后”中写“抛出异常”。他们可能写道:
Given a game where 6 bets are allowed and 5 bets have been made,
When a user makes the 6th bet,
Then the "Bet" button should become disabled.
或
Given a game where 6 bets are allowed and 6 bets have been made,
When a user tries to make a bet,
Then the a message appears, saying:
"You can not bet. The maximum of 6 bets has already been placed."
答案 1 :(得分:2)
由于还没有答案,我会尝试一下。我这样做的方法是将异常存储为Steps`实现的内部状态的一部分:
public class MySteps extends Steps {
private Throwable throwable = null;
@When("something happens")
public void somethingHappens() {
try {
// Event part of the specification
} catch (MyException e) {
throwable = e;
}
}
@Then("an exception is thrown") {
public void verifyException() {
assertThat(throwable, allOf(notNullValue(), myExceptionMatcher()));
}
private Matcher<Throwable> myExceptionMatcher() {
// Build and return some Matcher instance
}
}
这对我很有用,但需要仔细的状态管理。
答案 2 :(得分:0)
我们使用了Nils Wloka建议的模式,非常有效的答案,捕获了#34;当&#34;使用试试 - &gt;捕获然后验证&#34;然后&#34;中的预期异常。步骤