class AssertException extends Exception {
private static final long serialVersionUID = 1L;
+ public AssertException(String reason, Exception source) {
+ super(reason, source);
+ }
+
public AssertException(String reason) {
super(reason);
}
* @throws Exception
* in case of error
*/
+ @SuppressWarnings("unused")
public void setUp() throws Exception {
}
* @throws Exception
* in case of error
*/
+ @SuppressWarnings("unused")
public void tearDown() throws Exception {
}
/**
* Check that 2 {@link Object}s are equals.
*
- * @param the
- * error message to display if they differ
+ * @param errorMessage
+ * the error message to display if they differ
* @param expected
* the expected value
* @param actual
*/
public void assertEquals(String errorMessage, Object expected, Object actual)
throws AssertException {
-
- if (errorMessage == null) {
- errorMessage = String.format("" //
- + "Assertion failed!\n" //
- + "Expected value: [%s]\n" //
- + "Actual value: [%s]", expected, actual);
- }
-
if ((expected == null && actual != null)
|| (expected != null && !expected.equals(actual))) {
- throw new AssertException(errorMessage);
+ if (errorMessage == null) {
+ throw new AssertException(generateAssertMessage(expected,
+ actual));
+ }
+
+ throw new AssertException(errorMessage, new AssertException(
+ generateAssertMessage(expected, actual)));
}
}
/**
* Check that 2 {@link Object}s are equals.
*
- * @param the
- * error message to display if they differ
+ * @param errorMessage
+ * the error message to display if they differ
* @param expected
* the expected value
* @param actual
/**
* Check that 2 {@link Object}s are equals.
*
- * @param the
- * error message to display if they differ
+ * @param errorMessage
+ * the error message to display if they differ
* @param expected
* the expected value
* @param actual
/**
* Check that 2 {@link Object}s are equals.
*
- * @param the
- * error message to display if they differ
+ * @param errorMessage
+ * the error message to display if they differ
* @param expected
* the expected value
* @param actual
throws AssertException {
assertEquals(errorMessage, new Double(expected), new Double(actual));
}
+
+ /**
+ * Check that given {@link Object} is not NULL.
+ *
+ * @param errorMessage
+ * the error message to display if it is NULL
+ * @param actual
+ * the actual value
+ *
+ * @throws AssertException
+ * in case they differ
+ */
+ public void assertNotNull(String errorMessage, Object actual)
+ throws AssertException {
+ if (actual == null) {
+ String defaultReason = String.format("" //
+ + "Assertion failed!\n" //
+ + "Object should have been NULL: [%s]", actual);
+
+ if (errorMessage == null) {
+ throw new AssertException(defaultReason);
+ }
+
+ throw new AssertException(errorMessage, new AssertException(
+ defaultReason));
+ }
+ }
+
+ /**
+ * Generate the default assert message for 2 different values that were
+ * supposed to be equals.
+ *
+ * @param expected
+ * the expected value
+ * @param actual
+ * the actual value
+ *
+ * @return the message
+ */
+ public static String generateAssertMessage(Object expected, Object actual) {
+ return String.format("" //
+ + "Assertion failed!\n" //
+ + "Expected value: [%s]\n" //
+ + "Actual value: [%s]", expected, actual);
+ }
}