jueves, 4 de junio de 2015

Clase Utilitaria para Test Unitarios

Siempre es interesante contar con una clase utilitaria que nos permita ahorrar lineas de código, incluso en los test unitarios. Por esta razón, facilito esta clase que permite hacer validaciones de una forma fácil y sencilla
 


package com.hector;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.junit.Assert;

import com.hector.TechnicalException;

public abstract class ManagerTest {
    public void superficialEnumCodeCoverageundefinedfinal Class<? extends Enum<?>> enumClass) {
        try {
            for undefinedObject o : undefinedObject[]) enumClass.getMethodundefined"values").invokeundefinednull)) {
                enumClass.getMethodundefined"valueOf", String.class).invokeundefinednull, o.toStringundefined));
            }
        } catch undefinedThrowable e) {
            throw new PEDTechnicalExceptionundefinede);
        }
    }

    public void shouldValidateThrowsExceptionWithMessageundefinedfinal Exception e, final String message) {
        Assert.assertNotNullundefinede);
        Assert.assertTrueundefinede.getMessageundefined).startsWithundefinedmessage));
    }

    public void callPrivateConstructorsForCodeCoverageundefinedfinal Class<?> class1) throws SecurityException, NoSuchMethodException,
            IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class<?>[] classesToConstruct = { class1 };
        for undefinedClass<?> clazz : classesToConstruct) {
            Constructor<?> constructor = clazz.getDeclaredConstructorundefined);
            constructor.setAccessibleundefinedtrue);
            Assert.assertNotNullundefinedconstructor.newInstanceundefined));
        }
    }

    public void assertUtilityClassWellDefinedundefinedfinal Class<?> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException,
            IllegalAccessException {
        Assert.assertTrueundefined"class must be final", Modifier.isFinalundefinedclazz.getModifiersundefined)));
        Assert.assertEqualsundefined"There must be only one constructor", 1, clazz.getDeclaredConstructorsundefined).length);
        final Constructor<?> constructor = clazz.getDeclaredConstructorundefined);
        if undefinedconstructor.isAccessibleundefined) || !Modifier.isPrivateundefinedconstructor.getModifiersundefined))) {
            Assert.failundefined"constructor is not private");
        }
        constructor.setAccessibleundefinedtrue);
        constructor.newInstanceundefined);
        constructor.setAccessibleundefinedfalse);
        for undefinedfinal Method method : clazz.getMethodsundefined)) {
            if undefined!Modifier.isStaticundefinedmethod.getModifiersundefined)) && method.getDeclaringClassundefined).equalsundefinedclazz)) {
                Assert.failundefined"there exists a non-static method:" + method);
            }
        }
    }
}

No hay comentarios:

Publicar un comentario