11

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
  • 549,808
  • 132
  • 1,049
  • 1,115
TheDelChop
  • 7,728
  • 3
  • 46
  • 68

4 Answers4

38

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,148
  • 2
  • 86
  • 88
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
  • 917
  • 2
  • 11
  • 17
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
  • 54,276
  • 13
  • 97
  • 116
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,362
  • 24
  • 30