Permalink
Browse files

Added equivalence assertion

  • Loading branch information...
1 parent 57b4934 commit 8e01b199310d9f781616ec3556eae03a99114b97 Alan Escreet committed May 5, 2011
Showing with 84 additions and 0 deletions.
  1. +62 −0 src/main/java/org/junit/Assert.java
  2. +22 −0 src/test/java/org/junit/tests/assertion/AssertionTest.java
@@ -885,4 +885,66 @@ public static void assertEquals(Object[] expecteds, Object[] actuals) {
public static <T> void assertGreaterThan(Comparable<T> reference, Comparable<T> actual) {
assertGreaterThan(null, reference, actual);
}
+
+ /**
+ * Asserts that <code>actual</code> is equivalent to <code>reference</code>.
+ * If not, an {@link AssertionError} is thrown. The comparison will fail if
+ * either <code>actual</code> or <code>reference</code> is <code>null</code>
+ * .
+ * <p>
+ * Note that this tests <em>equivalence</em>, not equality, i.e.
+ * <code>actual.compareTo(reference) == 0</code> not
+ * <code>actual.equalTo(reference)</code>.
+ * <p>
+ * Virtually all Java core classes that implement <code>Comparable</code>
+ * have natural orderings that are consistent with equals. One exception is
+ * <code>java.math.BigDecimal</code>, whose natural ordering equates
+ * <code>BigDecimal</code> objects with equal values and different
+ * precisions (such as 4.0 and 4.00).
+ *
+ * @param message
+ * The identifying message for the {@link AssertionError} (
+ * <code>null</code> okay)
+ * @param reference
+ * The comparison reference value
+ * @param actual
+ * The value to check against <code>reference</code>
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> void assertEquivalent(String message,
+ Comparable<T> reference, Comparable<T> actual) {
+ if (assertComparableNullSafe(message, reference, actual, "equivalent to")) {
+ if (actual.compareTo((T) reference) == 0) {
+ return;
+ } else {
+ failComparable(message, reference, actual, "equivalent to");
+ }
+ }
+ }
+
+ /**
+ * Asserts that <code>actual</code> is equivalent to <code>reference</code>.
+ * If not, an {@link AssertionError} is thrown. The comparison will fail if
+ * either <code>actual</code> or <code>reference</code> is <code>null</code>
+ * .
+ * <p>
+ * Note that this tests <em>equivalence</em>, not equality, i.e.
+ * <code>actual.compareTo(reference) == 0</code> not
+ * <code>actual.equalTo(reference)</code>.
+ * <p>
+ * Virtually all Java core classes that implement <code>Comparable</code>
+ * have natural orderings that are consistent with equals. One exception is
+ * <code>java.math.BigDecimal</code>, whose natural ordering equates
+ * <code>BigDecimal</code> objects with equal values and different
+ * precisions (such as 4.0 and 4.00).
+ *
+ * @param reference
+ * The comparison reference value
+ * @param actual
+ * The value to check against <code>reference</code>
+ */
+ public static <T> void assertEquivalent(Comparable<T> reference,
+ Comparable<T> actual) {
+ assertEquivalent(null, reference, actual);
+ }
}
@@ -4,6 +4,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertEquivalent;
import static org.junit.Assert.assertGreaterThan;
import static org.junit.Assert.assertLessThan;
import static org.junit.Assert.assertNotSame;
@@ -489,6 +490,7 @@ public void compareBigDecimalAndInteger() {
private static final BigDecimal REFERENCE_VALUE = BigDecimal.ONE;
private static final BigDecimal LOWER_VALUE = new BigDecimal("0.99");
private static final BigDecimal HIGHER_VALUE = new BigDecimal("1.01");
+ private static final BigDecimal EQUIVALENT_VALUE = new BigDecimal("1.0");
@Test
public void assertLessThanFailsWithSelfExplanatoryMessage() {
@@ -559,4 +561,24 @@ public void assertGreaterThanShouldFailWhenActualEqualToReference() {
public void assertGreaterThanShouldFailWhenActualLessThanReference() {
assertGreaterThan(REFERENCE_VALUE, LOWER_VALUE);
}
+
+ @Test
+ public void assertEquivalentShouldPassWhenActualEqualToReference() {
+ assertEquivalent(REFERENCE_VALUE, REFERENCE_VALUE);
+ }
+
+ @Test
+ public void assertEquivalentShouldPassWhenActualEquivalentToButNotEqualToReference() {
+ assertEquivalent(REFERENCE_VALUE, EQUIVALENT_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertEquivalentShouldFailWhenActualLessThanReference() {
+ assertEquivalent(REFERENCE_VALUE, LOWER_VALUE);
+ }
+
+ @Test(expected=AssertionError.class)
+ public void assertEquivalentShouldFailWhenActualGreaterThanReference() {
+ assertEquivalent(REFERENCE_VALUE, HIGHER_VALUE);
+ }
}

0 comments on commit 8e01b19

Please sign in to comment.