In the Java Build Path project settings, there is now an attribute Contains test sources to configure that a source folder contains test sources.
(Note: test sources must have their own output folder).
Similarly, for projects and libraries there is an attribute Visible only for test sources.
This setting also exists for classpath containers, and if it is set to Yes for one of these, this value will be used for all contained libraries and projects.
Test source folders and dependencies are shown with a darker icon in the build path settings, the package explorer and other locations.
This can be disabled in Preferences > Java > Appearance:
Referenced projects can contain test sources and have test dependencies themselves.
Usually, when test sources are compiled, the test code in projects on the build path will be visible.
As this is not always desirable, it can be changed by setting the new build path attribute Without test code, that is available for projects, to Yes.
Build path entries configured like this have a decoration [without test code] after the project name, which can be disabled in Preferences > General > Appearance > Label Decorations:
For each project, compilation is now done in two phases: First all main sources (which cannot see any test-code on the build-path) and then all test sources.
As a consequence, if the project is a modular Java 9 project, test dependencies like JUnit can not be referenced in the
, as they will not be visible while compiling it.
The solution used to handle this is the same, that Maven uses: When test dependencies are put on the classpath, the module being compiled will automatically be configured to read the unnamed module during the compilation of the test sources, so the test dependencies will be visible.
Of course, code completion will not suggest test code in main sources:
There are now two dynamic Java working sets Java Main Sources and Java Test Sources containing the source folders grouped according to value of the Contains test sources attribute.
This can for example be used to remove warnings in test sources from the problems view:
To achieve this, create a new filter that shows warnings for the Java Main Sources working set and select it with the All Errors on Workspace filter:
There are also dedicated filters to quickly remove hits in main code or test code from Java search results:
Similar, there is a filter to remove test code from Call hierarchies:
Another filter to remove test code exists for Quick type hierarchies:
Test source folders will be preselected in the New JUnit Test Case wizard
In Run and Debug configurations, the Classpath tab (or Dependencies tab when launching with Java 9) contains a new option Exclude Test Code, that is automatically preselected when launching a Java Application from a source folder that is not marked to contain test sources:
When launching with Java 9 and this option is not selected, command line options will automatically be added so modules that have a non-empty classpath read the unnamed module.
These command line options are part of what can be overridden using the new Override Dependencies button.