Created
June 4, 2011 15:13
-
-
Save felipepedrini/1007965 to your computer and use it in GitHub Desktop.
Vraptor-sequence-validator raw example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Format { | |
} | |
interface Required { | |
} | |
@GroupSequence({ Default.class, Required.class, Format.class }) | |
interface Sequence { | |
} | |
@javax.persistence.Entity | |
class User extends Entity { | |
@NotBlank(groups = Required.class) | |
private String name; | |
@Email(groups = Format.class) | |
@NotBlank(groups = Required.class) | |
private String email; | |
@Length(min = 6, message = "{validation.weakPass}", groups = Format.class) | |
@NotBlank(groups = Required.class) | |
private String password; | |
// getters and setters omitted | |
} | |
@Resource | |
class UserController { | |
private final Result result; | |
private final UserRepository repository; | |
private final SequenceValidator validator; | |
// ... | |
UserController(Result result, UserRepository repository, SequenceValidator validator) { | |
this.result = result; | |
this.repository = repository; | |
this.validator = validator; | |
} | |
@Post("/users") | |
@Public | |
public void create(final User user) { | |
validator.validate(user, Sequence.class)); | |
validator.onErrorUsePageOf(this).newUser(); | |
// more code | |
} | |
} | |
// unit test | |
public class UserControllerTest { | |
private UserController controller; | |
private MockResult result; | |
private UserRepository repository; | |
private JSR303MockSequenceValidator validator; | |
@Before | |
public void setUp() { | |
result = spy(new MockResult()); | |
validator = spy(new JSR303MockSequenceValidator()); | |
repository = mock(UserRepository.class); | |
controller = new UserController(result, repository, validator); | |
} | |
// the tests | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment