Java unit tests, directory layout

java, unit-testing

Solution

You can put the tests in the same package as the original classes, even if the source code is under its own directory root:

PROJECT_ROOT
    +--- src/
    +----test/

You can declare a class `com.foo.MyClass` under `src` and its test `com.foo.MyClassTest` under `test`.

As for access to private members, you can use reflection to invoke the methods (altering their accessibility via `Class.getDeclaredMethod.setAccessible`), or you could use something like testng/junit5 to put some annotation-driven tests on the source code itself (I personally think this is a bad idea).

Why not check out some projects on `java.net` to see how they've organized things, for example swinglabs (the SVN repository is pretty slow I'm afraid)?

Problem

When building a suite of unit tests for Java code, is there a convention for where to place test code in relation to source code? For example, if I have a directory `/java` that contains a bunch of `.java` source files, is it better to put the test cases in `/java` itself or use something like `/java/test`. If the latter is preferred, how do you test the internals of the code when the `private` /`protected` members of a class aren't available outside the package?

Original source