# 47.6 测试

虽然可以使用JUnit 4（`spring-boot-starter-test`提供的默认版本）测试Kotlin代码，但是推荐JUnit 5。JUnit 5使一个测试类可以被实例化，然后被这个类的所有测试重复使用。这使它可以在非静态方法上使用`@BeforeAll`和`@AfterAll`注解，非常适合Kotlin。

使用JUnit 5，需要从`spring-boot-starter-test`里排除`junit:junit`依赖，添加JUnit 5依赖，并且相应地配置Maven或者Gradle插件。更多详情请查看[JUnit 5文档](https://junit.org/junit5/docs/current/user-guide/#dependency-metadata-junit-jupiter-samples)。你也需要[将测试实例的生命周期切换到“每个类”](https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle-changing-default)。
