Skip to content

Commit 5993bd4

Browse files
committedJan 8, 2014
Adding unit test for: Sonar blocker: obj could be null and is guaranteed
to be dereferenced in org.openmrs.validator.UserValidator.validate - TRUNK-4199
1 parent 4d9b5f4 commit 5993bd4

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
 

‎api/src/main/java/org/openmrs/validator/UserValidator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ public boolean supports(Class c) {
6363
* @should pass validation if all required fields have proper values
6464
* @should fail validation if email as username enabled and email invalid
6565
* @should fail validation if email as username disabled and email provided
66+
* @should not throw NPE when user is null
6667
*/
6768
public void validate(Object obj, Errors errors) {
6869
User user = (User) obj;
6970
if (user == null) {
70-
errors.rejectValue("user", "error.general");
71+
errors.reject("error.general");
7172
} else {
7273
if (user.isRetired() && StringUtils.isBlank(user.getRetireReason()))
7374
errors.rejectValue("retireReason", "error.null");

‎api/src/test/java/org/openmrs/validator/UserValidatorTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,16 @@ public void validate_shouldFailValidationIfEmailAsUsernameDisabledAndEmailProvid
248248

249249
Assert.assertTrue(errors.hasFieldErrors("username"));
250250
}
251+
252+
/**
253+
* @see UserValidator#validate(Object,Errors)
254+
*/
255+
@Test
256+
@Verifies(value = "not throw NPE when user is null", method = "validate(Object,Errors)")
257+
public void validate_shouldNotThrowNPEWhenUserIsNull() throws Exception {
258+
UserValidator userValidator = new UserValidator();
259+
Errors errors = new BindException(new User(), "user");
260+
userValidator.validate(null, errors);
261+
Assert.assertTrue(true);
262+
}
251263
}

0 commit comments

Comments
 (0)
Please sign in to comment.