How can I find out if code is running inside a JUnit test or not?

前端 未结 11 1279
梦毁少年i
梦毁少年i 2020-12-24 10:34

In my code I need to do certain fixes only when it is run inside a JUnit test. How can I find out if code is running inside a JUnit test or not? Is there something like JUni

11条回答
  •  天涯浪人
    2020-12-24 11:05

    First of all, this a probably not a good idea. You should be unit testing the actual production code, not slightly different code.

    If you really want to do this, you could look at the stacktrace, but since you are changing your program for this anyway, you might just as well introduce a new static boolean field isUnitTesting in your code, and have JUnit set this to true. Keep it simple.

提交回复
热议问题