0

I want to recognize at runtime whether my JUnit tests are being run from ant or not. The reason for this is, if it's being run from ant, I want to log information to a log file, else I just want to write to standard out.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 1
    Instead, can't you set it up to redirect output from `ant`? – Sotirios Delimanolis Nov 18 '14 at 20:32
  • @SotiriosDelimanolis would you mind elaborating? –  Nov 18 '14 at 20:34
  • This is a guess, but I'd image ant would be able to do this. Ant launches a JVM to run your tests. There might be a way to configure that JVM to redirect its output a different location/file. That's where I would start. Your way is kind of backwards. – Sotirios Delimanolis Nov 18 '14 at 20:36
  • Did you try to configure logger properly? When start from ant use configuration with ConsoleAppender. – Dmytro Nov 18 '14 at 20:38

2 Answers2

1

Ant's built in JUnit Task has a showoutput attribute which will send any output produced from the tests to ant's logging system.

See also this SO question ant junit task does not report detail about reporting options.

Community
  • 1
  • 1
dkatzel
  • 31,188
  • 3
  • 63
  • 67
0

One way to accomplish this would be to include a property in the junit task in the Ant script:

<junit fork="yes">
  <jvmarg value="-Druntime.agent=ANT"/>
  ...
</junit>

Then, in your JUnit test harness inquire about the existence and/or property for that property:

String agent = System.getProperty("runtime.agent");
if ("ANT".equals(agent)) {
    // set up file logging
} else {
    // set up STDOUT logging
}
Brent Worden
  • 10,624
  • 7
  • 52
  • 57