Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Environment Variables for All JUnit Tests in Eclipse

I am trying to set up some unit tests, which use the database. I would like to use a test database on the developer's computer instead of on the production database. The method I have in place now is to check an environment variable when connecting to the database, and if that variable exists to connect to the local one instead of the production one.

I can set environment variables in Eclipse through the Run Configurations, but I want this to happen whenever a JUnit test is run (just in case).

Is this possible in Eclipse?

like image 929
KLee1 Avatar asked Aug 12 '10 22:08

KLee1


1 Answers

Why don't you inject the database connection into the logic which needs testing, and only deal with environment variables in your startup code (which generally doesn't need unit testing)?

Using environment variables in tests is generally a sign that configuration is happening too deeply inside your code, IME. Where possible, I find it much better to construct objects with everything they need via dependency injection, keeping that code separate from the configuration code which works out what to inject.

like image 79
Jon Skeet Avatar answered Oct 04 '22 02:10

Jon Skeet