I’ve got a couple of data access objects (DefaultPersonServices.class, DefaultAddressServices.class) which is responsible for various CRUD operations in a database.
A few different classes use these services, but as the services requires that a connection is established with a database I can’t really use them in unit tests as they take too long. Thus, I’d like to create a test doubles for them and simply do FakePersonServices.class and FakeAddressService.class implementations which I can use throughout testing.
Now, this is all good (I assume)…but my question relates to where I put the test doubles. Should I keep them along with the default implementations (aka “real” implementations) or should I keep them in a corresponding test package.
The default implementations are found in Source Packages : com.company.data.services. Should I keep the test doubles here too, or should the test doubles rather be in Test Packages : com.company.data.services?
Unless they will actually be used by something other than tests, then they belong in the test package.
Possibly you might want them to go into a separate “test objects” package if you’re publishing your “source packages” and want other people to be able to write unit tests incorporating them, but unless you know this to be the case right now, I’d say YAGNI.
FWIW: There is only one instance in my past where I actually included fakes in the primary source code, and that was when the site was actually designed to be able to run in “demo mode” without an underlying database. As you can probably guess, these were true fakes, not just stubs or dummies, and the maintenance on them was considerable. Very few projects have any need for this, but I’m pointing it out anyway for the sake of completeness.
1