tags:

views:

102

answers:

4

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 ?

+3  A: 

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?

ctrlShiftBryan
+6  A: 

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.

SOA Nerd
I know this sounds a bit against TDD, but testing code for GoogleAppengine using Jersey/Jetty introduces more than one thread on my local machine, which in turn causes the new threads not to have the right mock testing environment from Google initialised, so I need to check in many points of my potential second thread if my AppEngine mock up env is properly initialised already, which I do not want to do if I am in a production server. I'ts clearly a bit messy and would be better if I could mimic the AppEngine env more closely on my local machine (i.e. have only one thread).
xamde
I think this should be your actual question. How do I get my unit tests to behave the same as the deployed environment. This might help you actually solve your issues without introducing code to actually check whether or not you are running a unit test.
Robin
Why not test for GAE using the GAE development server? It is pretty close to the real thing, and I believe they even have JUnit support now.
Thilo
+5  A: 

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.

Thilo
Yes, you might for instance have a singleton object in your class containing the isUnitTesting field, and set it to true in a JUnit @Before method.
Jim Ferrans
come to think of it, this is a little like dependency injection. He should be injecting mock objects for the dependencies that are too difficult to test, but I suppose that setting a boolean to true explicitly is still better then trying to have the class figure it out by itself.
Thilo
Guys, you convinced and encouraged me to rephrase this question. http://stackoverflow.com/questions/2351293/google-appengine-local-junit-tests-jersey-framework-embedded-jetty
xamde
A: 

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/

Ingo