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?
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With