Proof of Concept – LambdaSpec, how testing will look with Java8
Join the DZone community and get the full member experience.
Join For FreeI 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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments