views:

27

answers:

1

One of the JUnit best practices is : same package, separate directories. I am wondering what is the equivalent for Mock classes ? Do you keep them in the same package as classes they are supposed to mock, but in the test directory ? or elsewhere ?

+1  A: 

Like many things in programming, "it depends." Here are some rules of thumb I use:

  1. If I have a stub that is only used by one test and is small - create an inner class
  2. If I have a stub that is only used by one test and is large - put in same package/folder as test
  3. If I have a stub that is used by multiple tests in the same package - put in same package/folder as test
  4. If I have a stub that is used in many places in the same application - put in a test.util package
  5. If I have a stub that is used across applications, put it in a jar.

I have instances of all of these in my code.

Jeanne Boyarsky