Spring Mockmvcは入れ子形式のパラメータを通過します
-
21-12-2019 - |
質問
次の形式の
public class MyForm {
private Account account;
}
public class Account {
private String firstName;
}
.
firstNameパラメータをどのように渡すのですか? (以下のアプローチが機能しない)
mockMvc.perform(post("/xyz")
.param("account.firstName", "John"))
.andExpect(model().hasErrors())
.andExpect(view().name("/xyz"))
.andExpect(status().isOk())
. 解決
最後にこの問題を解決しました。スタンドアロンの設定を使用しているので、バリデータとMestorySourceを定義しなければなりませんでした。
void setupTest() {
MockitoAnnotations.initMocks(this)
this.mockMvc = MockMvcBuilders.standaloneSetup(getController())
.setValidator(getValidator())
.alwaysDo(MockMvcResultHandlers.print())
.build()
}
private MessageSource getMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
private LocalValidatorFactoryBean getValidator() {
def validator = new LocalValidatorFactoryBean()
validator.setValidationMessageSource(getMessageSource());
return validator;
}
. 所属していません StackOverflow