我正在对 Spring 控制器 post 方法(使用org.springframework.test.web.servlet.MockMvc
)进行单元测试,并且我试图确认当表单中存在验证错误时,它将通过检查BindingResult.hasErrors
方法将视图发送回表单。
这是我的测试
@Test
public void testFilterChannelProgrammesWhenChannelListAndGenreListAreEmptyAndProgNameIsTooLong() throws Exception {
String progName = TestUtil.createStringWithLength(301);
mockMvc.perform(post("/api/filter")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("progName", progName)
.sessionAttr("filter", new ProgrammeSearchDTO())
)
.andExpect(status().isOk())
.andExpect(view().name("api/filter"))
.andExpect(forwardedUrl("/WEB-INF/jsp/api/filter.jsp"))
.andExpect(model().attributeHasFieldErrors("filter", "progName"))
.andExpect(model().attributeHasFieldErrors("filter", "genreIdList"))
.andExpect(model().attributeHasFieldErrors("filter", "channelIdList"))
.andExpect(model().attribute("filter", hasProperty("progName", is(progName))));
verifyZeroInteractions(channelProgrammeServiceMock);
}
这是会话属性绑定到的 DTO
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
public class ProgrammeSearchDTO {
@NotEmpty
private String[] channelIdList;
@NotEmpty
private String[] genreIdList;
private String fromDateTime;
private String toDateTime;
@Length(max = 200)
private String progName;
private boolean subtitled;
private boolean signed;
private boolean film;
public String[] getChannelIdList() {
return channelIdList;
}
public String getFromDateTime() {
return fromDateTime;
}
public String[] getGenreIdList() {
return genreIdList;
}
public String getProgName() {
return progName;
}
public String getToDateTime() {
return toDateTime;
}
public boolean isFilm() {
return film;
}
public boolean isSigned() {
return signed;
}
public boolean isSubtitled() {
return subtitled;
}
public void setChannelIdList(String[] channelIdList) {
this.channelIdList = channelIdList;
}
public void setFilm(boolean film) {
this.film = film;
}
public void setFromDateTime(String fromDateTime) {
this.fromDateTime = fromDateTime;
}
public void setGenreIdList(String[] genreIdList) {
this.genreIdList = genreIdList;
}
public void setProgName(String progName) {
this.progName = progName;
}
public void setSigned(boolean signed) {
this.signed = signed;
}
public void setSubtitled(boolean subtitled) {
this.subtitled = subtitled;
}
public void setToDateTime(String toDateTime) {
this.toDateTime = toDateTime;
}
}
和控制器方法
@RequestMapping(value = "/api/filter", method = RequestMethod.POST)
public String filterChannelProgrammes(@Valid @ModelAttribute ProgrammeSearchDTO programmeSearchDTO, BindingResult result, Model model) {
if(result.hasErrors()) {
return "api/filter";
}
model.addAttribute("results", null);
return "redirect:filterResults";
}
对于这个测试,return "api/filter";
应该采取行动,但hasErrors()
总是错误的。我也尝试过以下
@RequestMapping(value = "/api/filter", method = RequestMethod.POST)
public String filterChannelProgrammes(@Valid @ModelAttribute("filter") ProgrammeSearchDTO programmeSearchDTO, BindingResult result, Model model) {
if(result.hasErrors()) {
return "api/filter";
}
model.addAttribute("results", null);
return "redirect:filterResults";
}
但是hasErrors()
还是假的
编辑
经过更多挖掘后,我对此进行了排序,它还需要在上下文配置 xml 中包含以下内容
<mvc:annotation-driven />
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
而这些依赖在maven pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>