MoreAssertspublic final class MoreAsserts extends Object Contains additional assertion methods not found in JUnit. |
Constructors Summary |
---|
private MoreAsserts()
|
Methods Summary |
---|
public static void | assertAssignableFrom(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 void | assertAssignableFrom(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.MatchResult | assertContainsRegex(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.MatchResult | assertContainsRegex(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 void | assertContentsInAnyOrder(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 void | assertContentsInAnyOrder(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 void | assertContentsInOrder(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.
Assert.assertEquals(message,
Arrays.asList(expected), Lists.newArrayList(actual));
| public static void | assertContentsInOrder(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 void | assertEmpty(java.lang.String message, java.lang.Iterable iterable)Asserts that {@code iterable} is empty.
if (iterable.iterator().hasNext()) {
failNotEmpty(message, iterable.toString());
}
| public static void | assertEmpty(java.lang.Iterable iterable)Variant of {@link #assertEmpty(String, Iterable)} using a
generic message.
assertEmpty(null, iterable);
| public static void | assertEmpty(java.lang.String message, java.util.Map map)Asserts that {@code map} is empty.
if (!map.isEmpty()) {
failNotEmpty(message, map.toString());
}
| public static void | assertEmpty(java.util.Map map)Variant of {@link #assertEmpty(String, Map)} using a generic
message.
assertEmpty(null, map);
| public static void | assertEquals(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 void | assertEquals(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 void | assertEquals(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 void | assertEquals(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 void | assertEquals(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 void | assertEquals(java.util.Set expected, java.util.Set actual)Asserts that two sets contain the same elements.
assertEquals(null, expected, actual);
| public static void | assertEquals(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 void | assertEquals(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 void | assertEquals(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 void | assertEquals(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.MatchResult | assertMatchesRegex(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.MatchResult | assertMatchesRegex(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 void | assertNotContainsRegex(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 void | assertNotContainsRegex(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 void | assertNotEmpty(java.lang.String message, java.lang.Iterable iterable)Asserts that {@code iterable} is not empty.
if (!iterable.iterator().hasNext()) {
failEmpty(message);
}
| public static void | assertNotEmpty(java.lang.Iterable iterable)Variant of assertNotEmpty(String, Iterable>)
using a generic message.
assertNotEmpty(null, iterable);
| public static void | assertNotEmpty(java.lang.String message, java.util.Map map)Asserts that {@code map} is not empty.
if (map.isEmpty()) {
failEmpty(message);
}
| public static void | assertNotEmpty(java.util.Map map)Variant of {@link #assertNotEmpty(String, Map)} using a generic
message.
assertNotEmpty(null, map);
| public static void | assertNotEqual(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 void | assertNotEqual(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 void | assertNotMatchesRegex(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 void | assertNotMatchesRegex(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 void | checkEqualsAndHashCodeMethods(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.)
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 void | checkEqualsAndHashCodeMethods(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 boolean | equal(java.lang.Object a, java.lang.Object b)
return a == b || (a != null && a.equals(b));
| private static void | failContains(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 void | failEmpty(java.lang.String message)
failWithMessage(message, "expected not to be empty, but was");
| private static void | failEqual(java.lang.String message, java.lang.Object unexpected)
failWithMessage(message, "expected not to be:<" + unexpected + ">");
| private static void | failMatch(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 void | failNotContains(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 void | failNotEmpty(java.lang.String message, java.lang.String actual)
failWithMessage(message, "expected to be empty, but contained: <"
+ actual + ">");
| private static void | failNotMatches(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 void | failWithMessage(java.lang.String userMessage, java.lang.String ourMessage)
Assert.fail((userMessage == null)
? ourMessage
: userMessage + ' " + ourMessage);
| private static void | failWrongElement(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 void | failWrongLength(java.lang.String message, int expected, int actual)
failWithMessage(message, "expected array length:<" + expected
+ "> but was:<" + actual + '>");
| private static java.util.regex.Matcher | getMatcher(java.lang.String expectedRegex, java.lang.String actual)
Pattern pattern = Pattern.compile(expectedRegex);
return pattern.matcher(actual);
|
|