views:

125

answers:

4

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?

+22  A: 

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
bkail
+1 exactly what i would say.
oedo
+1, as suggested by the Maven Java Archetype standard directory layout
Ramiro Gonzalez Maciel
Cool, much thanks man.
TheDelChop
+1  A: 

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
A: 

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
A: 

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