Search code examples
junitjunit4junit3junit-runner

How to tell if called from JUnit?


I think that a piece of code under test should not know that it is under test. Therefore, this question represents an anti-pattern, but I have a need to configure a legacy application in a particular way 'if called from a JUnit integration test'. If you think that this smells like an application that does not use CDI you are correct.

I can always walk the stack to see if I was called from JUnit. I am hoping for something more elegant.


Solution

  • How about using a system property that is set by the JUnit test, evaluated by the class under test and resetted by JUnit afterwards?