我正在对Spring Controller 的post方法(使用org.springframework.test.web.servlet.MockMvc
)进行单元测试,并且试图确认表单中存在验证错误时,它将通过检查BindingResult.hasErrors
方法将 View 发送回表单。
这是我的测试
@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);
}
这是 session 属性绑定(bind)到的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;
}
}
和 Controller 的方法
@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()
始终为false。我也尝试了以下 @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>
最佳答案
经过更多的挖掘之后,我对此进行了排序,它还需要在上下文配置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>