Class to test

1
2
3
4
5
6
7
8
9
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class MyClassToTest {
    @Test
    public void testDemo() {
        assertEquals(1, 1);
    }
}

Test runner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
    public static final String RED = "\033[31;1m";
    public static final String GREEN = "\033[32;1m";
    public static final String RESET = "\033[0m";

    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(MyClassToTest.class);
        System.out.print("Ran " + result.getRunCount() + ", ");
        System.out.print("ignored " + result.getIgnoreCount() + ", ");
        System.out.print("in " + result.getRunTime() + " ms.");
        int fails = result.getFailureCount();
        String color = (fails == 0 ? GREEN : RED);
        System.out.println(" " + color + result.getFailureCount() + " failed." + 
               RESET);
        if (result.wasSuccessful()) {
            System.out.println(GREEN + "✓ All tests passed." + RESET);
        } else {
            for (Failure failure : result.getFailures()) {
                System.out.println(RED + "𝘹 " + failure.toString() + "\033[0m");
            }
        }

    }
}

Makefile

1
2
3
4
5
6
7
test:
    @javac -cp .:/lib/junit-4.12.jar MyClassToTest.java TestRunner.java
    @java -cp .:./lib/junit-4.12.jar:./lib/hamcrest-core-1.3.jar \
        TestRunner

testwatch:
    @fswatch MyClassToTest.java | xargs -I % bash -c 'clear; make test'

Output

Success:

1
2
3
$ make test
Ran 1, ignored 0, in 9 ms. 0 failed.
✓ All tests passed.

Failure:

1
2
3
$ make test
Ran 1, ignored 0, in 88 ms. 1 failed.
𝘹 testDemo(MyClassToTest): expected:<1> but was:<0>