开发者

Include Unit tests in the same package as the source code in Java

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 sepera开发者_C百科te package, like com.example.graphics.test?


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


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.


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 .


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