【问题标题】:Hibernate validation no validation休眠验证无验证
【发布时间】:2012-09-18 03:18:56
【问题描述】:

您好,我正在关注我找到的示例

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;


}

【问题讨论】:

  • 显示你的 Profile 类源

标签: java validation spring-mvc spring-3 hibernate-validator


【解决方案1】:

基本上你用this.userController = new UserController() 实例化了一个POJO,然后调用它的方法this.controller.postUser(...)。只是带有简单对象的简单 Java,与 Spring 和 Spring MVC 没有任何关系:不考虑 @Valid。

如果你想让它工作,你必须给你的测试类一些 Spring 信息,@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(...)。然后,对于 Spring MVC 部分,您将不得不通过一些 Spring MVC 设施模拟控制器上的请求调用。如果您使用 Spring MVC 3.0- 或 3.1+,它会有所不同。更多信息和实际代码,例如see this post and its answers

【讨论】:

  • 我正在使用@ContextConfiguration(...) 和@RunWith(SpringJUnit4ClassRunner.class)。我的控制器现在没有接受任何请求。也许我应该改变它。
  • @user874774 控制器本身很好,它是你在单元测试中调用它的方式,它不起作用。你不能只做this.controller.postUser(...)。请参阅我给您的链接以了解如何调用它。
  • 在哪里可以找到框架的 jar。无法解决对 GIT hub 站点的依赖。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多