Skip to content

Commit

Permalink
Assert: Use JUnit 5 assertions
Browse files Browse the repository at this point in the history
Currently, Assert implements our custom assertThrows using try/catch.

This implementation was done when we used JUnit 4, and had no proper
support for assertThrows. As we migrate to JUnit 5[1], we can make use
of JUnit 5's Assertions.assertThrows. However, our current
assertThrows supports checking the correctness of error's message,
whcih JUnit 5 does not support.

Let's migrate Assert.java to use JUnit 5's assertThrows. In addition,
let's create a custom assertThrowsWithMessage to support checking the
correctness of error messages.

The old assertThrows is kept for backward compatability, as many
existing classes depend on it. It has been marked as deprecated, and
will be removed in a future commit after migrating all tests.

[1] se-edu#951
  • Loading branch information
sijie123 committed Feb 23, 2019
1 parent 921e61f commit 42b986c
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions src/test/java/seedu/address/testutil/Assert.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
package seedu.address.testutil;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.function.Executable;

/**
* A set of assertion methods useful for writing tests.
*/
public class Assert {

/**
* Asserts that the {@code callable} throws the {@code expected} Exception.
* Asserts that the {@code executable} throws the {@code expectedType} Exception.
* This is a wrapper method that invokes
* {@code Assertions.assertThrows(Class<? extends Throwable>, Executable)}, to maintain
* consistency with our custom
* {@see assertThrowsWithMessage(Class<? extends Throwable>, String, Executable)} method.
*/
public static void assertThrows(Class<? extends Throwable> expectedType, Executable executable) {
Assertions.assertThrows(expectedType, executable);
}

/**
* Asserts that the {@code executable} throws the {@code expectedType} Exception. Otherwise, display
* {@code feedbackMessage} in the error log.
* This is a wrapper method that invokes
* {@code Assertions.assertThrows(Class<? extends Throwable>, Executable}, to maintain
* consistency with our custom
* {@see assertThrowsWithMessage(Class<? extends Throwable>, String, Executable)} method.
*/
public static void assertThrows(Class<? extends Throwable> expected, VoidCallable callable) {
assertThrows(expected, null, callable);
public static void assertThrows(Class<? extends Throwable> expectedType, Executable executable,
String feedbackMessage) {
Assertions.assertThrows(expectedType, executable, feedbackMessage);
}

/**
* Asserts that the {@code callable} throws the {@code expectedException} and the {@code expectedMessage}.
* If there's no need for the verification of the exception's error message, call
* {@code assertThrows(Class<? extends Throwable>, VoidCallable)} instead.
* {@see assertThrows(Class<? extends Throwable>, VoidCallable}
*
* @deprecated use {@see assertThrowsWithMessage(Class<? extends Throwable>, String, Executable)} instead.
*/
@Deprecated
public static void assertThrows(Class<? extends Throwable> expectedException, String expectedMessage,
VoidCallable callable) {
try {
Expand All @@ -42,10 +62,39 @@ public static void assertThrows(Class<? extends Throwable> expectedException, St
"Expected %s to be thrown, but nothing was thrown.", expectedException.getName()));
}


/**
* Asserts that the {@code executable} throws the {@code expectedType} Exception with the
* {@code expectedMessage} message. If there's no need for the verification of the exception's error
* message, call {@code assertThrows(Class<? extends Throwable>, Executable)} instead.
* {@see assertThrows(Class<? extends Throwable>, Executable}}
*/
public static void assertThrowsWithMessage(Class<? extends Throwable> expectedType, String expectedMessage,
Executable executable) {
Throwable thrownException = Assertions.assertThrows(expectedType, executable);
Assertions.assertEquals(expectedMessage, thrownException.getMessage());
}

/**
* Asserts that the {@code executable} throws the {@code expectedType} Exception with the
* {@code expectedMessage} message. Otherwise, display {@code feedbackMessage}} in the error log.
* If there's no need for the verification of the exception's error message, call
* {@code assertThrows(Class<? extends Throwable>, Executable)} instead.
* {@see assertThrows(Class<? extends Throwable>, Executable}}
*/
public static void assertThrowsWithMessage(Class<? extends Throwable> expectedType, String expectedMessage,
Executable executable, String feedbackMessage) {
Throwable thrownException = Assertions.assertThrows(expectedType, executable, feedbackMessage);
Assertions.assertEquals(expectedMessage, thrownException.getMessage(), feedbackMessage);
}

/**
* Represents a function which does not return anything and may throw an exception.
*
* @deprecated use {@code org.junit.jupiter.api.function.Executable} instead
*/
@FunctionalInterface
@Deprecated
public interface VoidCallable {
void call() throws Exception;
}
Expand Down

0 comments on commit 42b986c

Please sign in to comment.