FileDocCategorySizeDatePackage
MoreAsserts.javaAPI DocAndroid 5.1 API20943Thu Mar 12 22:22:42 GMT 2015android.test

MoreAsserts

public final class MoreAsserts extends Object
Contains additional assertion methods not found in JUnit.

Fields Summary
Constructors Summary
private MoreAsserts()

 
Methods Summary
public static voidassertAssignableFrom(java.lang.Class expected, java.lang.Object actual)
Asserts that the class {@code expected} is assignable from the object {@code actual}. This verifies {@code expected} is a parent class or a interface that {@code actual} implements.

        assertAssignableFrom(expected, actual.getClass());
    
public static voidassertAssignableFrom(java.lang.Class expected, java.lang.Class actual)
Asserts that class {@code expected} is assignable from the class {@code actual}. This verifies {@code expected} is a parent class or a interface that {@code actual} implements.

        Assert.assertTrue(
                "Expected " + expected.getCanonicalName() +
                        " to be assignable from actual class " + actual.getCanonicalName(),
                expected.isAssignableFrom(actual));
    
public static java.util.regex.MatchResultassertContainsRegex(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)
Asserts that {@code expectedRegex} matches any substring of {@code actual} and fails with {@code message} if it does not. The Matcher is returned in case the test needs access to any captured groups. Note that you can also use this for a literal string, by wrapping your expected string in {@link Pattern#quote}.

        if (actual == null) {
            failNotContains(message, expectedRegex, actual);
        }
        Matcher matcher = getMatcher(expectedRegex, actual);
        if (!matcher.find()) {
            failNotContains(message, expectedRegex, actual);
        }
        return matcher;
    
public static java.util.regex.MatchResultassertContainsRegex(java.lang.String expectedRegex, java.lang.String actual)
Variant of {@link #assertContainsRegex(String,String,String)} using a generic message.

        return assertContainsRegex(null, expectedRegex, actual);
    
public static voidassertContentsInAnyOrder(java.lang.String message, java.lang.Iterable actual, java.lang.Object expected)
Asserts that {@code actual} contains precisely the elements {@code expected}, but in any order.

        HashMap<Object, Object> expectedMap = new HashMap<Object, Object>(expected.length);
        for (Object expectedObj : expected) {
            expectedMap.put(expectedObj, expectedObj);
        }

        for (Object actualObj : actual) {
            if (expectedMap.remove(actualObj) == null) {
                failWithMessage(message, "Extra object in actual: (" + actualObj.toString() + ")");
            }
        }
        
        if (expectedMap.size() > 0) {
            failWithMessage(message, "Extra objects in expected.");
        }
    
public static voidassertContentsInAnyOrder(java.lang.Iterable actual, java.lang.Object expected)
Variant of assertContentsInAnyOrder(String, Iterable, Object...) using a generic message.

        assertContentsInAnyOrder((String)null, actual, expected);
    
public static voidassertContentsInOrder(java.lang.String message, java.lang.Iterable actual, java.lang.Object expected)
Asserts that {@code actual} contains precisely the elements {@code expected}, and in the same order.

        ArrayList actualList = new ArrayList();
        for (Object o : actual) {
            actualList.add(o);
        }
        Assert.assertEquals(message, Arrays.asList(expected), actualList);
    
public static voidassertContentsInOrder(java.lang.Iterable actual, java.lang.Object expected)
Variant of assertContentsInOrder(String, Iterable, Object...) using a generic message.

        assertContentsInOrder((String) null, actual, expected);
    
public static voidassertEmpty(java.lang.String message, java.lang.Iterable iterable)
Asserts that {@code iterable} is empty.

        if (iterable.iterator().hasNext()) {
            failNotEmpty(message, iterable.toString());
        }
    
public static voidassertEmpty(java.lang.Iterable iterable)
Variant of {@link #assertEmpty(String, Iterable)} using a generic message.

        assertEmpty(null, iterable);
    
public static voidassertEmpty(java.lang.String message, java.util.Map map)
Asserts that {@code map} is empty.

        if (!map.isEmpty()) {
            failNotEmpty(message, map.toString());
        }
    
public static voidassertEmpty(java.util.Map map)
Variant of {@link #assertEmpty(String, Map)} using a generic message.

        assertEmpty(null, map);
    
public static voidassertEquals(java.lang.String message, long[] expected, long[] actual)

hide
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates first specific element mismatch.

        if (expected.length != actual.length) {
            failWrongLength(message, expected.length, actual.length);
        }
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] != actual[i]) {
                failWrongElement(message, i, expected[i], actual[i]);
            }
        }
    
public static voidassertEquals(long[] expected, long[] actual)

hide
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates first specific element mismatch.

        assertEquals(null, expected, actual);
    
public static voidassertEquals(java.lang.String message, double[] expected, double[] actual)
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates first specific element mismatch.

        if (expected.length != actual.length) {
            failWrongLength(message, expected.length, actual.length);
        }
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] != actual[i]) {
                failWrongElement(message, i, expected[i], actual[i]);
            }
        }
    
