Is it possible for e JUnit test to tell if it's running in Eclipse (rather than ant)

ant, eclipse, java, junit, xml

Solution

Here are 2 solutions.

Use system properties

boolean isEclipse() {
    return System.getProperty("java.class.path").contains("eclipse");
}

Use stacktrace

boolean isEclipse() {
    Throwable t = new Throwable();
    StackTraceElement[] trace = t.getStackTrace();
    return trace[trace.length - 1].getClassName().startsWith("org.eclipse");
}

Problem

I have a test that compares a large blob of expected XML with the actual XML received. If the XML is significantly different, the actual XML is written to disk for analysis and the test fails. I would prefer to use assertEquals so that I can compare the XML more easily in Eclipse - but this could lead to very large JUnit and CruiseControl logs. Is there a way I can change a JUnit test behaviour depending on whether it's running through Eclipse or through Ant.

Original source