views:

191

answers:

1

Hi all!

Please, tell me:

How can I write unit test (e.g. with JUnit) for operation "insert into some table" when many properties for normal application work are saved in config files for application server?

Thanks!a

A: 

You can either:

  • Change your class to require its configuration settings (like the data source) in its constructor. Make callers read the configuration settings and dictate them to your class. When you do this, your unit tests can specify the settings when they instantiate test instances.

Or

  • Use a mock object instead of the normal mechanism to obtain configuration settings.

The second option won't be possible if you use a shared singleton ConfigurationSettings (or something similar) to get your configuration data. If that's the case, use the first method - which is generally better anyway. (As your question demonstrates, different callers use different configuration techniques.)

Jeff Sternal