Issue
I have a strange behavior with Android Studio and Unit Testing.
I am doing instrumented unit tests (AndroidJUnitRunner
) on Android using a large json data (>800kb), and when I try to assert equality with random data:
assertEquals("ghgg", jsonData.toString());
It crashes with java.lang.RuntimeException: android.os.TransactionTooLargeException
.
While the inequality assertion does not crashes and passes green.
assertNotEquals("ghgg", jsonData.toString());
Now, if I do the same test, but with just plain non-instrumented junit, the inequality assertion does not crashes and passes green as before, and the equality assertion does not pass (red), but it does not give exception.
assertEquals("ghgg", jsonData.toString());
How come in instrumented tests, assertEquals
can not handle big strings, while assertNotEquals
can ?
Solution
I'm guessing that you only see the difference in behaviour for failing asserts?
If so this may be due to a implementation detail of the built in JUnit asserts.
The failure path is different for equals and not equals.
Equals creates a ComparisonFailure
which goes on to do some processing of both the expected and actual values.
static public void assertEquals(String message, Object expected,
Object actual) {
if (equalsRegardingNull(expected, actual)) {
return;
} else if (expected instanceof String && actual instanceof String) {
String cleanMessage = message == null ? "" : message;
throw new ComparisonFailure(cleanMessage, (String) expected,
(String) actual);
} else {
failNotEquals(message, expected, actual);
}
}
While not equals generates a failure messeage using only the actual value.
static public void assertNotEquals(String message, Object unexpected,
Object actual) {
if (equalsRegardingNull(unexpected, actual)) {
failEquals(message, actual);
}
}
private static void failEquals(String message, Object actual) {
String formatted = "Values should be different. ";
if (message != null) {
formatted = message + ". ";
}
formatted += "Actual: " + actual;
fail(formatted);
}
This is true for junit 4.12. It may not be true for earlier and/or later versions.
Answered By - henry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.