Last active
August 29, 2015 14:02
-
-
Save dtelaroli/5540faeb9aa6b89d4a82 to your computer and use it in GitHub Desktop.
VRaptor MockValidator
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
@Test | |
public void shouldRedirectIfInvalid() { | |
ModelImpl model = new ModelImpl(); | |
model.setId(1L); | |
controller.update(model); | |
assertThat(validator.used(), equalTo(true)); | |
} |
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
class CustomMockValidator extends MockValidator { | |
private javax.validation.Validator bvalidator; | |
private boolean enabled = true; | |
private boolean used; | |
public CustomMockValidator() { | |
bvalidator = Validation.buildDefaultValidatorFactory().getValidator(); | |
} | |
@Override | |
public Validator validate(Object object, Class<?>... groups) { | |
if(enabled) { | |
addAll(bvalidator.validate(object, groups)); | |
} | |
return this; | |
} | |
@Override | |
public <T extends View> T onErrorUse(Class<T> view) { | |
if(hasErrors()) { | |
used = true; | |
} | |
return new MockResult().use(view); | |
} | |
public boolean used() { | |
return used; | |
} | |
public Validator disableValidation() { | |
enabled = false; | |
return this; | |
} | |
} |
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
public class CustomMockValidator extends MockValidator { | |
private javax.validation.Validator bvalidator; | |
private boolean enabled = true; | |
public CustomMockValidator() { | |
bvalidator = Validation.buildDefaultValidatorFactory().getValidator(); | |
} | |
@Override | |
public Validator validate(Object object, Class<?>... groups) { | |
if(enabled) { | |
addAll(bvalidator.validate(object, groups)); | |
} | |
return this; | |
} | |
@Override | |
public <T extends View> T onErrorUse(Class<T> view) { | |
if(hasErrors()) { | |
throw new ValidationException(); | |
} | |
return new MockResult().use(view); | |
} | |
public Validator disableValidation() { | |
enabled = false; | |
return this; | |
} | |
} |
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
//não preciso testar porque o vraptor faz pra mim | |
public Model create(@NotNull @Valid Model model) { | |
validator().onErrorRedirectTo(this).add(); | |
return em.merge(model); | |
} |
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
@Test(expected = ValidationException.class) | |
public void shouldBeInvalid() { | |
Model model = new Model(); | |
model.setId(1L); | |
controller.update(model); | |
} |
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
//não tem como testar se ocorreu erro | |
public Model create(Model model) { | |
validator.validate(model); | |
validator.onErrorRedirectTo(this).add(); | |
return em.merge(model); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment