Java Code Examples for org.junit.runners.model.FrameworkField#getField()
The following examples show how to use
org.junit.runners.model.FrameworkField#getField() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtextParametrizedRunner.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Implements behavior from: org.junit.runners.Parameterized$TestClassRunnerForParameters */ 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().getConstructor().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; }
Example 2
Source File: ParameterStatement.java From neodymium-library with MIT License | 6 votes |
private void injectTestParameter() throws Exception { int parameterFieldCount = statementData.getParameterFrameworkFields().size(); Object[] parameter = statementData.getParameter(); if (parameterFieldCount != parameter.length) { throw new Exception("Number of parameters (" + parameter.length + ") and " + // "fields (" + parameterFieldCount + ") " + // "annotated with @Parameter must match!"); } for (FrameworkField parameterFrameworkField : statementData.getParameterFrameworkFields()) { Field field = parameterFrameworkField.getField(); int parameterIndex = field.getAnnotation(Parameter.class).value(); LOGGER.debug("Set parameter \"" + parameterFrameworkField.getName() + "\" to \"" + parameter[parameterIndex] + "\""); setField(field, parameter[parameterIndex]); } }
Example 3
Source File: BaseRunner.java From baratine with GNU General Public License v2.0 | 6 votes |
public List<T> getInjectionTestPoints() throws IllegalAccessException { List<T> result = new ArrayList<>(); List<FrameworkField> fields = getTestClass().getAnnotatedFields(); final MethodHandles.Lookup lookup = MethodHandles.lookup(); for (FrameworkField field : fields) { Field javaField = field.getField(); javaField.setAccessible(true); MethodHandle setter = lookup.unreflectSetter(javaField); T ip = createInjectionPoint(javaField.getType(), javaField.getAnnotations(), setter); result.add(ip); } return result; }
Example 4
Source File: WildflyTestRunner.java From wildfly-core with GNU Lesser General Public License v2.1 | 6 votes |
private void doInject(TestClass klass, Object instance) { try { for (FrameworkField frameworkField : klass.getAnnotatedFields(Inject.class)) { Field field = frameworkField.getField(); if ((instance == null && Modifier.isStatic(field.getModifiers()) || instance != null)) {//we want to do injection even on static fields before test run, so we make sure that client is correct for current state of server field.setAccessible(true); if (field.getType() == ManagementClient.class && controller.isStarted()) { field.set(instance, controller.getClient()); } else if (field.getType() == ModelControllerClient.class && controller.isStarted()) { field.set(instance, controller.getClient().getControllerClient()); } else if (field.getType() == ServerController.class) { field.set(instance, controller); } } } } catch (Exception e) { throw new RuntimeException("Failed to inject", e); } }
Example 5
Source File: FrameworkMethodWithParameters.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public Object invokeExplosively(final Object target, final Object... params) throws Throwable { if (!parameters_.isEmpty()) { final List<FrameworkField> annotatedFieldsByParameter = testClass_.getAnnotatedFields(Parameter.class); if (annotatedFieldsByParameter.size() != parameters_.size()) { throw new Exception( "Wrong number of parameters and @Parameter fields." + " @Parameter fields counted: " + annotatedFieldsByParameter.size() + ", available parameters: " + parameters_.size() + "."); } for (final FrameworkField each : annotatedFieldsByParameter) { final Field field = each.getField(); final Parameter annotation = field.getAnnotation(Parameter.class); final int index = annotation.value(); try { field.set(target, parameters_.get(index)); } catch (final IllegalArgumentException iare) { throw new Exception(testClass_.getName() + ": Trying to set " + field.getName() + " with the value " + parameters_.get(index) + " that is not the right type (" + parameters_.get(index).getClass().getSimpleName() + " instead of " + field.getType().getSimpleName() + ").", iare); } } } return super.invokeExplosively(target, params); }
Example 6
Source File: CustomParameterizedBlockJUnit4Runner.java From registry with Apache License 2.0 | 5 votes |
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 (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; }
Example 7
Source File: SharedBehaviorTesting.java From FreeBuilder with Apache License 2.0 | 5 votes |
public SharedBehaviorTesting( Function<RunNotifier, Statement> superChildrenInvoker, BiConsumer<FrameworkMethod, RunNotifier> superChildRunner, TestSupplier superCreateTest, Supplier<Description> superDescription, Supplier<TestClass> testClass, BiFunction<Class<?>, String, Description> descriptionFactory, FeatureSet features) throws InitializationError { this.superChildrenInvoker = superChildrenInvoker; this.superChildRunner = superChildRunner; this.superCreateTest = superCreateTest; this.superDescription = superDescription; this.features = features; List<FrameworkField> testerFields = testClass.get().getAnnotatedFields(Shared.class); if (testerFields.isEmpty()) { throw new InitializationError("No public @Shared field found"); } else if (testerFields.size() > 1) { throw new InitializationError("Multiple public @Shared fields found"); } FrameworkField frameworkField = getOnlyElement(testerFields); if (!frameworkField.isPublic()) { throw new InitializationError("@Shared field " + frameworkField + " must be public"); } if (!frameworkField.getType().isAssignableFrom(BehaviorTester.class)) { throw new InitializationError(String.format( "@Shared field %s must be of type %s", frameworkField, BehaviorTester.class.getSimpleName())); } testerField = frameworkField.getField(); introspection = descriptionFactory.apply(testClass.get().getJavaClass(), "Introspect"); }
Example 8
Source File: VertxUnitRunnerWithParameters.java From vertx-unit with Apache License 2.0 | 5 votes |
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; }