Performance Zone is brought to you in partnership with:

Konrad is passionate about the JVM, and the whole ecosystem that surrounds it. Lately he fell in love with Scala, but that doesn't mean he's not into Java or dynamic languages. Other than that, he?s a fan of automating every possible task and ridiculously long keyboard shortcuts. "After hours" he's still bound to programming - as a lead of the PolishJUG, the lead of the Google Developers Group Kraków and helping hand of Software Craftsmanship Kraków he's coding for fun and glory, speaking at conferences, or organising meetups ranging from small hackathons to big conferences like the annual GeeCON. In those rare times when he's not doing something code-related, he's collecting game consoles or playing tennis / squash. He bloggs and tweets. Konrad is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Proof of Concept – LambdaSpec, how testing will look with Java8

05.28.2013
| 5108 views |
  • submit to reddit

I recently had an hour or two to spare, so I decided to play around with Java 8 and its Lambdas. Coming from Scala and knowing the Spec of lambdas (I’ve been checking it out on each update) I went straight on to implementing anything useful with them.

I decided that Java will eventually get a testing framework using lambdas, in a more BDD way. My favourite example of such framework is ScalaTest (by Bill Venners) which I wholeheartedly love nowadays. So my goal was to implement something similar, within what’s possible with Java currently.

import pl.project13.test.lambda.exampleimpl.Adder;
import pl.project13.test.lambda.spec.set.ClassSpecSet;

import static org.fest.assertions.Assertions.assertThat;
import static pl.project13.test.lambda.SpecDSL.describe;

public class ClassSpecTest {

  // this `SpecSet` provides a new instance of the tested class for each `should`
  ClassSpecSet<Adder> it = describe(Adder.class);

  {
    it.should("add two numbers", adder -> {
      // given
      int a = 1;
      int b = 2;

      int expected = 3;

      // when
      int got = adder.add(a, b);

      // then
      assertThat(got).isEqualTo(expected);
    });

    it.should("add a negative number", adder -> {
      // given
      int a = -1;
      int b = 2;

      int expected = 333; // wrong `expected`, to demo failure logging

      // when
      int got = adder.add(a, b);

      // then
      assertThat(got).isEqualTo(expected);
    });

    it.should("add imaginary numbers", MarkAs.pending);
  }

  // currently LambdaSpec has no stand alone runner, so fire it up using JUnit
  @Test
  public void shouldPassAllTests() throws Exception {
    it.shouldPassAllTests();
  }
}

And here’s how a passing / failing and pending test would look like:

So pretty much like you’d expect it from a good testing framework nowadays. Truth be told – reading junit in the terminal is a horrible pain so you have to use your IDE for browsing test failures. (I honestly like my terminal output to be meaningful enough for testing – that way I always have all tests running on one screen, and am writing code on the main screen).

You may ask why this verbosity is a nice thing. That is: “Why not use annotations, like we always have?”. One of the answers is the bellow snippet, which displays how generating test data, and meaningful test names, and the tests themselfes are very easy to track and understand (this is not yet implemented):

// or even more plain:
testCases.foreach((case) -> {
  // given
  Object given = case.given;
  Object expected = case.expected;
  
  it.should("return [%s] when applied to [%s]", given, expected, system -> {
    // when
    Object got = system.doThing(given);
  
    // then
    assertThat(got).isEqualTo(expected);
  });
});

Or another style (yet to be improved):

it.should("return an expected value").forAll(testCases, (system, given, expected) -> {
  // when
  Object got = system.doThing(given);
  
  // then
  assertThat(got).isEqualTo(expected);
});

LambdaSpec is for now just a Proof of Concept, but eventually I hope to get it to the point that once J8 is released, we’ll be able to use it instead of JUnit. Some of the nice features I want to bring in would be smart test dependency analysis (so only the tests that may have failed will be run) and a separate runner… Think ~test if you know ScalaTest… :-)

Anyway, if you’re interested in Java 8 and what’s coming up, i encourage you to star the project (it’ll give me the necessary power to push this thing forward ;-)).

Check out the sources at https://github.com/ktoso/lambda-spec

Published at DZone with permission of Konrad Malawski, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)