lundi 17 juillet 2017

Junit test cases for private method in Java reflection API

I have written the below Junit test cases to test private method using java reflection. I have skipped few statement in UserException.java.

Hope it will help you.

  1. ValidatorTest.java

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    import org.junit.Before;
    import org.junit.Rule;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.rules.ExpectedException;
    
    import com.test.enums.ErrorMessage;
    import com.test.exception.UserException;
    import com.test.utils.Validator;
    
    public class ValidatorTest {
        Object validatorObject;
        Method method;
    
        @Before
        public void setUp() throws Exception {
            Class<Validator> validatorClass = Validator.class;
            validatorObject = validatorClass.newInstance();
            method = validatorObject.getClass().getDeclaredMethod(
                    "validateParam",
                    new Class[] { String.class, ErrorMessage.class });
            method.setAccessible(true);
        }
    
        @Rule
        private ExpectedException exception = ExpectedException.none();
    
        @Test(expected = UserException.class)
        public void testValidateException() throws Throwable {
            String inputForValidation = "PRASANT_ S\b\t\n\r";
            exception.expect(UserException.class);
            try {
                method.invoke(validatorObject, inputForValidation,
                        ErrorMessage.MY_ERROR_MESSAGE);
            } catch (InvocationTargetException | IllegalAccessException e) {
                throw e.getCause();
            }
        }
    
        @Test(expected = UserException.class)
        public void testValidateException2() throws InvocationTargetException,
                IllegalAccessException {
            String inputForValidation = "PRASANT @\b\t\n\r";
            exception.expect(UserException.class);
            try {
                method.invoke(validatorObject, inputForValidation,
                        ErrorMessage.MY_ERROR_MESSAGE);
            } catch (InvocationTargetException | IllegalAccessException e) {
                Assert.assertTrue("Input has invalid characters",
                        (e.getCause() instanceof UserException));
                throw (UserException) e.getCause();
            }
            Assert.fail("Invalid response");
        }
    
        @Test
        public void validatorTest() throws InvocationTargetException,
                IllegalAccessException {
            String inputForValidation = "PRASANT_S";
            method.invoke(validatorObject, inputForValidation,
                    ErrorMessage.MY_ERROR_MESSAGE);
            Assert.assertTrue(true);
        }
    }
    
    
  2. Validator.java

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.test.enums.ErrorMessage;
    import com.test.exception.UserException;
    
    public class Validator {
        private static final Logger LOG = LoggerFactory.getLogger(Validator.class);
    
        @SuppressWarnings("unused")
        private void validateParam(String validateString,
                ErrorMessage errorMsg) throws UserException {
            StringBuilder invalidChars = new StringBuilder();
            for (int i = 0; i < validateString.length(); i++) {
                switch (validateString.charAt(i)) {
                case '\t': {
                    invalidChars.append('\t');
                    break;
                }
                case '\n': {
                    invalidChars.append('\n');
                    break;
                }
                case '\b': {
                    invalidChars.append('\b');
                    break;
                }
                case '\r': {
                    invalidChars.append('\r');
                    break;
                }
                }
            }
            if (invalidChars.length() != 0) {
                LOG.error(errorMsg.getMsg() + " " + invalidChars.toString());
                throw new UserException(errorMsg, invalidChars.toString());
            }
        }
    }
    
    
  3. ErrorMessage.java

        public enum ErrorMessage {
            MY_ERROR_MESSAGE(490,"The invalid characters are ");
    
            private final int errorCode;
            private final String msg;
    
            private ErrorMessage(int errorCode,String msg){
                this.errorCode=errorCode;
                this.msg=msg;
            }
    
            public int getCode() {
                return errorCode;
            }
    
            public String getMsg() {
                return msg;
            }
    
            @Override
            public String toString(){
                return msg;
            }
        }
    
    
  4. UserException.java

        public class UserException extends RuntimeException {
            private String errorMessage;
            private int errorCode = Response.Status.BAD_REQUEST.getStatusCode();
            private static final String DEFAULT_MESSAGE = "Bad request, please check url or request parameters";
    
            public UserException(ErrorMessage userMessage, String invChar) {
                if (userMessage != null) {
                    this.errorMessage = userMessage.getMsg() + " " + invChar;
                    this.errorCode = userMessage.getCode();
                }
            }
            ................
                ............
                ............
        }
    
    




Aucun commentaire:

Enregistrer un commentaire