Behavior Driven Development for java - what framework to use?

28,919

Solution 1

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.

Solution 2

As the author of JGiven I have to disagree with sody that Java has not enough flexibility for DSL creation. In JGiven, BDD tests looks as follows:

@Test
public void users_can_login {
    given()
       .a_registered_user()
       .and().the_login_page_is_shown();

    when()
       .the_user_enters_correct_credentials()
       .and().the_login_button_is_pressed();

    then()
       .the_welcome_page_is_shown();
}

JGiven is used together with JUnit or TestNg and you write your tests in plain Java.

Solution 3

Unless your product owner/qa/customer need to be able to read the tests, use Spock. It is very simple tool, but improves readability of tests. Thanks to it's powerful features you don't need Mockito, Hamcrest nor AssertJ. And it has superb parametrized tests. In fact, it is "just" a better JUnit - a general tool for automated execution of simple tasks, be it unit tests, integration tests or acceptance tests.

Fearing Groovy? Why? It is very similar to java. The more you learn it, the more expressive and shorter your code is. Your tests will be shorter and more readable. Groovy is gateway drug to the better side of JVM.

Don't like dynamic languages? Well, it is tests, and tests are run by CI server after every commit, right? If your code breaks, you will know it after few minutes. Don't have CI server or not running tests regularly? Then don't bother with choosing a test framework and go fix your process. Broken tests are useless and if you don't run the tests regularly, they will break soon.

Go with JBehave/Cucumber if you need it; Otherwise, use Spock.

Solution 4

Another alternative would be Spectrum - see https://github.com/greghaskins/spectrum

Spectrum supports the RSpec/Mocha syntax and in its next release will also support Gherkin syntax, along with JUnit rule integration (so it interoperates with Mockito, Spring etc via the @Rule and @ClassRule members).

Full disclosure - I'm a contributor to this OS project

Example:

@RunWith(Spectrum.class)
public class TestSomething {{
    Supplier<Something> freshTestObject = let(Something::new);

    describe("The component", () -> {
        it("is tested by specs", () -> {
            // the "let` above gives us a new instance of the object
            // in each spec
            freshTestObject.get().doSomething();

            // using your favourite assertion framework
            assertThat(something.get().getSomething()).isEqualTo(42);
        });
    });
}}

Spectrum outputs a hierarchical test result in your JUnit console. The strength of it is in mixing the Java implementation of the spec execution in with the spec definition - this can be more direct than frameworks that rely on feature files and glue code to parse them, especially if there's a need to pass results from one step of the test to another.

Spectrum aims to be polyglot, so should seem familiar to users of several existing frameworks.

Solution 5

Give Ginkgo4j a go. It uses Java 8's lamda's to mirror the approach used by Ruby's RSpec and Go's Ginkgo.

This library allows you to build expressive, content-rich tests.

```

package com.github.paulcwarren.ginkgo4j.examples;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.runner.RunWith;

import com.github.paulcwarren.ginkgo4j.Ginkgo4jRunner;

@RunWith(Ginkgo4jRunner.class)
public class BookTests {
    private Book longBook;
    private Book shortBook;
    {
        Describe("Book", () -> {
            BeforeEach(() -> {
                longBook = new Book("Les Miserables", "Victor Hugo", 1488);
                shortBook = new Book("Fox In Socks", "Dr. Seuss", 24);
       });

       Context("Categorizing book length", () -> {
           Context("With more than 300 pages", () -> {
               It("should be a novel", () -> {
                   assertThat(longBook.categoryByLength(), is("NOVEL"));
               });
           });

           Context("With fewer than 300 pages", () -> {
               It("should be a short story", () -> {
                   assertThat(shortBook.categoryByLength(), is("NOVELLA"));
              });
           });
       });
       });
     }
}

```

Also supports Spring.

(Full disclosure. I am the author of this library).

Share:
28,919
Olimpiu POP
Author by

Olimpiu POP

Updated on October 02, 2020

Comments

  • Olimpiu POP
    Olimpiu POP over 3 years

    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?

  • Alex Blakemore
    Alex Blakemore almost 11 years
    One persons opinion. I've had fantastic experiences with spock. No problem testing Java code, and groovy is incredibly easy for a Java programmer to pickup. You can start out writing regular Java and keep working, then gradually adopt the more terse style as you learn Groovy if desired. I was skeptical of Groovy, but spock is such a joy to use that it more than repays the minimal effort to learn a little bit of Groovy.
  • G. Demecki
    G. Demecki over 9 years
    I have to disagree with You my friend. What you've presented has nothing to do with DSL. It's some kind of fluent API for Java (based on the static methods), but IMHO not much readable nor usable.
  • Jan Schaefer
    Jan Schaefer over 9 years
    Well, then please explain me the difference to a Groovy DSL. Btw, there is no static method here, these are all instance methods. In addition, the example I have shown is much more complex than the above example. However, whether it is useable or not should everybody decide for him or herself. JGiven gives you a tool that is much easier to use than Cucumber or JBehave and still provides scenario reports that are readable by business owners.
  • G. Demecki
    G. Demecki over 9 years
    It's hard for me to call such API a DSL due to amount of parentheses and dots. In Java you cannot simply skip them. It's just my personal feeling.
  • Jan Schaefer
    Jan Schaefer over 9 years
    Maybe I write too much Java so that don't even see the parentheses anymore :-). However, as JGiven is a Java framework it can also be used in Groovy and Scala. In Groovy you can get at least get rid of the semicolons, in Scala you can also omit the parentheses.
  • Jeremiah Adams
    Jeremiah Adams almost 6 years
    Looks useful to me. I'm not interested in a DSL but very interested in more readable unit tests using BDD concepts. I'll never sell the entire business on DSL and pure BDD approach but I can at least do something about the lack of clarity in my unit tests.
  • Jeremiah Adams
    Jeremiah Adams almost 6 years
    Fearing Groovy? Why? - an ever growing list of polyglot solutions that make it more difficult to find talent.