Skip to content

Showcase of simple tests in Kotlin using JUnit 5, Spek 2 and using Kotlin Script Gradle

License

Notifications You must be signed in to change notification settings

PHPirates/kotlin-template-project

Repository files navigation

. .
GitHub Actions GitHub Workflow Status
Travis Build Status
Coveralls Coverage Status
Codecov codecov

Template project which contains minimal examples of using Gradle

Note that both a Gradle (build.gradle) and a Gradle Kotlin DSL (build.gradle.kts) build file are present.

This project aims to provide an example of the use of the Gradle Kotlin DSL with some testing frameworks and libraries, since the official documentation is at the moment of writing very minimal. There are both a Groovy Gradle (build.gradle, not updated recently) and a Kotlin Script Gradle (build.gradle.kts updated recently) build file present which do roughly the same, which may make translation a bit easier. For other translation examples, see below.

This is the Kotlin equivalent of this (older) java template project.

It makes use of

  • Kotlin
  • Kotlin Script Gradle or Gradle Kotlin DSL
  • A normal, Groovy Gradle build file is also present
  • JaCoCo for generating test reports
  • Codecov.io which takes the JaCoCo report and shows some statistics
  • Coveralls.io coverage reports uploaded by the coveralls-jacoco plugin (https://github.com/nbaztec/coveralls-jacoco-gradle-plugin)

There are tests using

  • kotlintest (will be run by Gradle since kotlintest 3)
  • JUnit 5
  • Spek 2

How to add Gradle to your existing project

Decide whether you want to use Groovy Gradle (older and widely used) or the Gradle Kotlin DSL (newer and starting to be used more and more).

  • Copy the build.gradle.kts and settings.gradle.kts (Gradle Kotlin DSL, as you can see from the .kts extension) or the build.gradle (Groovy Gradle, it this repo it may be named build.gradle.inactive but just rename it to build.gradle)
  • Read through the file you just copied and change the relevant names, remove things of which you are sure you don't need them
  • Copy the gradlew, gradlew.bat and gradle/ files and folder.
  • Go to File | New | Module from Existing Sources and select your build.gradle file. Make sure 'Use gradle wrapper' is selected. Select a different Gradle JVM if prompted.
  • You should commit all the files you added, but not the generated files in .gradle/.
  • You can run Gradle tasks from the Gradle window on the right, for example build to build your project, run to run the main class if you have one or check to run tests.
  • It is recommended that you use a continuous integration service like Travis, which will build and run tests on their server when you push a commit. Install the Travis GitHub App by going to the GitHub marketplace, and after that copy the .travis.yml, possibly remove parts you don't need.

Tips

This was only tested with IntelliJ.

  • You can quickly import the project into IntelliJ by importing the build.gradle.kts file. Select the Gradle wrapper if needed.

  • To run Gradle tasks, e.g. to run all tests (the verification/check task) or to start the main class (application/run) open the Gradle toolbar on the right and double-click (or right-click and debug) the task.

  • Alternatively, without IntelliJ, you can run gradlew test on the terminal to run the tests via gradle with the gradle wrapper (so you don't have to first install gradle locally by hand).

  • If you see all gradle keywords greyed out you may want to link the Gradle project in IntelliJ, if you do not see a popup asking you to do so then go to modules - import module and select your build.gradle.kts.

Feel free to use GitHub emojis as for example by François Parmentier.

Task configuration

Groovy Gradle:

// This is a configuration for the shadowJar task.
shadowJar{
  classifier = 'deploy'
}

Kotlin Script Gradle:

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

tasks {
    // This is a configuration for the shadowJar task.
    "shadowJar"(ShadowJar::class) {
        classifier = "deploy"
    }
}

Corresponding SO questions

Run JUnit 5 with Gradle

Run kotlintest and JUnit 5 tests with Gradle

Upload test reports of Kotlin sources to Coveralls

The DefaultSourceDirectorySet constructor has been deprecated. How to use the ObjectFactory service?

About

Showcase of simple tests in Kotlin using JUnit 5, Spek 2 and using Kotlin Script Gradle

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages