我有一个测试,将一大块预期的 XML 与实际收到的 XML 进行比较。如果 XML 明显不同,则将实际的 XML 写入磁盘进行分析并且测试失败。
我更愿意使用 assertEquals,这样我可以在 Eclipse 中更轻松地比较 XML - 但这可能会导致非常大的 JUnit 和 CruiseControl 日志。
有没有一种方法可以根据它是通过 Eclipse 还是通过 Ant 运行来更改 JUnit 测试行为。
最佳答案
这里有 2 个解决方案。
使用系统属性
boolean isEclipse() {
return System.getProperty("java.class.path").contains("eclipse");
}
使用堆栈跟踪
boolean isEclipse() {
Throwable t = new Throwable();
StackTraceElement[] trace = t.getStackTrace();
return trace[trace.length - 1].getClassName().startsWith("org.eclipse");
}
关于java - e JUnit 测试是否有可能判断它是否在 Eclipse 中运行(而不是 ant),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4375602/