I\'m writing tests to verify that I can do a generic form post to our API.
I\'ve also added quite some debugging, but I noticed that the data posted by an actual for
Here is a Kotlin SpringBoot example:
@RunWith(MockitoJUnitRunner::class)
class ApiFormControllerTest {
lateinit var mvc: MockMvc
@InjectMocks
lateinit var apiFormController: ApiFormController
@Before
fun setup() {
mvc = MockMvcBuilders.standaloneSetup(apiFormController).setControllerAdvice(ExceptionAdvice()).build()
}
fun MockHttpServletRequestBuilder.withForm(params: Map): MockHttpServletRequestBuilder {
this.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(
EntityUtils.toString(
UrlEncodedFormEntity(
params.entries.toList().map { BasicNameValuePair(it.key, it.value) }
)
)
)
return this
}
@Test
fun canSubmitValidForm() {
mvc.perform(post("/forms").withForm(mapOf("subject" to "hello")))
.andExpect(status().isOk)
}
}