Search in sources :

Example 11 with FrameworkField

use of org.junit.runners.model.FrameworkField in project junit4 by junit-team.

the class BlockJUnit4ClassRunnerWithParameters method createTestUsingFieldInjection.

private Object createTestUsingFieldInjection() throws Exception {
    List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
    if (annotatedFieldsByParameter.size() != parameters.length) {
        throw new Exception("Wrong number of parameters and @Parameter fields." + " @Parameter fields counted: " + annotatedFieldsByParameter.size() + ", available parameters: " + parameters.length + ".");
    }
    Object testClassInstance = getTestClass().getJavaClass().newInstance();
    for (FrameworkField each : annotatedFieldsByParameter) {
        Field field = each.getField();
        Parameter annotation = field.getAnnotation(Parameter.class);
        int index = annotation.value();
        try {
            field.set(testClassInstance, parameters[index]);
        } catch (IllegalAccessException e) {
            IllegalAccessException wrappedException = new IllegalAccessException("Cannot set parameter '" + field.getName() + "'. Ensure that the field '" + field.getName() + "' is public.");
            wrappedException.initCause(e);
            throw wrappedException;
        } catch (IllegalArgumentException iare) {
            throw new Exception(getTestClass().getName() + ": Trying to set " + field.getName() + " with the value " + parameters[index] + " that is not the right type (" + parameters[index].getClass().getSimpleName() + " instead of " + field.getType().getSimpleName() + ").", iare);
        }
    }
    return testClassInstance;
}
Also used : Field(java.lang.reflect.Field) FrameworkField(org.junit.runners.model.FrameworkField) Parameter(org.junit.runners.Parameterized.Parameter) FrameworkField(org.junit.runners.model.FrameworkField)

Example 12 with FrameworkField

use of org.junit.runners.model.FrameworkField in project junit4 by junit-team.

the class BlockJUnit4ClassRunnerWithParameters method validateFields.

@Override
protected void validateFields(List<Throwable> errors) {
    super.validateFields(errors);
    if (getInjectionType() == InjectionType.FIELD) {
        List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
        int[] usedIndices = new int[annotatedFieldsByParameter.size()];
        for (FrameworkField each : annotatedFieldsByParameter) {
            int index = each.getField().getAnnotation(Parameter.class).value();
            if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
                errors.add(new Exception("Invalid @Parameter value: " + index + ". @Parameter fields counted: " + annotatedFieldsByParameter.size() + ". Please use an index between 0 and " + (annotatedFieldsByParameter.size() - 1) + "."));
            } else {
                usedIndices[index]++;
            }
        }
        for (int index = 0; index < usedIndices.length; index++) {
            int numberOfUse = usedIndices[index];
            if (numberOfUse == 0) {
                errors.add(new Exception("@Parameter(" + index + ") is never used."));
            } else if (numberOfUse > 1) {
                errors.add(new Exception("@Parameter(" + index + ") is used more than once (" + numberOfUse + ")."));
            }
        }
    }
}
Also used : Parameter(org.junit.runners.Parameterized.Parameter) FrameworkField(org.junit.runners.model.FrameworkField)

Aggregations

FrameworkField (org.junit.runners.model.FrameworkField)12 Field (java.lang.reflect.Field)7 ArrayList (java.util.ArrayList)4 Parameter (org.junit.runners.Parameterized.Parameter)4 MultipleFailureException (org.junit.runners.model.MultipleFailureException)2 Key (com.google.inject.Key)1 Errors (com.google.inject.internal.Errors)1 ErrorsException (com.google.inject.internal.ErrorsException)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Random (java.util.Random)1 AfterClass (org.junit.AfterClass)1 BeforeClass (org.junit.BeforeClass)1 Parameterized (org.junit.runners.Parameterized)1 TestClass (org.junit.runners.model.TestClass)1