Writing Steps in Scala

Scala is the new kid on the block that compiles directly to the JVM.

JBehave supports writing Steps classes directly in Scala, using the ScalaStepsFactory and providing the list of Scala classes found in the classpath:

@RunWith(JUnitReportingRunner.class)
public class ScalaStories extends JUnitStories {
 
    @Override
    public Configuration configuration() {
        return new MostUsefulConfiguration()
                .useStoryReporterBuilder(new StoryReporterBuilder().withFormats(ANSI_CONSOLE));
    }
 
    @Override
    protected List<String> storyPaths() {
        return new StoryFinder()
                .findPaths(codeLocationFromClass(this.getClass()), "**/*.story", "");
    }
 
    @Override
    public InjectableStepsFactory stepsFactory() {
        return new ScalaStepsFactory(configuration(), new ScalaContext("ScalaSteps"));
    }
 
}
The Scala classes must be compiled directly into Java bytecode.

The Scala steps classes contain annotated methods, just as you would in Java:

class ScalaSteps {
     
  @org.jbehave.core.annotations.Given("a date of $date")
  def date(date: java.util.Date) {
     org.junit.Assert.assertNotNull(date)
  }
 
  @org.jbehave.core.annotations.When("$days days pass")
  def daysPass(days: Int) {
     org.junit.Assert.assertNotNull(days)
  }
 
  @org.jbehave.core.annotations.Then("the date is $date")
  def theDate(date: java.util.Date) {
     org.junit.Assert.assertNotNull(date)
  }
 
  override def toString(): String = "ScalaSteps";
 
}