Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help! I having the following simple validator class:
Java
 package com.govpaynet.rest.validator.marionco;
 
import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.validation.Errors;
 
import com.govpaynet.rest.integration.GenericResponseCode;
 import com.govpaynet.rest.model.CheckUTTRequest;
 import com.govpaynet.validator.model.Validator;
 
public class CheckUTTValidator implements Validator<checkuttrequest> {
 private final Logger logger = LoggerFactory
 .getLogger(CheckUTTValidator.class);
 
public final void validate(final Object obj, final Errors errors) {
 final CheckUTTRequest input = (CheckUTTRequest)obj;
 if (input.getInputValue().isEmpty()){
 logger.debug("The user input data.");
 errors.rejectValue(GenericResponseCode.GENERIC_200.value(),"Invalid request data.");
 
}
 return;
 }
 
@Override
 public final Boolean supports(final Classreturn inputType != null && CheckUTTRequest.class.isAssignableFrom(inputType); 
}
 
}

I need to unit test it using junit. I have the following class:
Java
 package com.govpaynet.rest.validator.marionco;
 
import static org.junit.Assert.*;
 
import org.easymock.Capture;
 import org.junit.Before;
 import org.junit.Test;
 import org.springframework.validation.Errors;
 
import com.govpaynet.rest.integration.GenericResponseCode;
 import com.govpaynet.rest.model.CheckUTTRequest;
 
public class CheckUTTValidatorTest {
 private CheckUTTValidator validator;
 private CheckUTTRequest request;
 private Errors errorsMock;
 
@Before
 public void setup(){
 //request=new CheckUTTRequest();
 request.setUserInputValue(null, null);
 
request=creakeMock(CheckUTTValidator.class);
 errorsMock= createMock(Errors.class);
 validator=new CheckUTTValidator();
 
}
 
@Test
 public void supportsNot() {
 assertFalse(validator.supports(null));
 }
 
@Test
 public void supports() {
 try{
 assertTrue(validator.supports(CheckUTTRequest.class));
 }
 catch(NullPointerException e){
 
}
 }
 
@Test
 public void validateEmptyCheckUTTUserInput() {
 GenericResponseCode expectedErroCode = GenericResponseCode.GENERIC_200;
 final Capture<string> errorCode = new Capture<string>();
 
try {
 request.setUserInputValue(null, null);
 validator.validate(request, errorsMock);
 assertEquals(expectedErroCode, errorCode.getValue());
 } catch (NullPointerException e) {
 
}
 }
 
}

I only want to test the validate method to make sure that if the arraylist is empty the appropriate errors will be returned. I was attempting to use easyMock, but things have not went successful. Please help what sould the appropriate junit testcase look like to test this simple validator.
Posted
Updated 18-Aug-13 16:36pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900