Java Hibernate验证没有验证
嗨,我在遵循我发现的一个例子Java Hibernate验证没有验证,java,validation,spring-mvc,spring-3,hibernate-validator,Java,Validation,Spring Mvc,Spring 3,Hibernate Validator,嗨,我在遵循我发现的一个例子 http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/ 问题是在我发布的个人资料中没有发现错误。我应该是。为什么会这样 @Test @Ignore public void anotherTest() { Profile profile = ProfileUtil.getProfile(); profile.setEmail("user@mail.com");
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/
问题是在我发布的个人资料中没有发现错误。我应该是。为什么会这样
@Test
@Ignore
public void anotherTest() {
Profile profile = ProfileUtil.getProfile();
profile.setEmail("user@mail.com");
profile.setSex("dafjsgkkdsfa");
BindingResult bindingResult = new BeanPropertyBindingResult(profile, "profile");
userController.postUser(new ModelMap(), profile, bindingResult);
if (bindingResult.hasErrors()) {
System.out.println("errors");
}
assertTrue(bindingResult.hasErrors());
profileService.deleteProfile(profile);
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public View postUser(ModelMap data, @Valid Profile profile, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
System.out.println("No errors");
return dummyDataView;
}
data.put(DummyDataView.DATA_TO_SEND, "users/user-1.json");
profileService.save(profile);
return dummyDataView;
}
编辑:
这是个人资料。我现在正在测试性,所以我想这才是重要的
package no.tine.web.tinetips.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import no.tine.web.tinetips.util.CommonRegularExpressions;
import org.hibernate.validator.constraints.NotBlank;
@Entity
public class Profile {
@Id
@GeneratedValue
private Long id;
@NotNull(message = "profile.email.null")
@NotBlank(message = "profile.email.blank")
@Size(max = 60, message = "profile.email.maxlength")
@Pattern(regexp = CommonRegularExpressions.EMAIL, message = "profile.email.regex")
@Column(name = "Email", unique = true)
private String email;
@Pattern(regexp = "^[M|F]{1}$", message = "profile.sex.regex")
@Size(max = 1, message = "profile.sex.maxlength")
private String sex;
}
基本上,您用
this.userController=newusercontroller()
实例化了一个POJO,然后调用了它的方法this.controller.postaser(…)
。只是一个简单的Java和一个简单的对象,与Spring没有任何关系,Spring MVC:@Valid没有被考虑在内
如果您想让它正常工作,您必须使用
@RunWith(SpringJUnit4ClassRunner.class)
和@ContextConfiguration(…)
为测试类提供一些Spring信息。然后,对于SpringMVC部分,您必须通过一些SpringMVC工具模拟控制器上的请求调用。如果您使用SpringMVC3.0-或3.1+,则会有不同的做法。有关更多信息和实际代码,例如。基本上,您使用this.userController=new userController()
实例化了一个POJO,然后调用了它的方法this.controller.postser(…)
。只是一个简单的Java和一个简单的对象,与Spring没有任何关系,Spring MVC:@Valid没有被考虑在内
如果您想让它正常工作,您必须使用
@RunWith(SpringJUnit4ClassRunner.class)
和@ContextConfiguration(…)
为测试类提供一些Spring信息。然后,对于SpringMVC部分,您必须通过一些SpringMVC工具模拟控制器上的请求调用。如果您使用SpringMVC3.0-或3.1+,则会有不同的做法。有关更多信息和实际代码,例如。显示您的配置文件类源显示您的配置文件类源我正在使用@ContextConfiguration(…)和@RunWith(SpringJUnit4ClassRunner.class)。我的控制器现在不接受任何请求。也许我应该改变它。@user874774控制器本身很好,但在单元测试中调用它的方式不起作用。你不能只做这个.controller.postaser(…)
。请参阅我给您的链接,了解如何调用它。在哪里可以找到框架的jar。无法解决对GIT hub站点的依赖关系。我正在使用@ContextConfiguration(…)和@RunWith(SpringJUnit4ClassRunner.class)。我的控制器现在不接受任何请求。也许我应该改变它。@user874774控制器本身很好,但在单元测试中调用它的方式不起作用。你不能只做这个.controller.postaser(…)
。请参阅我给您的链接,了解如何调用它。在哪里可以找到框架的jar。无法解决对GIT hub站点的依赖关系。