After a couple of years away from Java, I had a hard time transitioning back from Ruby and JavaScript, particularly in the test framework area. I was so used to RSpec and Jasmine… and now I had to go back to JUnit. Don’t get me wrong, JUnit is a great tool, but RSpec tests are way more expressive.

So, I started to play around with Java 8 and lambda expressions, and decided to see how far I could go writing a framework similar to RSpec for the Java land.

The result is J8Spec. Here’s an example:

package j8spec.examples;

import j8spec.junit.J8SpecRunner;
import org.junit.runner.RunWith;

import static j8spec.J8Spec.describe;
import static j8spec.J8Spec.it;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(J8SpecRunner.class)
public class CalculatorTest {
  {
    describe("operations", () -> {
      it("adds two integers", () -> {
        Calculator calculator = new Calculator();

        assertThat(calculator.add(1, 3), is(4));
      });
    });
  }
}

Thanks to JUnit’s fantastic design, J8Spec tests are execute as regular JUnit tests, leveraging all the existing tooling.

I’m very happy with the outcome, even considering the limitations Java imposes. ;)