我对这个问题很头疼。我想在我的测试中模拟数据的表单发布,但用户模型始终为null,因此始终无效。这是控制器动作代码:
@RequestMapping(value = "/users/add", method = RequestMethod.POST)
public ModelAndView addUser(ModelMap map, @Valid @ModelAttribute(value = "user") User user, BindingResult result,
HttpServletResponse response, final RedirectAttributes redirectAttributes) throws IOException {
if(!result.hasErrors()) {
try {
String userStatus = user.getId() != null ? "User Updated: " : "User Created: ";
userService.saveOrUpdateUser(user);
redirectAttributes.addFlashAttribute("userStatusMessage", userStatus + user.toString());
return new ModelAndView(new RedirectView("/users"));
} catch (EntityNotFoundException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
return null;
}
}
map.addAttribute("genders", generateGenders());
return new ModelAndView("addUser", "user", user);
}
这是测试代码:
@Test
public void createUserTest() throws Exception {
User user = generateSampleUser(null, "Pera", "Peric");
when(userServiceMock.saveOrUpdateUser(user)).thenAnswer(resolveUserIdGeneration(user)).thenReturn(user);
mockMvc.perform(post("/users/add")
.param("firstName", user.getFirstName())
.param("email", user.getEmail())
.param("birthday", new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday()))
.param("gender", user.getGender().toString())
.param("personalNumber", user.getPersonalNumber()))
.andExpect(status().isOk())
.andExpect(model().attributeHasNoErrors("user"));
verify(userServiceMock, times(1)).saveOrUpdateUser(user);
verifyNoMoreInteractions(userServiceMock);
}
最后是模型代码:
@Entity
@Table(name = "BADMINTON_USER")
public class User extends AbstractBaseEntity {
@Column(length = 64, nullable = false)
@Size(min = 3, max = 64)
@NotNull
private String firstName;
@Column(length = 64, nullable = false)
@Size(min = 3, max = 64)
@NotNull
private String lastName;
@Column(nullable = false)
@Temporal(value = TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd")
@NotNull
private Date birthday;
@Column(nullable = false)
@Enumerated(EnumType.ORDINAL)
@NotNull
private Gender gender;
@Column(nullable = false, length = 13)
@Size(min = 13, max = 13, message = "Personal number must be exactly 13 characters long.")
@NotNull
private String personalNumber;
@Column(length = 64, nullable = false)
@Email
@Size(min = 10, max = 64)
@NotNull
private String email;
@Column(length = 20, nullable = true)
@Phone
private String phone1;
@Column(length = 20, nullable = true)
@Phone
private String phone2;
@Column(length = 200, nullable = true)
private String note;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List<Slot> slots = new ArrayList<Slot>();
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getPersonalNumber() {
return personalNumber;
}
public void setPersonalNumber(String personalNumber) {
this.personalNumber = personalNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone1() {
return phone1;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public String getPhone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public List<Slot> getSlots() {
return slots;
}
public void setSlots(List<Slot> slots) {
this.slots = slots;
}
@Override
public String toString() {
return String.format("%d, %s %s, %s, %s, %s, %s", this.getId(), this.getFirstName(), this.getLastName(),
new SimpleDateFormat("yyyy-MM-dd").format(this.getBirthday()), this.getGender().getDisplayName(), this.getPersonalNumber(),
this.getEmail());
}
}
编辑:可以在此处找到跟踪日志:http://pastebin.com/KyRjujyV