Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior Driven Development for java - what framework to use? [closed]

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?

like image 809
Olimpiu POP Avatar asked Apr 16 '13 11:04

Olimpiu POP


People also ask

What type of framework is BDD?

Behavior Driven Development (BDD) Framework enables software testers to complete test scripting in plain English. BDD mainly focuses on the behavior of the product and user acceptance criteria. Cucumber is one of the best tools used to develop in the BDD Framework.

Which SDLC model is best suited for BDD framework?

BDD is an advanced and faster transformation of traditional SDLC model i.e. Waterfall model. Waterfall model is a linear sequential arrangement of different phases of software development activities. It is called as linear sequential phases as each phase is dependent on previous phase deliverables or output.

Why is BDD preferred over other frameworks?

BDD provides a path that acts as a bridge to overcome the gap between the technical and the non-technical teams because the test cases are commonly written in simple text, i.e. English. The main advantage of BDD is the low jargon and clearer approach which is easier to understand.


4 Answers

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.

like image 162
sody Avatar answered Oct 22 '22 08:10

sody


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.

like image 36
Jan Schaefer Avatar answered Oct 22 '22 08:10

Jan Schaefer


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.

like image 14
dkl Avatar answered Oct 22 '22 07:10

dkl


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.

like image 4
Ashley Frieze Avatar answered Oct 22 '22 07:10

Ashley Frieze