5

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

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    Perhaps http://stackoverflow.com/questions/1068785/which-bdd-framework-for-java-do-you-use might be helpful. – Mark Jun 08 '10 at 20:14
  • 1
    Have you looked at the frameworks available? Just for Scala, ScalaTest has BDD support, and Specs was designed with BDD in mind. Then there's a multitude of testing frameworks for Java. Perhaps if you could explain your requirements a bit better, it would be easier to answer this question. – Daniel C. Sobral Jun 08 '10 at 22:02

6 Answers6

8

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)
    }
  }
}
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
6

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
  • 15,494
  • 38
  • 61
4

Now you can use Cucumber and define your steps in Java or pure Scala.

Here you have a short and easy tutorial on how to use it in Scala with SBT: http://func.io/post/36452127031/pure-scala-bdd-made-easy-with-sbt-and-cucumber.

agile_jordi
  • 113
  • 4
3

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

user142435
  • 357
  • 1
  • 4
1

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 C. Sobral
  • 295,120
  • 86
  • 501
  • 681
1

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
  • 17,277
  • 4
  • 47
  • 92