4

I love the Javascript BDD tool Jasmine, as it is highly flexible to define the stories. The stories in Jasmine can be structured in a way that the prerequisite stories are ran before the stories that depended on the previous ones. This makes the testing code highly re-usable, and readable.

Example of code reuse:

describe("parent story", function() {
  var a = 1;
  beforeEach(function(){
    a++;
  });

  it("should equal to 2", function() {
    expect(a).toBe(2);
  });

  describe("child story"), function(){
    beforeEach(function(){
      a++;
    });

    it("should equal to 3", function(){
      expect(a).toBe(3);
    });
  });
});

I have done some research on using this concept in the Java world, and found the most popular BDD in Java is JBehave. However, it doesn't seem to be that flexible as Jasmine is in terms of re-using the testing code in the previous stories for the child stories. I couldn't see how it can pass in the reused variables into the child stories from the parent ones as the Jasmine example does.

JBehave has the GivenStories concept to run before the others, however I couldn't find how the state made in the GivenStories can be passed to the ones that depended on them.

Can JBehave do this work as nicely as Jasmine does? If not, is there another BDD framework in Java that can do the same thing?

Mark Bertenshaw
  • 5,594
  • 2
  • 27
  • 40
Kata
  • 685
  • 1
  • 9
  • 22

1 Answers1

1

JBehave and Jasmine are not that similar. Jasmine is much more similar to rspec. JBehave and Cucumber (another BDD framework, even more popular, I think) are more similar to one another than to Jasmine or rspec.

Although JBehave doesn't have Jasmine's nested contexts, it can certainly pass state between steps. Look at http://jbehave.org/: state is passed around in instance variables of a Steps class. Cucumber works similarly, although the Ruby version at least (I'm not familiar with Java Cucumber) is more flexible, in that state can be passed from any step to any other step as an instance variable of the Cucumber World.

Be careful, though: in all of these frameworks, state sharing is intended to allow you to build tests out of multiple steps, not to allow tests to depend on one another. Tests can share code, but whether one test passes or fails should be completely independent of whether another test passes or fails. Once you assert something, you're not in setup code any more, you're testing, and you should not reuse your state for another test.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121