This short tutorial will show you how to use Gradle together with Eclipse and JUnit. Gradle is a build-automation tool that is aimed at making it easy to share build environments between developers.
We will assume that you have already installed Eclipse. Do not install the Gradle Eclipse plugin, as you will not need it (but you might want to check it out later).
- Start by following the installation tutorial of Gradle, which lists a number of options for different platforms. If you are unsure which one to choose, you should probably go for the Install Manually option.
- After having installed Gradle, follow the tutorial on creating a minimal java application. The resulting application also contains a minimal JUnit 4 test suite, for which you will be able to get an html test report. When sharing a project, you may check in all the files except the
build
directory to your repo. The filesbuild.gradle
andsettings.gradle
will allow other developers to build the project usinggradle build
. You may or may not check in thegradle
directory and thegradlew
andgradlew.bat
files. These files and directories contain the Gradle Wrapper, which allows to run gradle without installing it. - Gradle also allows generating Eclipse project files with project’s configuration. To generate the project files, follow the tutorial and add the
eclipse
plugin to Gradle’s configuration file. You should not check in the Eclipse project files to your repo, as they are specific to your local environment, and can be generated by other developers usinggradle eclipse
. After following the tutorial, you should be able to run the JUnit 4 test suite from Eclipse and see its report in Eclipse. - Finally, to use any of the above configurations with JUnit 5, you need to make a few changes to your
build.gradle
file. First, add the following lines before theapply plugin
lines.
Then, add the following line to otherbuildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' } }
apply plugin
lines:
Finally, add the following lines to theapply plugin: 'org.junit.platform.gradle.plugin'
dependencies
section:
At the same time, you should remove the following line:testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.3' testCompile 'org.junit.jupiter:junit-jupiter-engine:5.0.3' testRuntime 'org.junit.platform:junit-platform-launcher:1.0.1' testRuntime 'org.junit.platform:junit-platform-runner:1.0.1'
If you want to be able to run JUnit 4 tests as well, add this line:testCompile 'junit:junit:4.12'
With these changes, you should be able to execute JUnit 5 test suites (of course you need to adapt your unit tests to match JUnit 5 requirements). Unfortunately, html test report does not seem to be supported in this configuration. On the other hand, viewing test results from Eclipse works.testCompile 'org.junit.vintage:junit-vintage-engine:4.12.3'