在 JVM 平台中用 JUnit 测试代码——教程

This tutorial will show 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 library under the hood and runs the test using JUnit.

To get started, first download and install the latest version of IntelliJ IDEA.

Add dependencies

  1. Open a Kotlin project in IntelliJ IDEA. If you don’t already have a project, create one.

    Specify JUnit 5 as your test framework when creating your project.

    在 JVM 平台中用 JUnit 测试代码——教程 - 图1

  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:

【Kotlin】

  1. dependencies {
  2. // Other dependencies.
  3. testImplementation(kotlin("test"))
  4. }

【Groovy】

  1. dependencies {
  2. // Other dependencies.
  3. testImplementation 'org.jetbrains.kotlin:kotlin-test'
  4. }
  1. Add the test task to the build.gradle(.kts) file:

【Kotlin】

  1. tasks.test {
  2. useJUnitPlatform()
  3. }

【Groovy】

  1. test {
  2. useJUnitPlatform()
  3. }

If you created the project using the New Project wizard, the task will be added automatically.

在 JVM 平台中用 JUnit 测试代码——教程 - 图2

Add the code to test it

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

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

    1. class Sample() {
    2. fun sum(a: Int, b: Int): Int {
    3. return a + b
    4. }
    5. }

Create a test

  1. In IntelliJ IDEA, select Code | Generate | Test… for the Sample class.

    Create a test

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

    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.

    在 JVM 平台中用 JUnit 测试代码——教程 - 图4

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

    • Define the test testSum() function using the @Test annotation.
    • Check that the sum() function returns the expected value by using the assertEquals() function.
    1. import kotlin.test.Test
    2. import kotlin.test.assertEquals
    3. internal class SampleTest {
    4. private val testSample: Sample = Sample()
    5. @Test
    6. fun testSum() {
    7. val expected = 42
    8. assertEquals(expected, testSample.sum(40, 2))
    9. }
    10. }

Run a test

  1. Run the test using the gutter icon.

    Run the test

    You can also run all project tests via the command-line interface using the ./gradlew check command.

    在 JVM 平台中用 JUnit 测试代码——教程 - 图6

  2. Check the result in the Run tool window:

    Check the test result. The test passed successfully

    The test function was executed successfully.

  3. Make sure that the test works correctly by changing the expected variable value to 43:

    1. @Test
    2. fun testSum() {
    3. val expected = 43
    4. assertEquals(expected, classForTesting.sum(40, 2))
    5. }
  4. Run the test again and check the result:

    Check the test result. The test has been failed

    The test execution failed.

下一步做什么

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