public static voidassertEquals(double[] expected, double[] actual)
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates first specific element mismatch.

        assertEquals(null, expected, actual);
    
public static voidassertEquals(java.lang.String message, java.lang.Object[] expected, java.lang.Object[] actual)
Asserts that array {@code actual} is the same size and every element is the same as those in array {@code expected}. Note that this uses {@code equals()} instead of {@code ==} to compare the objects. {@code null} will be considered equal to {@code null} (unlike SQL). On failure, message indicates first specific element mismatch.

        if (expected.length != actual.length) {
            failWrongLength(message, expected.length, actual.length);
        }
        for (int i = 0; i < expected.length; i++) {
            Object exp = expected[i];
            Object act = actual[i];
            // The following borrowed from java.util.equals(Object[], Object[]).
            if (!((exp==null) ? act==null : exp.equals(act))) {
                failWrongElement(message, i, exp, act);
            }
        }
    
public static voidassertEquals(java.lang.Object[] expected, java.lang.Object[] actual)
Asserts that array {@code actual} is the same size and every element is the same as those in array {@code expected}. Note that this uses {@code ==} instead of {@code equals()} to compare the objects. On failure, message indicates first specific element mismatch.

        assertEquals(null, expected, actual);
    
public static voidassertEquals(java.lang.String message, java.util.Set expected, java.util.Set actual)
Asserts that two sets contain the same elements.

        Set<Object> onlyInExpected = new HashSet<Object>(expected);
        onlyInExpected.removeAll(actual);
        Set<Object> onlyInActual = new HashSet<Object>(actual);
        onlyInActual.removeAll(expected);
        if (onlyInExpected.size() != 0 || onlyInActual.size() != 0) {
            Set<Object> intersection = new HashSet<Object>(expected);
            intersection.retainAll(actual);
            failWithMessage(
                    message,
                    "Sets do not match.\nOnly in expected: " + onlyInExpected
                    + "\nOnly in actual: " + onlyInActual
                    + "\nIntersection: " + intersection);
        }
    
public static voidassertEquals(java.util.Set expected, java.util.Set actual)
Asserts that two sets contain the same elements.

        assertEquals(null, expected, actual);
    
public static voidassertEquals(java.lang.String message, byte[] expected, byte[] actual)
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates specific element mismatch.

        if (expected.length != actual.length) {
            failWrongLength(message, expected.length, actual.length);
        }
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] != actual[i]) {
                failWrongElement(message, i, expected[i], actual[i]);
            }
        }
    
public static voidassertEquals(byte[] expected, byte[] actual)
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates specific element mismatch.

        assertEquals(null, expected, actual);
    
public static voidassertEquals(java.lang.String message, int[] expected, int[] actual)
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates first specific element mismatch.

        if (expected.length != actual.length) {
            failWrongLength(message, expected.length, actual.length);
        }
        for (int i = 0; i < expected.length; i++) {
            if (expected[i] != actual[i]) {
                failWrongElement(message, i, expected[i], actual[i]);
            }
        }
    
