Lambda Behave 0.2 Released


Lambda Behave 0.2 adds a host of improvements including automatic testcase generation.

Sun 06 July 2014

tl;dr Lambda Behave 0.2 adds a host of improvements including automatic testcase generation.

What is Lambda Behave?

If you're a Java developer and you've seen the fluent, modern specification frameworks available in other programming languages such as spock or jasmine then Lambda Behave is for you. Its overall goal is to make testing a more pleasant experience than it currently is with junit, specifically to:

  • To read like plain English.
  • To encourage describing tests using long and descriptive sentences, rather than a few words.
  • An API that is fluent and discoverable nearly entirely through IDE auto-completion.

Here's an example specification to give an example of what Lambda Behave looks like in action:

        public class StackSpec {{
        
            Stack stack = new Stack<>();
        
            describe("a stack", it -> {
        
                it.shouldSetup(stack::clear);
        
                it.shouldTearDown(stack::clear);
        
                it.should("be empty when created", expect -> {
                    expect.that(stack).isEmpty();
                });
        

What's new in the 0.2 release?

The biggest new feature is automatic testcase generation, similar to the way quick check or scala check works. The Fluent API for this is similar to data driven specifications allows for control over the way that the values are generated and how many need to be generated. Here is an example of how to show that reversing a String twice returns the same String using randomly generated test case values.

        it.requires(10)
          .example(asciiStrings())
          .toShow("reversing a String twice returns the original String", (expect, str) -> {
              String same = new StringBuilder(str).reverse().reverse().toString();
              expect.that(same).isEqualTo(str);
          });
        

All generated specifications follow this common pattern where;

  • The require clause expresses how many values to generate,
  • The example clause states what type of objects to generate and how to generate them, This is overloaded to allow multiple columns of testcase values to be generated.
  • The toShow clause behaves like a toShow clause for a data drive spec. It is type safe against the the different columns. So in the above example the paramter str will have had its type correctly inferred as String.

In addition 0.2 also bans multiple specifications with the same name in order to make integration with other tools easier and not confuse end users. It fixes a couple of bugs, most importantly that stack traces being were being incorrectly reported in errors. The fluent expectations API has also been significantly extended with more overloads, including support for arrays and exceptions.

Moving Forward

I'm currently looking forward to 0.3 and trying to figure out which features to prioritize and implement. If you have any particular issue that you feel is a priority to you then just comment on it. Contributions via pull request and feedback are also most welcome.

A Quick Plug

If you've been interested in this Lambda Behave blog post then you're probably also interested in Java 8! I've written a book and am teaching a training course to help get Java developers up to speed with Java 8 quickly.