Reputation: 723
I've been utilizing stub implementations of my abstract
classes for testing purposes. Currently, my unit tests are residing in a test
directory alongside my src
directory.
I'm wondering if there is a best-practice for where to place any stubs that are used in the testing. I have been trying several things, but part of me seems to be perpetually perturbed no matter what I do.
Things I have tried:
src
.test
package structure. This seems like the most reasonable option, but I am not sure what the best way to do this is. Bear in mind, I am probably over-thinking this. Is there any information on best-practices that could put my mind to rest? Is switching to a mock framework a solution to this dilemma?
Much appreciated.
Upvotes: 2
Views: 1917
Reputation: 67360
The 3rd option is definitely better than the other two for the reasons you've given. I like to refer to the Maven directory structure during times like this, and it doesn't seem to imply anything different than your 3rd option.
What I do is create the stub as an inner class inside the test itself. Once this gets too messy or needs to be reused, I put the stubs in the same package as the abstract class, but under the test
directory.
It might be worth knowing that a lot of projects evolve to have "Builder" objects that work like this:
Person person = new PersonBuilder().withName("John Doe").withDefaultAddress().build();
The reason you do this is because sometimes a Person
needs to be created lots of different ways depending on what you're testing. Having just one PersonStub
may not be enough.
Upvotes: 2