protected void injectMock(Object testObject, Field field, Mock<?> mock) { setFieldValue(testObject, field, mock); callAfterCreateMockMethods(testObject, mock, field.getName()); }
public static void injectIntoAnnotatedFields(Object objectToInject, Object target, Class<? extends Annotation> annotation) { Set<Field> annotatedFields = AnnotationUtils.getFieldsAnnotatedWith(target.getClass(), annotation); for (Field annotatedField : annotatedFields) { setFieldValue(target, annotatedField, objectToInject); } }
/** * All the elements with the @BaseUrlString will be initialised with the base url value of the unitils.properties. * * @param testObject : the testobject. */ public void initBaseUrl(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), BaseUrl.class); for (Field field : fields) { ReflectionUtils.setFieldValue(testObject, field, baseUrl); } }
protected void createTempFileForField(Object testObject, Method testMethod, Field field) { TempFile annotation = field.getAnnotation(TempFile.class); String fileName = annotation.value(); if (fileName.isEmpty()) { fileName = testObject.getClass().getName() + "-" + testMethod.getName() + ".tmp"; } try { File tempFile = tempService.createTempFile(fileName); setFieldValue(testObject, field, tempFile); } catch (Exception e) { throw new UnitilsException("Error creating temp file for field " + field.getName(), e); } }
protected void createTempDirForField(Object testObject, Method testMethod, Field field) { TempDir annotation = field.getAnnotation(TempDir.class); String fileName = annotation.value(); if (fileName.isEmpty()) { fileName = testObject.getClass().getName() + "-" + testMethod.getName(); } try { File f = tempService.createTempDir(fileName); setFieldValue(testObject, field, f); } catch (Exception e) { throw new UnitilsException("Error creating temp dir for field " + field.getName(), e); } }
/** * checks for the {@link Dummy} annotation on the testObject. If so it is created by the DummyObjectUtil. The two aproaches possible are * stuffed or normal depending on the value in the {@link Dummy} annotation. * * @param testObject The tested object not null */ protected void createAndInjectDummiesIntoTest(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), Dummy.class); for (Field field : fields) { Object dummy = createDummy(field.getType()); setFieldValue(testObject, field, dummy); } }
/** * Creates an objects of the given fields' declared type and assigns it to this field on the given testObject * * @param testObject The test instance, not null * @param testedObjectField The tested object field, not null */ protected void createObjectForField(Object testObject, Field testedObjectField) { Class<?> declaredClass = testedObjectField.getType(); if (declaredClass.isInterface()) { logger.warn("Field " + testedObjectField.getName() + " (annotated with @TestedObject) has type " + testedObjectField.getType().getSimpleName() + " which is an interface type. It is not automatically instantiated."); } else if (isAbstract(declaredClass.getModifiers())) { logger.warn("Field " + testedObjectField.getName() + " (annotated with @TestedObject) has type " + testedObjectField.getDeclaringClass().getSimpleName() + " which is an abstract class. It is not automatically instantiated."); } else { try { declaredClass.getDeclaredConstructor(); Object instance = createInstanceOfType(declaredClass, true); setFieldValue(testObject, testedObjectField, instance); } catch (NoSuchMethodException e) { logger.warn("Field " + testedObjectField.getName() + " (annotated with @TestedObject) has type " + testedObjectField.getDeclaringClass().getSimpleName() + " which has no default (parameterless) constructor. It is not automatically instantiated."); } } }
/** * This will initialize a new SMTP server and the method will put it in the correct field (SimpleSmtpServer) of the testedObject. * * @param testObject of type {@link Object} */ public void initSmtpServer(Object testObject) { Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestSmtpServer.class); if (fields.size() > 1) { throw new UnitilsException("Multiple SmtpServer declarations found. Module support only one. "); } if (fields.size() == 1) { Field field = fields.iterator().next(); ReflectionUtils.setFieldValue(testObject, field, new SmtpServerImpl(port)); } }
/** * * Searches all the fields with the {@link WebPage} annotation and sets the correct elements. * * @param testObject : the testobject. */ public void initElements(Object testObject) { // find fields that has the @WebPage annotation Set<Field> fields = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), WebPage.class); // find the webdriver Set<Field> webdrivers = AnnotationUtils.getFieldsAnnotatedWith(testObject.getClass(), TestWebDriver.class); if (!webdrivers.isEmpty()) { // initialise the page and set the object in the correct field. WebDriver webdriver = ReflectionUtils.getFieldValue(testObject, webdrivers.iterator().next()); for (Field field : fields) { if (webdriver != null) { ReflectionUtils.setFieldValue(testObject, field, getElement(webdriver, field.getType())); } } } else { LOGGER.error("The TestWebDriver cannot be found."); } }
/** * Does the actual content reading and injection for the given field. * * @param testObject The test instance, not null * @param field The field with the FileContent annotation, not null */ protected void readFileContentForField(Object testObject, Field field) { FileContent fileContentAnnotation = field.getAnnotation(FileContent.class); String encoding = determineEncoding(fileContentAnnotation); String fileName = determineFileName(fileContentAnnotation); Class<?> targetType = field.getType(); Class<?> testClass = testObject.getClass(); try { Object result = fileContentReader.readFileContent(fileName, targetType, encoding, testClass); setFieldValue(testObject, field, result); } catch (Exception e) { throw new UnitilsException("Error reading file content for field " + field.getName(), e); } }
ReflectionUtils.setFieldValue(testObject, field, driver);
String tempDatabaseName = StringUtils.isEmpty(annotation.value()) ? databaseConfigurations.getDatabaseConfiguration().getDatabaseName() : annotation.value(); if (annotation != null && tempDatabaseName.equals(databaseName)) { ReflectionUtils.setFieldValue(testObject, field, dataSource);
/** * This is the actual method that creates an object of the correct type and initialises all the elements. * * @param webdriver : should be of type {@link WebDriver} * @param type: the class that should be initialised. * @return {@link Object} * */ protected Object getElement(WebDriver webdriver, Class<?> type) { Object webpage = PageFactory.initElements(webdriver, type); for (Field field : AnnotationUtils.getFieldsAnnotatedWith(type, TestWebDriver.class)) { ReflectionUtils.setFieldValue(webpage, field, webdriver); } initBaseUrl(webpage); return webpage; }
logger.warn("Unable to retrieve current value of field to inject into. Will not be able to restore value after injection.", e); setFieldValue(target, fieldToInjectTo, objectToInject); return oldValue;
/** * Sets the given value on the static property of the given targetClass * * @param targetClass The class on which the static property value should be set * @param staticProperty The name of the property (simple name, not a composite expression) * @param value The value to set */ private static void setValueStatic(Class<?> targetClass, String staticProperty, Object value) { Method staticSetter = ReflectionUtils.getSetter(targetClass, staticProperty, true); if (staticSetter != null) { try { invokeMethod(targetClass, staticSetter, value); } catch (InvocationTargetException e) { throw new UnitilsException("Exception thrown by target", e); } } else { Field staticField = getFieldWithName(targetClass, staticProperty, true); if (staticField == null) { throw new UnitilsException("Static property named " + staticProperty + " not found on class " + targetClass.getSimpleName()); } setFieldValue(targetClass, staticField, value); } } }