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
Lots of people on this thread say that it's a bad idea for code to run slightly differently while under JUnit. I generally agree, but I think there are some exceptions.
For example, I am currently writing INTEGRATION (as opposed to Unit) tests for an app that connects to a DB.
These acceptance tests often need to completely reinitialize the DB with specific test data.
Obviously, I don't want this EVER, EVER to be done on an actual production DB, because that might completely erase valuable production data.
The easiest way to guarantee that this will never happen, is to make it impossible for the code to connect to a production DB when it is running under JUnit. This in turn can be done if, for example, the Factory that generates a connection can tell that it's running under JUnit, and in that case, will return a null connection unless the database we are trying to connect to has a name that is known to be a test database (ex: "testdatabase").