12

I'm getting back into Java after a long stint in the Ruby world and I've got a question about JUnit tests and the source I'm testing.

If I've got a package of graphics code for my company, lets call it com.example.graphics, should I include my tests in that package too or should they be included in a seperate package, like com.example.graphics.test?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
TheDelChop
  • 7,938
  • 4
  • 48
  • 70

4 Answers4

39

In the same java package is fine. It's actually necessary if you need to access package-private classes, methods, or fields. However, the source should be logically separate:

src/main/com/example/graphics
src/test/com/example/graphics
Brett Kail
  • 33,593
  • 2
  • 85
  • 90
1

If you do not need to access private classes it is actually a question of flavor.

I sometimes even tend to create an additional test-project referenced to the product-project. Therefore, product and test are clearly seperated.

Philipp Andre
  • 997
  • 3
  • 11
  • 18
1

I personally keep all my tests in the same package, but in the test tree of Maven(which I always use for Java projects). Consider using Maven for your builds as well - it saves a lot of work on your part. It enforces a similar structure to what bkail mentions, but gives you much more than a standardized project folder layout - project lifecycle(clean, compile, package, test), plugins, etc .

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
0

Putting tests in a separate package does tend to avoid the problem of accidentally having some method or class at package-scope that you intended clients to use.

soru
  • 5,464
  • 26
  • 30