tags:

views:

162

answers:

2

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

A: 

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.

springify
+1  A: 
JeffH