1

I'm writing some tests for an Android 2.3.3 project, using the Android JUnit Test runner, and I'm seeing some weird results in the failure traces on assertions. Here's a simple example:

import junit.framework.TestCase;

public class TU_Test extends TestCase {

    public void testStuff() {
        assertEquals("aft", "af");
    }

}

The assertion obviously fails, and here's the trace copied from Eclipse:

junit.framework.ComparisonFailure: expected:<...t> but was:<...>
at com.redprairie.test.TU_Test.testStuff(TU_Test.java:33)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

The displayed expected + actual are not very helpful... it seems to display the difference / missing characters between the two, but it would be much more helpful if I could see the full values of each. I'm normally (using JUnit 4 + not using the Android Test Runner) able to double click the failure trace in Eclipse and see a diff of the two results. Is there any way to achieve this using the Android Test Runner and it's JUnit 3 style tests? It's kind of a pain in the ass to always set breakpoints.

Thanks!

4

2 回答 2

0

Have you tried catching ComparisonFailure? You can call getActual() and getExpected() on the exception object to find out the values.

Take a look at the following javadoc: http://kentbeck.github.com/junit/javadoc/4.10/org/junit/ComparisonFailure.html

于 2012-09-10T13:42:30.537 回答
0

Since I couldn't find a real solution to this, I ended up just wrapping the assertion and building the message myself like so:

private void _assertEquals(String expected, String actual) {
    assertEquals("expected <" + expected + "> but was <" + actual + ">", expected, actual);
}
于 2012-09-11T13:02:33.530 回答