DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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

Konrad Malawski user avatar by
Konrad Malawski
·
May. 28, 13 · Interview
Like (0)
Save
Tweet
Share
6.68K Views

Join the DZone community and get the full member experience.

Join For Free

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

Concept (generic programming) Testing

Published at DZone with permission of Konrad Malawski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 5 Node.js REST API Frameworks
  • Iptables Basic Commands for Novice
  • OpenID Connect Flows
  • Visual Network Mapping Your K8s Clusters To Assess Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: