Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 339
tomdee Avatar asked Dec 07 '10 10:12

tomdee


People also ask

Which methods Cannot be tested by JUnit test class?

Which methods cannot be tested by the JUnit test class? Explanation: When a method is declared as “private”, it can only be accessed within the same class. So there is no way to test a “private” method of a target class from any JUnit test class.

How do I get JUnit to work in Eclipse?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.


2 Answers

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");
}
like image 158
AlexR Avatar answered Nov 16 '22 02:11

AlexR


Yes - you can test if certain osgi properties are set (System.getProperty("osgi.instance.area") for instance). They will be empty if junit is started through ant outside of eclipse.

like image 26
Andreas Dolk Avatar answered Nov 16 '22 02:11

Andreas Dolk