tags:

views:

244

answers:

5

Is there a Java or Scala equivalent to Cucumber/SpecFlow? One possibility is using Cucumber with JRuby; any others?

+6  A: 

Take a look at ScalaTest with Feature Spec. Sample feature spec from the ScalaTest website:

import org.scalatest.FeatureSpec
import org.scalatest.GivenWhenThen
import scala.collection.mutable.Stack

class ExampleSpec extends FeatureSpec with GivenWhenThen {

  feature("The user can pop an element off the top of the stack") {

    info("As a programmer")
    info("I want to be able to pop items off the stack")
    info("So that I can get them in last-in-first-out order")

    scenario("pop is invoked on a non-empty stack") {

      given("a non-empty stack")
      val stack = new Stack[Int]
      stack.push(1)
      stack.push(2)
      val oldSize = stack.size

      when("when pop is invoked on the stack")
      val result = stack.pop()

      then("the most recently pushed element should be returned")
      assert(result === 2)

      and("the stack should have one less item than before")
      assert(stack.size === oldSize - 1)
    }

    scenario("pop is invoked on an empty stack") {

      given("an empty stack")
      val emptyStack = new Stack[String]

      when("when pop is invoked on the stack")
      then("NoSuchElementException should be thrown")
      intercept[NoSuchElementException] {
        emptyStack.pop()
      }

      and("the stack should still be empty")
      assert(emptyStack.isEmpty)
    }
  }
}
abhin4v
+1  A: 

Look at Fitness, if you want to separate test code from acceptance text. Otherwise, both Specs and ScalaTest support BDD-style (Specs was written to be BDD-style), and lots of other Java frameworks support it was well.

Daniel
+4  A: 

specs provides Literate Specifications with "forms" to develop Fit-like specifications. You can find a post explaining the rationale of it here, and some examples of what can be done with it.

Note however that the library is still in alpha mode as I plan to give it more attention once Scala 2.8.0 has settled.

Eric
+1  A: 

JBehave works just fine with Scala. For an example, in this article, http://jnb.ociweb.com/jnb/jnbJun2010.html there is a link to a zip file that contains a sample application using JBehave implemented completely in Scala.

Direct link to zip: http://www.ociweb.com/jnb/jnbJun2010-scala-bowling.zip

+1  A: 

JBehave was rewritten after Cucumber was released, so that we could also use plain text. Gherkin wasn't out when we wrote it, so it doesn't parse exactly the same - uses tokens instead of regexp - but it'll do the same job.

http://jbehave.org

Lunivore