d1snin
d1snin

Reputation: 180

MockMvc GET request failed with 404 but the URL is valid

I am trying to test my controller using MockMvc and the springmockk library, when I make a request for a valid URL, I get a 404 error. Here is the 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()
            }
        }
    }
}

This is the code I'm trying to test:

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
    )
}

The implementation of this interface is:

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))
        }
    }
}

The controller maps at /api/badge/{statisticId}
Thank you.

Upvotes: 0

Views: 656

Answers (1)

d1snin
d1snin

Reputation: 180

I solved this problem by marking BadgeControllerImplTest with @ContextConfiguration(classes = [BadgeControllerImpl::class])

Upvotes: 1

Related Questions