Search code examples
testingbddspockautomated-testsjbehave

Behavior Driven Development for java - what framework to use?


For the ongoing projects and for improving our development process we considered adopting TDD as development philosophy. While researching for best practices and how to "sell" the new approach to my colleagues/ developers I came across BDD and found it even more appropriate to what we would need and somehow to be next iteration of TDD. The problem is that up to now I tried only the tool developed by Dan North, JBehave and I cannot say that I am amazed.

The setup seems to me cumbersome and I couldn't find very appropriate documentation for it. On the other hand I tried also spock the groovy tool and up to now I kind of like it.

Q: are there any proper tools to be used for BDD?
Q: you would use instead spock and deal with the overhead of introducing another language?


Solution

  • Behavior Driven Development is just a technique that can be used without any tools. You can just write tests in BDD style - e.g. start test methods with should and introduce some separate feature with this method. When and then sections can be replaced with just comments, e.g.

    @Test
    public void should_do_something() {
        // given
        Something something = getSomething();
    
        // when
        something.doSomething();
        // then 
        assertSomething();
    
        // when
        something.doSomethingElse();
        // then 
        assertSomethingElse();
    }
    

    My opinion on the mentioned frameworks:

    • The problem with JBehave is that tests look like a complex spaceship. On the other hand it has pretty output for your specifications.

    • spock is really cool. Compact syntax, pretty output, a lot of features, written with the powerful groovy language, which means the possibility of usage in conjunction with geb. BUT it is groovy and it can be very important for someone.

    • scalatest (written with scala) and easyb (written with groovy) both have the same disadvantage as spock. The "... should ..." and "Given...Then" notation. Specifications are in .story files, and the step implementations are in Java classes. This approach work very well as a collaboration and communication tool to define the specs, but would usually be too much overhead for low-level coding.

    I also think that the most successful BDD frameworks for Java are those that are not written in Java, since the Java language has no such flexibility for DSL (Domain Specific Language) creation that Groovy or Scala has.