Advanced MiniJava Testing Utilities

Custom Assertions

Sometimes, the provided MiniJavaTestBuilder assertions don't meet your exact use case. In those cases, you can use:

For example, if we want to assert that the number of lines of errors printed matches some expected number, we can implement a custom assertion like this using a lambda expression :

int expectedNumberOfLines = ...;
new MiniJavaTestBuilder()
        .assertSystemErr(systemErr -> {
            // your code goes here
            int numberOfLines = systemErr.split("\n").length;
            assertEquals(expectedNumberOfLines, numberOfLines);
        })
        .testCompiler(...);

ExecutionResult stores the contents printed to System.out, System.err, and the exit status. You can use it to perform assertions on some or all of them, such as asserting that System.out matches System.err and their length combined equals to the exit status:

new MiniJavaTestBuilder()
        .assertExecutionResult(result -> {
            String stdout = result.systemOut();
            String stderr = result.systemErr();
            int exitStatus = result.exitStatus();

            assertEquals(stderr, stdout);
            assertEquals(exitStatus, stderr.length() + stdout.length());
        })
        .testCompiler(...);

CSE401TestingUtils

MiniJavaTestBuilder is built on top of CSE401TestingUtils, the collection of more generic testing utility methods. You do not need to understand what is available or how to use them, but they can be helpful if you want to implement a more customized testing infrastructure. To learn more, you can read the CSE401TestingUtils JavaDoc comments to see the list of methods and their example usage.