/** * Returns a name used to describe this Runner */ protected String getName() { return testClass.getName(); }
@Override public String toString() { return testClass.getName() + " '" + name + "' with parameters " + parameters; } }
/** * Returns a name used to describe this Runner */ protected String getName() { return fTestClass.getName(); }
private static Exception parametersMethodReturnedWrongType( TestClass testClass, FrameworkMethod parametersMethod) throws Exception { String className = testClass.getName(); String methodName = parametersMethod.getName(); String message = MessageFormat.format( "{0}.{1}() must return an Iterable of arrays.", className, methodName); return new Exception(message); }
/** * Validate that the specified {@link TestClass} is public. * * @param testClass the {@link TestClass} that is validated. * @return an empty list if the class is public or a list with a single * exception otherwise. */ public List<Exception> validateTestClass(TestClass testClass) { if (testClass.isPublic()) { return NO_VALIDATION_ERRORS; } else { return singletonList(new Exception("The class " + testClass.getName() + " is not public.")); } } }
private Exception parametersMethodReturnedWrongType(FrameworkMethod method) throws Exception { String className = getTestClass().getName(); String methodName = method.getName(); String message = MessageFormat.format( "{0}.{1}() must return an Iterable of arrays.", className, methodName); return new Exception(message); }
private static FrameworkMethod getParametersMethod(TestClass testClass, ClassLoader classLoader) throws Exception { List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class); for (FrameworkMethod each : methods) { int modifiers = each.getMethod().getModifiers(); if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) { return getFrameworkMethodInClassLoader(each, classLoader); } } throw new Exception("No public static parameters method on class " + testClass.getName()); }
protected void validateNoNonStaticInnerClass(List<Throwable> errors) { if (getTestClass().isANonStaticInnerClass()) { String gripe = "The inner class " + getTestClass().getName() + " is not static."; errors.add(new Exception(gripe)); } }
protected void validateNoNonStaticInnerClass(List<Throwable> errors) { if (getTestClass().isANonStaticInnerClass()) { String gripe = "The inner class " + getTestClass().getName() + " is not static."; errors.add(new Exception(gripe)); } }
private Exception parametersMethodReturnedWrongType() throws Exception { String className = getTestClass().getName(); String methodName = getParametersMethod().getName(); String message = MessageFormat.format( "{0}.{1}() must return an Iterable of arrays.", className, methodName); return new Exception(message); }
private Object[] computeParams(ClassLoader classLoader) throws Exception { // Robolectric uses a different class loader when running the tests, so the parameters objects // created by the test runner are not compatible with the parameters required by the test. // Instead, we compute the parameters within the test's class loader. try { List<Object[]> parametersList = getParametersList(getTestClass(), classLoader); if (parametersIndex >= parametersList.size()) { throw new Exception( "Re-computing the parameter list returned a different number of " + "parameters values. Is the data() method of your test non-deterministic?"); } return parametersList.get(parametersIndex); } catch (ClassCastException e) { throw new Exception( String.format( "%s.%s() must return a Collection of arrays.", getTestClass().getName(), name)); } catch (Exception exception) { throw exception; } catch (Throwable throwable) { throw new Exception(throwable); } }
private static FrameworkMethod getParametersMethod(TestClass testClass) throws Exception { List<FrameworkMethod> methods = testClass .getAnnotatedMethods(Parameters.class); for (FrameworkMethod each : methods) { if (each.isStatic() && each.isPublic()) { return each; } } throw new Exception("No public static parameters method on class " + testClass.getName()); }
private Object createTestUsingFieldInjection() throws Exception { List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter(); if (annotatedFieldsByParameter.size() != fParameters.length) { throw new Exception("Wrong number of parameters and @Parameter fields." + " @Parameter fields counted: " + annotatedFieldsByParameter.size() + ", available parameters: " + fParameters.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, fParameters[index]); } catch (IllegalArgumentException iare) { throw new Exception(getTestClass().getName() + ": Trying to set " + field.getName() + " with the value " + fParameters[index] + " that is not the right type (" + fParameters[index].getClass().getSimpleName() + " instead of " + field.getType().getSimpleName() + ").", iare); } } return testClassInstance; }
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(); Parameterized.Parameter annotation = field.getAnnotation(Parameterized.Parameter.class); int index = annotation.value(); try { field.set(testClassInstance, parameters[index]); } 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; }
} catch (IllegalArgumentException iare) { throw new Exception( getTestClass().getName() + ": Trying to set " + field.getName()
throw wrappedException; } catch (IllegalArgumentException iare) { throw new Exception(getTestClass().getName() + ": Trying to set " + field.getName() + " with the value " + parameters[index]
private FrameworkMethod getParametersMethod() throws Exception { List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods( Parameters.class); for (FrameworkMethod each : methods) { if (each.isStatic() && each.isPublic()) { return each; } } throw new Exception("No public static parameters method on class " + getTestClass().getName()); }
private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception { List<FrameworkMethod> methods = testClass.getAnnotatedMethods( Parameterized.Parameters.class ); for ( FrameworkMethod each : methods ) { int modifiers = each.getMethod().getModifiers(); if ( Modifier.isStatic( modifiers ) && Modifier.isPublic( modifiers ) ) { return each; } } throw new Exception( "No public static parameters method on class " + testClass.getName() ); }
private List<FrameworkMethod> getParametersMethods() throws Exception { List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods( Parameterized.Parameters.class); SortedMap<Integer, FrameworkMethod> sortedMethods = new TreeMap<Integer, FrameworkMethod>(); for (FrameworkMethod each : methods) { if (each.isPublic()) { if (!each.isStatic()) { if (getTestClass().getOnlyConstructor().getParameterCount() != 0) { throw new Exception("Method " + each.getMethod() + " is annotated with @Parameters, it is not static and there is no parameter-less constructor!"); } } Order order = each.getAnnotation(Order.class); int value = order == null ? 0 : order.value(); FrameworkMethod prev = sortedMethods.put(value, each); if (prev != null) { throw new Exception(String.format("There are more methods annotated with @Parameters and @Order(value=%d): %s (%s) and %s (%s)", value, prev.getMethod(), prev.getAnnotation(Order.class), each.getMethod(), order)); } } else { throw new Exception("Method " + each.getMethod() + " is annotated with @Parameters but it is not public!"); } } if (sortedMethods.isEmpty()) { throw new Exception("No public static parameters method on class " + getTestClass().getName()); } return new ArrayList<FrameworkMethod>(sortedMethods.values()); }
private Object[] computeParams() throws Exception { try { return fParameterList.get( fParameterSetNumber ); } catch (ClassCastException e) { throw new Exception( String.format( "%s.%s() must return a Collection of arrays.", getTestClass().getName(), getParametersMethod( getTestClass() ).getName() ) ); } }