1

Hi i am following an example I found

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

The problem is that no errors are found in my profile that I post. I should be. Why can this happend?

@Test
@Ignore
public void anotherTest() {
    Profile profile = ProfileUtil.getProfile();

    profile.setEmail("[email protected]");
    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;
}

Edit: This is the Profile. I am testing the sex now so I guess thats what is important.

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;


}
1
  • show your Profile class source Commented Sep 26, 2012 at 13:25

1 Answer 1

1

Basically you instantiated a POJO with this.userController = new UserController(), then called its method this.controller.postUser(...). Just simple Java with a simple object, without any relation to Spring and Spring MVC : @Valid is not taken into account.

If you want to make it work, you will have to give your test class some Spring information, with @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(...). Then, for the Spring MVC part, you will have to mock a request call on your Controller through some Spring MVC facilites. It is done differently if you use Spring MVC 3.0- or 3.1+. For more information and actual code, see this post and its answers, for example.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using @ContextConfiguration(...) and @RunWith(SpringJUnit4ClassRunner.class). My controller does not take any Request by now. Maybe I should change that.
@user874774 The controller by itself is fine, it is how you call it in your unit test that doesn't work. You can't just do this.controller.postUser(...). See the link I have given you to know how to call it.
Where can I find the jar for the framework. The dependency on the GIT hub site cant be resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.