Assertions2

public final class Assertions2(source)

The Assertions2 class roughly mimics the functionality of the org.junit.jupiter.api.Assertions class while providing the option to pass along a Context.

Note: some methods do not provice a 1:1 mapping to the org.junit.jupiter.api.Assertions class. For example, * instead of using assertDoesNotThrow you should use * call or if you need the result of the call you should use * callObject.

Here is an example of how to convert Junit5 assertions to Assertions2 assertions:


// Junit5
Assertions.assertEquals(1 + 1, 2, "The result of 1 + 1 should be 2.");

// Assertions2
Context context = Assertions2.contextBuilder()
    .add("summand1", 1)
    .add("summand2", 1)
    .build();
Assertions2.assertEquals(2, 1 + 1, context, r -> "The result of 1 + 1 should be 2.");
Of course in this example it doesn't make much sense to use Assertions2 instead of Junit5, since the context is obvoius. Here is a more practical example:

 // Junit5, not using JSON parameter set Tests

public void testMoveToPosition() {
   World.setSize(10,10);
   var robot = new Robot(1,1, Direction.UP, 0);
   assertions2.assertDoesNotThrow(() -> MyTestClass.moveToPosition(robot, 5, 5), "The Method threw an exception.");
   assertions2.assertEquals(5, robot.getX(), "Wrong final x coordinate.");
   assertions2.assertEquals(5, robot.getY(), "The final y coordinate.");
}

// assertions2, using JSON parameter set Tests

public static final Map<String, Function<JsonNode, ?>> customConverters = Map.ofEntries(
        Map.entry("worldWidth", JsonNode::asInt),
        Map.entry("worldHeight", JsonNode::asInt),
        Map.entry("robot", JsonConverters::toRobot),
        Map.entry("expectedEndX", JsonNode::asInt),
        Map.entry("expectedEndY", JsonNode::asInt)
    );