public static voidassertEquals(int[] expected, int[] actual)
Asserts that array {@code actual} is the same size and every element equals those in array {@code expected}. On failure, message indicates first specific element mismatch.

        assertEquals(null, expected, actual);
    
public static java.util.regex.MatchResultassertMatchesRegex(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)
Asserts that {@code expectedRegex} exactly matches {@code actual} and fails with {@code message} if it does not. The MatchResult is returned in case the test needs access to any captured groups. Note that you can also use this for a literal string, by wrapping your expected string in {@link Pattern#quote}.

        if (actual == null) {
            failNotMatches(message, expectedRegex, actual);
        }
        Matcher matcher = getMatcher(expectedRegex, actual);
        if (!matcher.matches()) {
            failNotMatches(message, expectedRegex, actual);
        }
        return matcher;
    
public static java.util.regex.MatchResultassertMatchesRegex(java.lang.String expectedRegex, java.lang.String actual)
Variant of {@link #assertMatchesRegex(String,String,String)} using a generic message.

        return assertMatchesRegex(null, expectedRegex, actual);
    
public static voidassertNotContainsRegex(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)
Asserts that {@code expectedRegex} does not match any substring of {@code actual}, and fails with {@code message} if it does. Note that you can also use this for a literal string, by wrapping your expected string in {@link Pattern#quote}.

        Matcher matcher = getMatcher(expectedRegex, actual);
        if (matcher.find()) {
            failContains(message, expectedRegex, actual);
        }
    
public static voidassertNotContainsRegex(java.lang.String expectedRegex, java.lang.String actual)
Variant of {@link #assertNotContainsRegex(String,String,String)} using a generic message.

        assertNotContainsRegex(null, expectedRegex, actual);
    
public static voidassertNotEmpty(java.lang.String message, java.lang.Iterable iterable)
Asserts that {@code iterable} is not empty.

        if (!iterable.iterator().hasNext()) {
            failEmpty(message);
        }
    
public static voidassertNotEmpty(java.lang.Iterable iterable)
Variant of assertNotEmpty(String, Iterable) using a generic message.

        assertNotEmpty(null, iterable);
    
public static voidassertNotEmpty(java.lang.String message, java.util.Map map)
Asserts that {@code map} is not empty.

        if (map.isEmpty()) {
            failEmpty(message);
        }
    
public static voidassertNotEmpty(java.util.Map map)
Variant of {@link #assertNotEmpty(String, Map)} using a generic message.

        assertNotEmpty(null, map);
    
public static voidassertNotEqual(java.lang.String message, java.lang.Object unexpected, java.lang.Object actual)
Asserts that {@code actual} is not equal {@code unexpected}, according to both {@code ==} and {@link Object#equals}.

        if (equal(unexpected, actual)) {
            failEqual(message, unexpected);
        }
    
public static voidassertNotEqual(java.lang.Object unexpected, java.lang.Object actual)
Variant of {@link #assertNotEqual(String,Object,Object)} using a generic message.

        assertNotEqual(null, unexpected, actual);
    
public static voidassertNotMatchesRegex(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)
Asserts that {@code expectedRegex} does not exactly match {@code actual}, and fails with {@code message} if it does. Note that you can also use this for a literal string, by wrapping your expected string in {@link Pattern#quote}.

        Matcher matcher = getMatcher(expectedRegex, actual);
        if (matcher.matches()) {
            failMatch(message, expectedRegex, actual);
        }
    
public static voidassertNotMatchesRegex(java.lang.String expectedRegex, java.lang.String actual)
Variant of {@link #assertNotMatchesRegex(String,String,String)} using a generic message.

        assertNotMatchesRegex(null, expectedRegex, actual);
    
public static voidcheckEqualsAndHashCodeMethods(java.lang.String message, java.lang.Object lhs, java.lang.Object rhs, boolean expectedResult)
Utility for testing equals() and hashCode() results at once. Tests that lhs.equals(rhs) matches expectedResult, as well as rhs.equals(lhs). Also tests that hashCode() return values are equal if expectedResult is true. (hashCode() is not tested if expectedResult is false, as unequal objects can have equal hashCodes.)

param
lhs An Object for which equals() and hashCode() are to be tested.
param
rhs As lhs.
param
expectedResult True if the objects should compare equal, false if not.


        if ((lhs == null) && (rhs == null)) {
            Assert.assertTrue(
                    "Your check is dubious...why would you expect null != null?",
                    expectedResult);
            return;
        }

        if ((lhs == null) || (rhs == null)) {
            Assert.assertFalse(
                    "Your check is dubious...why would you expect an object "
                            + "to be equal to null?", expectedResult);
        }

        if (lhs != null) {
            Assert.assertEquals(message, expectedResult, lhs.equals(rhs));
        }
        if (rhs != null) {
            Assert.assertEquals(message, expectedResult, rhs.equals(lhs));
        }

        if (expectedResult) {
            String hashMessage =
                    "hashCode() values for equal objects should be the same";
            if (message != null) {
                hashMessage += ": " + message;
            }
            Assert.assertTrue(hashMessage, lhs.hashCode() == rhs.hashCode());
        }
    
public static voidcheckEqualsAndHashCodeMethods(java.lang.Object lhs, java.lang.Object rhs, boolean expectedResult)
Variant of checkEqualsAndHashCodeMethods(String,Object,Object,boolean...)} using a generic message.

        checkEqualsAndHashCodeMethods((String) null, lhs, rhs, expectedResult);
    
private static booleanequal(java.lang.Object a, java.lang.Object b)

        return a == b || (a != null && a.equals(b));
    
private static voidfailContains(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)

        failWithMessage(message, "expected not to contain regex:<" + expectedRegex
                + "> but was:<" + actual + '>");
    
private static voidfailEmpty(java.lang.String message)

        failWithMessage(message, "expected not to be empty, but was");
    
private static voidfailEqual(java.lang.String message, java.lang.Object unexpected)

        failWithMessage(message, "expected not to be:<" + unexpected + ">");
    
private static voidfailMatch(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)

        failWithMessage(message, "expected not to match regex:<" + expectedRegex
                + "> but was:<" + actual + '>");
    
private static voidfailNotContains(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)

        String actualDesc = (actual == null) ? "null" : ('<" + actual + '>");
        failWithMessage(message, "expected to contain regex:<" + expectedRegex
                + "> but was:" + actualDesc);
    
private static voidfailNotEmpty(java.lang.String message, java.lang.String actual)

        failWithMessage(message, "expected to be empty, but contained: <"
                + actual + ">");
    
private static voidfailNotMatches(java.lang.String message, java.lang.String expectedRegex, java.lang.String actual)

        String actualDesc = (actual == null) ? "null" : ('<" + actual + '>");
        failWithMessage(message, "expected to match regex:<" + expectedRegex
                + "> but was:" + actualDesc);
    
private static voidfailWithMessage(java.lang.String userMessage, java.lang.String ourMessage)

        Assert.fail((userMessage == null)
                ? ourMessage
                : userMessage + ' " + ourMessage);
    
private static voidfailWrongElement(java.lang.String message, int index, java.lang.Object expected, java.lang.Object actual)

        failWithMessage(message, "expected array element[" + index + "]:<"
                + expected + "> but was:<" + actual + '>");
    
private static voidfailWrongLength(java.lang.String message, int expected, int actual)

        failWithMessage(message, "expected array length:<" + expected
                + "> but was:<" + actual + '>");
    
private static java.util.regex.MatchergetMatcher(java.lang.String expectedRegex, java.lang.String actual)

        Pattern pattern = Pattern.compile(expectedRegex);
        return pattern.matcher(actual);