简体   繁体   中英

Android Test Project JUnit Failure Trace Not Showing Expected + Actual

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!

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM