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 JUnit.isRunning() == true ?
Your are really violating the idea of TDD if your code if doing something different for the Test. Why do you only need to change it inside the test?
If you're doing things differently because you're doing a unit test you're defeating the purpose of a unit test. A unit test is supposed to perform exactly like production would (except for the setup and teardown of any necessary data and such you need....but that is included in the JUnit test itself and not your code).
However, if you truly do have a good reason for this I'd look at the stack and see if JUnit is there.
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.
I agree with the previous answers, it seems like a bad idea to mix testcode in production code. If you cannot perform the tests you want with JUnit and a mock framework you should probably rethink your design. Good mocking frameworks: http://easymock.org/ http://mockito.org/