Kotlin/Gradle/JUnit starter project

1. Create a project

  1. Download and install IntelliJ Community, free for Java/Kotlin projects. Alternatively, you can use IntelliJ Ultimate EAP (if available, no subscription is required).
  2. Go to “File” ⇀ “New project”. In the wizard:
    ⦿ Write a Name and pick a Location;
    ⦿ Activate “Create Git repository”;
    ⦿ Language: “Kotlin”;
    ⦿ Build system: “Gradle”;
    ⦿ JDK: Pick any JDK. If you’re unsure which to pick, go to “Download JDK” (in the dropdown) and pick the latest “Oracle OpenJDK”. If you get a warning, decrease one version because Gradle may not support it yet.
    ⦿ Gradle DSL: Pick “Kotlin” as Gradle DSL. Since we’re using Kotlin, it makes sense that the Gradle DSL is Kotlin.
  3. Press “Create”.

2. Setup Git

To ensure you don’t lose your work and can always go back in time, ensure you set up Git — even if only locally.

  1. If you haven’t picked “Create Git repository” before, start a new git repository (git init).
  2. Create a .gitignore file with the following content, and commit all the changes:
/.gradle
/.idea
/build

3. Update Gradle/Kotlin

The IDE wizard doesn’t apply the latest versions of Gradle and Kotlin, so let’s do it manually.

  1. Check the latest version of Gradle (e.g. 7.4.2) and update the file gradle/wrapper/gradle-wrapper.properties accordingly.
  2. Check the latest version of Kotlin (e.g. 1.7.10) and update the file build.gradle.kts accordingly.
plugins {
kotlin("jvm") version "1.7.10"
}

3. You may need to click on the second icon to force the Gradle update:

4. Set up JUnit

JUnit gets automatically set up for you. If it doesn’t, make sure you have this at build.gradle.kts:

dependencies {
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}
  1. Create a dummy test (src/test/kotlin/DummyTest.kt):
import kotlin.test.Test
import kotlin.test.assertTrue

class TestAmazonCart {
@Test
fun `everything is fine`() {
assertTrue(true)
}
}

Notice that we’re using the imports from Kotlin Test which abstract JUnit but are basically the same.

2. Run it (there’s a ▶️ near the test) just to prove that the JUnit setup is good:

You can also run the tests with ./gradlew test

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luís Soares

Luís Soares

I write about Lean, TDD, CI/CD, trunk-based dev., user-centric dev, DDD, coding good practices, testing.