You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a test against a generic PUT endpoint. The .andExpect { status().isOk } assertion always passes, regardless of the actual result status, while .andExpect( status().isOk ) works as expected. I suspect this is a bug?
We're using spring-boot-starter-parent:2.6.6 on Kotlin 1.6.10 on a JDK 11.
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Test {
@Autowired
lateinit var mockMvc: MockMvc
@Test
fun `should reject invalid payload`() {
mockMvc.perform( MockMvcRequestBuilders.put("/analyses/297e2c6c6a26a2ba016a27bd5f977f74")
.contentType(APPLICATION_JSON)
.content("invalid")
)
.andExpect { status().isOk } // does not work
.andExpect( status().isOk ) // works
}
}
The text was updated successfully, but these errors were encountered:
I can see how this is confusing, but I don't really see it as a bug.
andExpect() expects a ResultMatcher.
In the working case, that's what you're passing: status().isOk is an expression of type ResultMatcher. When it's match() method is called with an MvcResult, it checks that the status in the MvcResult is OK. So the working code is equivalent to
You're totally right, thanks for the detailed explanation! This is an example of the main failure mode of DSLs in my experience: what actually happens becomes more obfuscated in the quest for aesthetic call chains. Sorry to have wasted your time.
This is a test against a generic PUT endpoint. The
.andExpect { status().isOk }
assertion always passes, regardless of the actual result status, while.andExpect( status().isOk )
works as expected. I suspect this is a bug?We're using spring-boot-starter-parent:2.6.6 on Kotlin 1.6.10 on a JDK 11.
The text was updated successfully, but these errors were encountered: