基于Spring的应用程序-迁移到Junit 5

这是有关将基于Gradle的Spring Boot应用程序从Junit 4迁移到闪亮的新Junit 5的快速文章。Junit 4测试继续与Junit 5 Test Engine抽象一起工作,该抽象为在不同编程模型中编写的测试提供支持。例如,Junit 5支持能够运行JUnit 4测试的Vintage Test Engine。

这是一个具有JUnit 5集成的示例项目 ,以及Junit 4和Junit 5中的示例测试– https://github.com/bijukunjummen/boot2-with-junit5-sample

样本Junit 4候选测试

作为一个候选项目,我有一个Spring Boot 2应用程序,该应用程序使用Junit 4作为测试框架,使用Kotlin编写了测试。 这是在显式调出所有依赖项的情况下样本测试的外观。 它使用Junit4的@RunWith注释加载Spring Context:

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.reactive.server.WebTestClient
import java.nio.charset.StandardCharsets

@RunWith(SpringRunner::class)
@WebFluxTest(controllers = arrayOf(RouteConfig::class))
class SampleJunit4Test {

    @Autowired
    lateinit var webTestClient: WebTestClient

    @Test
    fun `get of hello URI should return Hello World!`() {
        webTestClient.get()
                .uri("/hello")
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .consumeWith({ m ->
                    assertThat(String(m.responseBodyContent, StandardCharsets.UTF_8)).isEqualTo("Hello World!")
                })

    }

}

Junit 4依赖项通过“ spring-boot-starter-test”模块可传递地引入:

testCompile('org.springframework.boot:spring-boot-starter-test')

Junit 5迁移

要做的第一步是引入Junit 5依赖项以及Gradle插件,该插件可以运行测试:

插入:

buildscript {
 dependencies {
  ....
  classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
 }
}
apply plugin: 'org.junit.platform.gradle.plugin'

依存关系:

testCompile("org.junit.jupiter:junit-jupiter-api")
testRuntime("org.junit.jupiter:junit-jupiter-engine")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.2")

完成这些更改后,所有Junit 4测试将继续在IDE中运行,并在执行Gradle构建时运行,此时,测试本身可以缓慢迁移。

我之前显示的测试与Junit 5 Jupiter相似,它提供了测试的编程模型:

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.reactive.server.WebTestClient
import java.nio.charset.StandardCharsets

@ExtendWith(SpringExtension::class)
@WebFluxTest(controllers = arrayOf(RouteConfig::class))
class SampleJunit5Test {

    @Autowired
    lateinit var webTestClient: WebTestClient

    @Test
    fun `get of hello URI should return Hello World!`() {
        webTestClient.get()
                .uri("/hello")
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .consumeWith({ m ->
                    assertEquals("Hello World!", String(m.responseBodyContent, StandardCharsets.UTF_8))
                })
    }

}

需要注意的是现在,而不是使用JUnit 4 @RunWith注释,我现在用的是@ExtendWith标注,并提供SpringExtension作为一个参数,它负责加载了Spring上下文像以前一样。 Spring其余注解将继续与JUnit 5一起使用。通过这种方式,可以将测试从JUnit 4缓慢移至JUnit 5。

注意事项

但是,并非一切都十分顺利,从JUnit 4迁移到JUnit 5时会遇到一些问题,其中最大的问题可能是对JUnit @Rule和@ClassRule批注的支持,并且JUnit 5文档确实详细介绍了如何实现。减轻了

翻译自: https://www.javacodegeeks.com/2018/01/spring-based-application-migrating-junit-5.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值