https://kotlinlang.org logo
Title
a

Axel

05/08/2023, 2:18 PM
I have this test:
@ExtendWith(SpringExtension::class)
@WebMvcTest(GetStoresByMarket::class)
internal class GetStoresByMarketTest {
    @Autowired
    private lateinit var mockMvc: MockMvc

    @MockBean
    private lateinit var getStoresByMarketAndConsumerId: GetStoresByMarketAndConsumerId
    @MockBean
    private lateinit var getStoresByMarketAndCategoryAndConsumerId: GetStoresByMarketAndCategoryAndConsumerId

    @Test
    fun `when market is unknown a client error should be returned`() {
        mockMvc.get("/v1/stores/asd") {
            param("consumerId", UUID.randomUUID().toString())
        }.andExpect {
            status { isBadRequest() }
        }
    }
}
the endpoint it tests is
suspend
, so the test fails with this output:
MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /v1/stores/asd
       Parameters = {consumerId=[2d80c908-3699-4bf2-8eb8-ea1069de108a]}
          Headers = []
             Body = null
    Session Attrs = {}

Handler:
             Type = com.foo.http.v1.stores.GetStoresByMarket
           Method = com.foo.http.v1.stores.GetStoresByMarket#getStoresByMarket(String, String, String, Integer, Integer, Continuation)

Async:
    Async started = true
     Async result = org.springframework.web.server.ResponseStatusException: 400 BAD_REQUEST

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = []
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Status expected:<400> but was:<200>
Expected :400
Actual   :200
How do I make sure I get the async result back in the test? I have tried variations of
runBlocking
and
runTest
to avail
i

Ivan Pavlov

05/08/2023, 2:52 PM
Not tested if it's everything you need, but you should call asyncDispatch like described here https://stackoverflow.com/a/58997355
a

Axel

05/08/2023, 2:57 PM
that worked, thank you 🙂