Detecting if code is running in test mode

Sometimes, we need to know if the code execution was started by a junitĀ test or it was normally started by the application.

boolean isInJunitTest(){
  for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
    if (s.getClassName().contains("TestExecutionListener")) {
      return true;
    }
  }
  return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.