带注释的控制器– Spring Web / Webflux和测试

Spring WebfluxSpring Web是两个完全不同的Web堆栈。 但是, Spring Webflux继续支持基于注释的编程模型

使用这两个堆栈定义的端点可能看起来很相似,但是测试该端点的方式却大不相同,并且编写此类端点的用户必须知道哪个堆栈处于活动状态并相应地制定测试。

样本端点

考虑一个基于示例注释的端点:

import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


data class Greeting(val message: String)

@RestController
@RequestMapping("/web")
class GreetingController {
    
    @PostMapping("/greet")
    fun handleGreeting(@RequestBody greeting: Greeting): Greeting {
        return Greeting("Thanks: ${greeting.message}")
    }
    
}

使用Spring Web进行测试

如果使用Spring Boot 2启动程序以Spring Web作为启动程序来创建此应用程序,请通过以下方式使用Gradle构建文件进行指定:

compile('org.springframework.boot:spring-boot-starter-web')

那么将使用Mock Web运行时(称为Mock MVC)对这种端点进行测试:

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content


@RunWith(SpringRunner::class)
@WebMvcTest(GreetingController::class)
class GreetingControllerMockMvcTest {

    @Autowired
    lateinit var mockMvc: MockMvc

    @Test
    fun testHandleGreetings() {
        mockMvc
                .perform(
                        post("/web/greet")
                                .content(""" 
                                |{
                                |"message": "Hello Web"
                                |}
                            """.trimMargin())
                ).andExpect(content().json("""
                    |{
                    |"message": "Thanks: Hello Web"
                    |}
                """.trimMargin()))
    }
}

使用Spring Web-Flux进行测试

另一方面,如果引入了Spring-Webflux入门者,请使用以下Gradle依赖项进行说明:

compile('org.springframework.boot:spring-boot-starter-webflux')

那么此端点的测试将使用出色的WebTestClient类,如下所示:

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.http.HttpHeaders
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.web.reactive.function.BodyInserters


@RunWith(SpringRunner::class)
@WebFluxTest(GreetingController::class)
class GreetingControllerTest {

    @Autowired
    lateinit var webTestClient: WebTestClient

    @Test
    fun testHandleGreetings() {
        webTestClient.post()
                .uri("/web/greet")
                .header(HttpHeaders.CONTENT_TYPE, "application/json")
                .body(BodyInserters
                        .fromObject(""" 
                                |{
                                |   "message": "Hello Web"
                                |}
                            """.trimMargin()))
                .exchange()
                .expectStatus().isOk
                .expectBody()
                .json("""
                    |{
                    |   "message": "Thanks: Hello Web"
                    |}
                """.trimMargin())
    }
}

结论

可以很容易地假设,由于使用Spring Web和Spring Webflux堆栈的编程模型看起来非常相似,因此使用Spring Web进行的这种遗留测试的测试将继续到Spring Webflux,但是事实并非如此,作为我们的开发人员注意所使用的基础堆栈并相应地制定测试。 我希望这篇文章阐明应该如何设计这样的测试。

翻译自: https://www.javacodegeeks.com/2017/12/annotated-controllers-spring-web-webflux-testing.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值