在 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
- 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.
2. Open the
build.gradle(.kts)file and add the following dependency to the Gradle configuration. This dependency will allow you to work withkotlin.testandJUnit:【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. }
- Add the
testtask to thebuild.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.
Add the code to test it
- Open the
main.ktfile insrc/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() {
- fun sum(a: Int, b: Int): Int {
- return a + b
- }
- } ```
Create a test
- In IntelliJ IDEA, select Code | Generate | Test… for the
Sampleclass.
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
*.ktfile for tests insrc/test/kotlin.
3. Add the test code for the
sum()function inSampleTest.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
-
internal class SampleTest {
-
private val testSample: Sample = Sample()
-
@Test
- fun testSum() {
- val expected = 42
- assertEquals(expected, testSample.sum(40, 2))
- }
- } ```
Run a test
- Run the test using the gutter icon.

You can also run all project tests via the command-line interface using the
./gradlew checkcommand.
2. Check the result in the Run tool window:

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:

The test execution failed.
下一步做什么
Once you’ve finished your first test, you can:
- Try to write another test using other kotlin.test functions. For example, you could use the assertNotEquals() function.
- Create your first application with Kotlin and Spring Boot.
- Watch these video tutorials on YouTube, which demonstrate how to use Spring Boot with Kotlin and JUnit 5.
