Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: update the Test code using JUnit in JVM tutorial #4158

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file modified docs/images/tutorials/jvm-test/create-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/tutorials/jvm-test/run-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 59 additions & 27 deletions docs/topics/jvm/jvm-test-using-junit.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
[//]: # (title: Test code using JUnit in JVM – tutorial)

This tutorial will show you how to write a simple unit test and run it with the Gradle build tool.
This tutorial shows you how to write a simple unit test and run it with the Gradle build tool.

The example in the tutorial has the [kotlin.test](https://kotlinlang.org/api/latest/kotlin.test/index.html) library under the hood and runs the test using JUnit.
The example in the tutorial has the [`kotlin.test`](https://kotlinlang.org/api/latest/kotlin.test/index.html) library under the hood and runs the test using JUnit.

To get started, first download and install the latest version of [IntelliJ IDEA](https://www.jetbrains.com/idea/download/index.html).

## Add dependencies

1. Open a Kotlin project in IntelliJ IDEA. If you don't already have a project, [create one](jvm-get-started.md#create-an-application).
1. Open a Kotlin project in IntelliJ IDEA. If you don't have a project,
[create one](https://www.jetbrains.com/help/idea/create-your-first-kotlin-app.html#create-project).

> Specify **JUnit 5** as your test framework when creating your project.
>
{type="note"}

2. Open the `build.gradle(.kts)` file and add the following dependency to the Gradle configuration. This dependency will allow you to work with `kotlin.test` and `JUnit`:
2. Open the `build.gradle(.kts)` file and check that the `testImplementation` dependency is present.
This dependency allows you to work with `kotlin.test` and `JUnit`:

<tabs group="build-script">
<tab title="Kotlin" group-key="kotlin">
Expand Down Expand Up @@ -62,15 +60,43 @@ To get started, first download and install the latest version of [IntelliJ IDEA]
</tab>
</tabs>

> If you created the project using the **New Project** wizard, the task will be added automatically.
>
> If you use the `useJUnitPlatform()`function in your build script,
> the `kotlin-test` library automatically includes JUnit 5 as a dependency.
> This setup enables access to all JUnit 5 APIs, along with the `kotlin-test` API,
> in JVM-only projects and JVM tests of Kotlin Multiplatform (KMP) projects.
>
{type="note"}

Here's a complete code for the `build.gradle.kts`:

```kotlin
plugins {
kotlin("jvm") version "%kotlinVersion%"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

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

tasks.test {
useJUnitPlatform()
}
```
{initial-collapse-state="collapsed"}

## Add the code to test it

1. Open the `main.kt` file in `src/main/kotlin`.
1. Open the `Main.kt` file in `src/main/kotlin`.

The `src` directory contains Kotlin source files and resources. The `main.kt` file contains sample code that will print `Hello, World!`.
The `src` directory contains Kotlin source files and resources.
The `Main.kt` file contains sample code that prints `Hello, World!`.

2. Create the `Sample` class with the `sum()` function that adds two integers together:

Expand All @@ -85,28 +111,32 @@ To get started, first download and install the latest version of [IntelliJ IDEA]

## Create a test

1. In IntelliJ IDEA, select **Code** | **Generate** | **Test...** for the `Sample` class.
1. In IntelliJ IDEA, select **Code** | **Generate** | **Test...** for the `Sample` class:

![Create a test](create-test.png)
![Create a test](generate-test.png)

2. Specify the name of the test class. For example, `SampleTest`:

2. Specify the name of the test class. For example, `SampleTest`.
![Create a test](create-test.png)

IntelliJ IDEA creates the `SampleTest.kt` file in the `test` directory. This directory contains Kotlin test source files and resources.
IntelliJ IDEA creates the `SampleTest.kt` file in the `test` directory.
This directory contains Kotlin test source files and resources.

> You can also manually create a `*.kt` file for tests in `src/test/kotlin`.
>
{type="note"}

2. Add the test code for the `sum()` function in `SampleTest.kt`:
3. Add the test code for the `sum()` function in `SampleTest.kt`:

* Define the test `testSum()` function using the [@Test annotation](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/-test/index.html).
* Check that the `sum()` function returns the expected value by using the [assertEquals()](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-equals.html) function.
* Define the test `testSum()` function using the [`@Test` annotation](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/-test/index.html).
* Check that the `sum()` function returns the expected value by using the [`assertEquals()`](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-equals.html) function.

```kotlin
import org.example.Sample
import org.junit.jupiter.api.Assertions.*
import kotlin.test.Test
import kotlin.test.assertEquals

internal class SampleTest {
class SampleTest {

private val testSample: Sample = Sample()

Expand All @@ -120,7 +150,7 @@ To get started, first download and install the latest version of [IntelliJ IDEA]

## Run a test

1. Run the test using the gutter icon.
1. Run the test using the gutter icon:

![Run the test](run-test.png)

Expand All @@ -130,7 +160,7 @@ To get started, first download and install the latest version of [IntelliJ IDEA]

2. Check the result in the **Run** tool window:

![Check the test result. The test passed successfully](check-the-result.png)
![Check the test result. The test passed successfully](test-successful.png)

The test function was executed successfully.

Expand All @@ -146,14 +176,16 @@ To get started, first download and install the latest version of [IntelliJ IDEA]

4. Run the test again and check the result:

![Check the test result. The test has been failed](check-the-result-2.png)
![Check the test result. The test has failed](test-failed.png)

The test execution failed.

## What's next

Once you've finished your first test, you can:

* Try to write another test using other [kotlin.test](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/) functions. For example, you could use the [`assertNotEquals()`](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-not-equals.html) function.
* [Create your first application](jvm-get-started-spring-boot.md) with Kotlin and Spring Boot.
* Watch [these video tutorials](https://www.youtube.com/playlist?list=PL6gx4Cwl9DGDPsneZWaOFg0H2wsundyGr) on YouTube, which demonstrate how to use Spring Boot with Kotlin and JUnit 5.
* Write more tests using other [`kotlin.test`](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/) functions.
For example, use the [`assertNotEquals()`](https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-not-equals.html) function.
* Improve your test output with the [Kotlin Power-assert compiler plugin](power-assert.md).
The plugin enriches the test output with contextual information.
* [Create your first server-side application](jvm-get-started-spring-boot.md) with Kotlin and Spring Boot.
Loading