Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipepedrini/1007965 to your computer and use it in GitHub Desktop.
Save felipepedrini/1007965 to your computer and use it in GitHub Desktop.
Vraptor-sequence-validator raw example
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