View on GitHub

Lambda Behave

A modern testing and behavioural specification framework for Java 8

Download this project as a .zip file Download this project as a tar.gz file

Introduction

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 goal is to make testing a more pleasant experience than it currently is with junit.

The changelog explains what features have been added in each release.

Fluent Specifications

The Lambda Behave Specification design has several goals in mind:

public class StackSpec {{

    Stack<Integer> stack = new Stack<>();

    describe("a stack", it -> {

        it.isSetupWith(stack::clear);

        it.isConcludedWith(stack::clear);

        it.should("be empty when created", expect -> {
            expect.that(stack).isEmpty();
        });

There are many, many, expectations builtin to the framework - not just isEmpty().

Every specification suite starts its declaration using the Suite.describe method. From that point onwards your IDE should be able to auto-complete the domain specific language for declaring specifications, but just in case you want more information, here's the details.

Data Driven Specifications

The ability to parametrise specifications by different data inputs. Data driven tests in TestNG or the @Parameterized junit annotation perform a similar task. @Parameterized only parameterises at the level of a class, whereas Lambda Behave parameterises at the level of a specification.

describe("a pair of numbers", it -> {
    it.uses(2, 4)
      .and(4, 8)
      .toShow("%d / %d is two", (expect, x, y) -> {
          expect.that(y / x).is(2);
      });
});

The API in Lambda Behave is both fluent and also type safe and doesn't rely on reflection magic. The uses method is overloaded to allow a different number of columns of data to be used. It also supports taking streams or lists of data as its inputs, rather than explicitly chaining individual values.

Not only is the specification parameterised by the data, but the description is also parameterised, its name being interpreted as a format String. The aforementioned test would output the following:

a pair of numbers
  2 / 4 is two
  8 / 4 is two

Generated Specifications

Lambda Behave can automatically generate testcases for your to test your code with, similar to quick check or scala check. 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;

Downloading Lambda Behave

If you're using a maven project then you can download Lambda Behave using the following pom entry.

<dependency>
    <groupId>com.insightfullogic</groupId>
    <artifactId>lambda-behave</artifactId>
    <version>0.3</version>
    <scope>test</scope>
</dependency>

If you're using a gradle project then you can use:

testCompile group: 'com.insightfullogic', name: 'lambda-behave', version: '0.3'

There's also an example project and there's published Javadoc.

Junit Integration

Lambda Behave also offers a junit runner. This lets you easily integrate into existing your existing test suite, or the tests via an Eclipse, Intellij, Netbeans, Maven, Gradle or Ant. You just add an annotation to enable this, and it can be run through your normal tooling.

@RunWith(JunitSuiteRunner.class)
public class StackSpec {{

Lambdas - what the hell are they?

Conveniently I've written a book on Lambda expressions in Java 8 and the cleaner code they enable!

More Details and How to contribute

The wiki has more information.