MockMvc GET请求失败,返回404,但URL有效



我正试图使用MockMvc和springmmock库测试我的控制器,当我请求一个有效的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}
谢谢。

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

最新更新