【问题标题】:MockMvc GET request failed with 404 but the URL is validMockMvc GET 请求失败,出现 404 但 URL 有效
【发布时间】:2022-01-05 02:01:59
【问题描述】:

我正在尝试使用 MockMvc 和 springmockk 库测试我的控制器,当我请求有效 URL 时,我收到 404 错误。 这是BadgeControllerImplTest

package uno.d1s.pulseq.controller

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get
import uno.d1s.pulseq.controller.impl.BadgeControllerImpl
import uno.d1s.pulseq.core.constant.mapping.BadgeMappingConstants
import uno.d1s.pulseq.service.BadgeService

@WebMvcTest(useDefaultFilters = false, controllers = [BadgeControllerImpl::class])
class BadgeControllerImplTest {

    @Autowired
    private lateinit var mockMvc: MockMvc

    @MockkBean
    private lateinit var badgeService: BadgeService

    @BeforeEach
    fun setup() {
        every {
            badgeService.createBadge(any(), any(), any(), any(), any())
        }.returns(byteArrayOf())
    }

    @Test
    fun `should return the badge on getBadge`() {
        mockMvc.get(BadgeMappingConstants.GET_BADGE.replace("{statisticId}", "total-beats")) {
            param("color", "red")
            param("title", "Redefined title")
        }.andExpect {
            status {
                isOk()
            }
        }
    }
}

这是我要测试的代码:

package uno.d1s.pulseq.controller

import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import uno.d1s.pulseq.core.constant.mapping.BadgeMappingConstants
import javax.servlet.http.HttpServletResponse
import javax.validation.constraints.NotEmpty

interface BadgeController {

    @RequestMapping(
        BadgeMappingConstants.GET_BADGE,
        method = [RequestMethod.GET]
    )
    fun getBadge(
        @PathVariable statisticId: String,
        @RequestParam(required = false) color: String?,
        @RequestParam(required = false) title: String?,
        @RequestParam(required = false) style: String?,
        @RequestParam(required = false) logoUrl: String?,
        response: HttpServletResponse
    )
}

这个接口的实现是:

package uno.d1s.pulseq.controller.impl

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.RestController
import uno.d1s.pulseq.controller.BadgeController
import uno.d1s.pulseq.service.BadgeService
import javax.servlet.http.HttpServletResponse
import javax.validation.constraints.NotEmpty

@Validated
@RestController
class BadgeControllerImpl : BadgeController {

    @Autowired
    private lateinit var badgeService: BadgeService

    override fun getBadge(
        @NotEmpty statisticId: String,
        color: String?,
        title: String?,
        style: String?,
        logoUrl: String?,
        response: HttpServletResponse
    ) {
        response.run {
            contentType = "image/svg+xml"

            val badge = badgeService.createBadge(statisticId, color, title, style, logoUrl)
            writer.println(String(badge))
        }
    }
}

控制器映射到/api/badge/{statisticId}
谢谢。

【问题讨论】:

    标签: spring spring-boot kotlin spring-boot-test


    【解决方案1】:

    我通过用@ContextConfiguration(classes = [BadgeControllerImpl::class]) 标记BadgeControllerImplTest 解决了这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2021-04-10
      • 2023-03-22
      • 2021-03-10
      • 2020-03-07
      • 2021-09-05
      • 1970-01-01
      相关资源
      最近更新 更多