Skip to content

Instantly share code, notes, and snippets.

@michalpalka
Last active June 3, 2018 21:53
Show Gist options
  • Save michalpalka/b19eaf655c44501ba3867e863a49ea8f to your computer and use it in GitHub Desktop.
Save michalpalka/b19eaf655c44501ba3867e863a49ea8f to your computer and use it in GitHub Desktop.
Using Gradle with Eclipse and JUnit for DIT085

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).

  1. 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.
  2. 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 files build.gradle and settings.gradle will allow other developers to build the project using gradle build. You may or may not check in the gradle directory and the gradlew and gradlew.bat files. These files and directories contain the Gradle Wrapper, which allows to run gradle without installing it.
  3. 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 using gradle eclipse. After following the tutorial, you should be able to run the JUnit 4 test suite from Eclipse and see its report in Eclipse.
  4. 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 the apply plugin lines.
      buildscript {
          repositories {
              mavenCentral()
          }
          dependencies {
              classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
          }
      }
    
    Then, add the following line to other apply plugin lines:
       apply plugin: 'org.junit.platform.gradle.plugin'
    
    Finally, add the following lines to the dependencies section:
       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'
    
    At the same time, you should remove the following line:
       testCompile 'junit:junit:4.12'
    
    If you want to be able to run JUnit 4 tests as well, add this line:
       testCompile 'org.junit.vintage:junit-vintage-engine:4.12.3'
    
    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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment