Question: I have a file - in a large legacy codebase - containing methods that access databases.
No classes are used, just a header file with the method declarations, and the source file with the implementation.

I want to override these methods to eliminate the DB access during unit testing.  

I thought of the following options:  

1. Make file into class and override methods.  

The main minus here is that it results in alot of changes throughout the codebase.  

Not ideal, though it does improve the code...
2. Wrap the whole source file with an `#ifdef PRODUCTION_CODE` and create a new source file containing the stub and wrap it with the opposite, i.e. make the whole thing compilation dependent. Problem here is that in a build system that performs regression tests I would have to compile twice, once in order to create the apps and do regression tests, and an additional time to create the unit test executables.

Any recommended ways of doing this?

Answer: 
How about taking the existing functions, move the code inside into a new class and call the new methods from the existing functions and then override this class during tests?

Like so:

```
 static DBAccessClass dac = new DBAccessClass ();

 void origFunction() { dac.origFunction(); }

```

And in the test:

```
 dac = new DBAccessMockup();

```
> 
> 1. Make file into class and override methods. [...]
> 2. Wrap the whole source file with an #ifdef [...]
> 
> 
> 

If possible, go with 1, as you will have a centralized way of referring to the database code (a class pointer instead of X functions). That means modularity, ease in replacing the implementation (with stubs or with another DB back-end) and more encapsulated code.

If you go with 2, consider replacing *the implementation* of the functions. That is, inside the original functions, engage the testing code (based on an `if`).

Your tested code is completely agnostic if running in a test environment or not, the performance loss is negligible (`if(booleanFlagHere)` is a negligible cost in most cases) and you don't have to modify the tested code at all).