小编Avv*_*Avv的帖子

如何在 spring-boot web 客户端中发送请求正文?

我在 Spring Boot Web 客户端发送请求正文时遇到了一些问题。尝试发送如下正文:

val body = "{\n" +
            "\"email\":\"test@mail.com\",\n" +
            "\"id\":1\n" +
            "}"
val response = webClient.post()
    .uri( "test_uri" )
    .accept(MediaType.APPLICATION_JSON)
    .body(BodyInserters.fromObject(body))
    .exchange()
    .block()
Run Code Online (Sandbox Code Playgroud)

它不工作。请求正文应为 JSON 格式。 请让我知道我哪里做错了。

webclient kotlin spring-boot spring-webflux

10
推荐指数
2
解决办法
2万
查看次数

kotlin + Spring boot 中的单元测试休息控制器

编辑:我创建了另一个类“Utils”并将函数移动到该类。

class Utils {
fun isMaintenanceFileExist(maintenanceFile: String) : Boolean {
    /** method to check maintenance file, return True if found else False. */
    return File(maintenanceFile).exists()
}
}
Run Code Online (Sandbox Code Playgroud)

我正在测试 post API 并模拟如下方法:

@Test
fun testMaintenanceMode() {
    val mockUtil = Mockito.mock(Utils::class.java)
    Mockito.`when`(mockUtil.isMaintenanceFileExist("maintenanceFilePath"))
            .thenReturn(true)

    // Request body
    val body = "authId=123&email=a@mail.com&confirmationKey=86b498cb7a94a3555bc6ee1041a1c90a"

    // When maintenance mode is on
    mvc.perform(MockMvcRequestBuilders.post("/post")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
            .content(body))
            .andExpect(MockMvcResultMatchers.status().isBadRequest)
            .andReturn()
    }
Run Code Online (Sandbox Code Playgroud)

但我没有得到预期的结果。

控制器代码:

{
utilObj = Utils()
...
@PostMapping("/post")
fun registerByMail(@Valid data: RequestData) : ResponseEntity<Any>
{

    // check for maintenance mode, …
Run Code Online (Sandbox Code Playgroud)

junit unit-testing mockito kotlin spring-boot

4
推荐指数
1
解决办法
6723
查看次数

单元测试 - Wiremock 验证失败并出现连接错误

我正在测试一个 spring-boot 应用程序并使用 wiremock 存根来模拟外部 API。在一个测试用例中,我想确保我的存根只被调用一次,但由于连接错误而失败。

我的测试文件:

@SpringBootTest
@AutoConfigureWebTestClient
@ActiveProfiles("test")
class ControllerTest {

    @Autowired
    private lateinit var webClient: WebTestClient

    private lateinit var wireMockServer: WireMockServer

    @BeforeEach
    fun setup() {
        wireMockServer = WireMockServer(8081)
        wireMockServer.start()
        setupStub()
    }

    @AfterEach
    fun teardown() {
        wireMockServer.stop()
    }

    // Stub for external API
    private fun setupStub() {
        wireMockServer.stubFor(
        WireMock.delete(WireMock.urlEqualTo("/externalApiUrl"))
            .willReturn(
                WireMock.aResponse()
                    .withHeader("Content-Type", "application/json")
                    .withStatus(204)
                    .withBodyFile("file.json")
            )
    )
    }

    @Test
    fun test_1() {

        val email = "some-email"
        val Id = 123

        webClient.post()
        .uri { builder ->
            builder.path("/applicationUrl")
                .queryParam("email", email) …
Run Code Online (Sandbox Code Playgroud)

junit kotlin spring-boot wiremock

4
推荐指数
1
解决办法
3565
查看次数