= "inputs.json", customConverters = "customConverters")
public void testMovementInvalidDirection(final JsonParameterSet params) {
   var worldWidth = params.getInt("worldWidth");
   var worldHeight = params.getInt("worldHeight");
   World.setSize(worldWidth, worldHeight);
   var robot = params.<Robot>get("robot");
   var expectedEndX = params.getInt("expectedEndX");
   var expectedEndY = params.getInt("expectedEndY");
   var context = params.toContext("expectedEndX", "expectedEndY");
   assertions2.call(() -> MyTestClass.moveToPosition(robot, worldWidth, worldHeight), context, r -> "The Method threw
   an exception.");
   assertions2.assertEquals(expectedEndX, robot.getX(), context, r -> "Wrong final x coordinate.");
   assertions2.assertEquals(expectedEndY, robot.getY(), context, r -> "The final y coordinate.");
}

In this example the student gets way more information about the test failure than in the Junit5 example while also providing a more readable test method.

Author

Dustin Glaser

Functions

Link copied to clipboard
public static void assertArrayEquals(Array<Object> expected, Array<Object> actual, Context context, PreCommentSupplier<? super ResultOfFail> preCommentSupplier)
Asserts that two arrays are equal.
Link copied to clipboard
public static T assertCallEquals<T>(T expected, ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the callable returns an object equal to the given expected object.
Link copied to clipboard
public static boolean assertCallFalse(ObjectCallable<Boolean> callable, Context context, PreCommentSupplier<? super ResultOfObject<Boolean>> preCommentSupplier)
Asserts that the callable returns FALSE.
Link copied to clipboard
public static T assertCallNotEquals<T>(T unexpected, ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the callable returns an object not equal to the given object.
Link copied to clipboard
public static T assertCallNotNull<T>(ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the callable returns an object.
Link copied to clipboard
public static T assertCallNotSame<T>(T unexpected, ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given callable does not return the given object.
Link copied to clipboard
public static T assertCallNull<T>(ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given callable returns null.
Link copied to clipboard
public static T assertCallSame<T>(T expected, ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given callable returns the given object.
Link copied to clipboard
public static boolean assertCallTrue(ObjectCallable<Boolean> callable, Context context, PreCommentSupplier<? super ResultOfObject<Boolean>> preCommentSupplier)
Asserts that the given callable returns TRUE.
Link copied to clipboard
public static T assertEquals<T>(T expected, T actual, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given actual object is equal to the given expected object.
Link copied to clipboard
public static boolean assertFalse(boolean actual, Context context, PreCommentSupplier<? super ResultOfObject<Boolean>> preCommentSupplier)
Asserts that the callable returns FALSE.
Link copied to clipboard
public static void assertIterableEquals(Iterable<? extends Object> expected, Iterable<? extends Object> actual, Context context, PreCommentSupplier<? super ResultOfFail> preCommentSupplier)
public static void assertIterableEquals(Iterable<? extends Object> expected, Iterable<? extends Object> actual, Context context, PreCommentSupplier<? super ResultOfFail> preCommentSupplier, String iterableName)
Asserts that two Iterables are equal.
Link copied to clipboard
public static T assertNotEquals<T>(T unexpected, T actual, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given actual object is not equal to the given unexpected object.
Link copied to clipboard
public static T assertNotNull<T>(T actual, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given actual object is not null.
Link copied to clipboard
public static T assertNotSame<T>(T unexpected, T actual, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given actual object is not the same as the given unexpected object.
Link copied to clipboard
public static T assertNull<T>(T actual, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given actual object is null.
Link copied to clipboard
public static T assertSame<T>(T expected, T actual, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Asserts that the given actual object is the same as the given expected object.
Link copied to clipboard
public static T assertThrows<T extends Exception>(Class<T> expected, Callable callable, Context context, PreCommentSupplier<? super ResultOfExceptionalCall<T>> preCommentSupplier)
Asserts that the given callable throws an exception of the given expected type.
Link copied to clipboard
public static boolean assertTrue(boolean actual, Context context, PreCommentSupplier<? super ResultOfObject<Boolean>> preCommentSupplier)
Asserts that the given boolean value is true.
Link copied to clipboard
public static void call(Callable callable)
public static void call(Callable callable, Context context, PreCommentSupplier<? super ResultOfCall> preCommentSupplier)
public static void call(MethodLink methodLink, Object instance, Context context, PreCommentSupplier<? super ResultOfCall> preCommentSupplier, Array<Object> args)
Asserts that the given callable does not throw an exception.
Link copied to clipboard
public static T callObject<T>(ObjectCallable<T> callable)
public static T callObject<T>(ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
public static T callObject<T>(MethodLink methodLink, Object instance, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier, Array<Object> args)
Asserts that the given callable does not throw an exception and returns the object returned by the callable.
Link copied to clipboard
public static Context context(Array<Object> records)
Link copied to clipboard
public static Context.Builder<? extends Object> contextBuilder()
Returns a contexts builder.
Link copied to clipboard
public static Context emptyContext()
Returns an empty contexts.
Link copied to clipboard
public static T fail<T>(Context context, PreCommentSupplier<? super ResultOfFail> preCommentSupplier)
public static T fail<T>(Throwable cause, Context context, PreCommentSupplier<? super ResultOfFail> preCommentSupplier)
Fails.
public static T fail<T>(Expected expected, Actual actual, Context context, PreCommentSupplier<? super ResultOfFail> preCommentSupplier)
Link copied to clipboard
public static Fail.Builder failBuilder()
Returns a fail builder.
Link copied to clipboard
public static T objectAssert<T>(ExpectedObject<T> expected, T object, Context context)
public static T objectAssert<T>(ExpectedObject<T> expected, T object, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
public static T objectAssert<T>(ExpectedObject<T> expected, ObjectCallable<T> callable, Context context)
public static T objectAssert<T>(ExpectedObject<T> expected, ObjectCallable<T> callable, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
public static T objectAssert<T>(ExpectedObject<T> expected, T object, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
public static T objectAssert<T>(ExpectedObject<T> expected, ObjectCallable<T> callable, Context context, PreCommentSupplier<? super ResultOfObject<T>> preCommentSupplier)
Link copied to clipboard
Link copied to clipboard
Returns a object test builder.
Link copied to clipboard