Rolling your own testing framework with Java Assertions

Chanmann Lim
lchanmann
Published in
3 min readAug 24, 2016

A few years back I gave a talk and demo about doing test-driven development at DevCamp organized by a group of like-minded and passionate software developers, who wish to bridge the gap between college education and industrial expectation of university fresh graduates and to improve software development atmosphere for knowledge sharing.

Doing test-driven development (TDD) can be challenging to get started since it requires programmers to stop the urge to code the solution but think of the desire behaviors to be implemented. This is a non-trivial mental switch that could only make feasible with the support from the programming languages used in the work and their testing ecosystems. Yet we are so blessed to have plenty of great testing tools and frameworks tightly integrated with most modern programming languages to date.

Java, in particular, has embraced testing built-in to the language itself and today we are going to dive into using Java assertions to build our own minimal testing framework in Java.

Java assertions is disabled by default and must be enabled to use assertion statement.

We begin with our plain old Java application skeleton and walk through TDD-ing implementing classical singly LinkedList then extracting and generalizing the test code to be our mini testing framework.

Create our first failing test to check the size of a newly created LinkedList:

Program.java:3: error: cannot find symbol
LinkedList list = new LinkedList();
^
symbol: class LinkedList
location: class Program

The program is not compiled which is intended since we are using the LinkedList class before it even exists. Then the fix would be creating a LinkedList class with a size() method that return zero in it.

class LinkedList {
public int size() {
return 0;
}
}

Next, continue to assert the head and tail of the LinkedList to be null then fix the failing assertions.

Now it’s time to refactor. The LinkedList head, tail and size should be turned into variables.

Continue to add the failing test:

And fix the test then we can proceed to extract code for constructing our testing framework:

Now let the fun part begins: we are going to refactor and generalize the test code to form our reusable mini testing facility.

First we refactor out LinkedList class and the tests inside Program class to LinkedListTest then extract each test case to a public method, whose name starts with test_ so that we can apply Java Reflection to lookup and run all test cases inside LinkedListTest.

And our core JMinitest class contains the method that run all the test cases in LinkedListTest class.

And the test runner class which is our Program class can simply invoke the runTests() method on LinkedListTest to run all its test cases.

Hooray! Here we get the first version of our testing framework JMinitest.

Moving on, we could encapsulate Java assertions to be more user-friendly for other users of our framework by wrapping it with our desire interface.

Here, we create verify() method in JMinitest class that return Verifiable type which contains all the assertion functions we might need.

At this point onwards, we just need to extend Verifiable class to support the assertion functionalities to enrich our JMinitest framework. We could also incorporate Java lambda expression to test for exception handling cases.

By no mean, this piece of work is considered completed and indeed it is far from perfect. I hope you guys as Java forks could give me feedbacks, or even better by lending a hand and make this project fun. The source code of this work is hosted at https://github.com/lchanmann/JMinitest.

--

--

Chanmann Lim
lchanmann

M.S. in Computer Science, University of Missouri-Columbia.