Jbehave and expecting exceptions

試著忘記壹切 提交于 2019-12-07 07:38:47

问题


I have a "when" in JBehave which under certain circumstances should throw an exception. I cant find any documentation, however, on how to handle this. Here is my scenario:

given a game with 6 existing bets and the game's max bets are 6 when a user places a bet

there's no then, since I want an exception thrown when the user places the bet.

Keep in mind, I don't always want the when to throw an exception. E.g. when the existing bets are less than max bets. In that case, I want to do some ensuring in the "then".


回答1:


Implementation Details and "Computer-ese" Don't Belong in Scenarios


My understanding of BDD is that it is stakeholder-focused, allowing non-technical people to write what a system is supposed to do, using (more-or-less) natural language, in a way that developers can code to:

It extends TDD by writing test cases:

in a natural language that non-programmers can read.
[wikipedia]

That said, a stakeholder would never write "throw an exception" in a "Then". They'd probably write:

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.

or

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."



回答2:


As there are no answers yet, I will give it a try. The way I do this is by storing the exception as part of the Steps` implementation's internal state:

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
  }

}

This works well for me, but requires careful state management.




回答3:


We have used the pattern suggested by Nils Wloka, very valid answer, capture the exception in "When" using try --> catch and then validate against expected exception in "Then" step.



来源:https://stackoverflow.com/questions/1478886/jbehave-and-expecting-exceptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!