use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-boot by spring-projects.
the class MockitoPostProcessor method registerMock.
private void registerMock(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry, MockDefinition definition, Field field) {
RootBeanDefinition beanDefinition = createBeanDefinition(definition);
String beanName = getBeanName(beanFactory, registry, definition, beanDefinition);
String transformedBeanName = BeanFactoryUtils.transformedBeanName(beanName);
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(1, beanName);
if (registry.containsBeanDefinition(transformedBeanName)) {
registry.removeBeanDefinition(transformedBeanName);
}
registry.registerBeanDefinition(transformedBeanName, beanDefinition);
Object mock = createMock(definition, beanName);
beanFactory.registerSingleton(transformedBeanName, mock);
this.mockitoBeans.add(mock);
this.beanNameRegistry.put(definition, beanName);
if (field != null) {
this.fieldRegistry.put(field, new RegisteredField(definition, beanName));
}
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-boot by spring-projects.
the class MockitoPostProcessor method createBeanDefinition.
private RootBeanDefinition createBeanDefinition(MockDefinition mockDefinition) {
RootBeanDefinition definition = new RootBeanDefinition(mockDefinition.getTypeToMock().resolve());
definition.setTargetType(mockDefinition.getTypeToMock());
definition.setFactoryBeanName(BEAN_NAME);
definition.setFactoryMethodName("createMock");
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, mockDefinition);
if (mockDefinition.getQualifier() != null) {
mockDefinition.getQualifier().applyTo(definition);
}
return definition;
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-boot by spring-projects.
the class MockitoPostProcessorTests method canMockBeanProducedByFactoryBeanWithObjectTypeAttribute.
@Test
public void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
factoryBeanDefinition.setAttribute("factoryBeanObjectType", SomeInterface.class.getName());
context.registerBeanDefinition("beanToBeMocked", factoryBeanDefinition);
context.register(MockedFactoryBean.class);
context.refresh();
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class ConfigBeanDefinitionParser method createAdviceDefinition.
/**
* Creates the RootBeanDefinition for a POJO advice bean. Also causes pointcut
* parsing to occur so that the pointcut may be associate with the advice bean.
* This same pointcut is also configured as the pointcut for the enclosing
* Advisor definition using the supplied MutablePropertyValues.
*/
private AbstractBeanDefinition createAdviceDefinition(Element adviceElement, ParserContext parserContext, String aspectName, int order, RootBeanDefinition methodDef, RootBeanDefinition aspectFactoryDef, List<BeanDefinition> beanDefinitions, List<BeanReference> beanReferences) {
RootBeanDefinition adviceDefinition = new RootBeanDefinition(getAdviceClass(adviceElement, parserContext));
adviceDefinition.setSource(parserContext.extractSource(adviceElement));
adviceDefinition.getPropertyValues().add(ASPECT_NAME_PROPERTY, aspectName);
adviceDefinition.getPropertyValues().add(DECLARATION_ORDER_PROPERTY, order);
if (adviceElement.hasAttribute(RETURNING)) {
adviceDefinition.getPropertyValues().add(RETURNING_PROPERTY, adviceElement.getAttribute(RETURNING));
}
if (adviceElement.hasAttribute(THROWING)) {
adviceDefinition.getPropertyValues().add(THROWING_PROPERTY, adviceElement.getAttribute(THROWING));
}
if (adviceElement.hasAttribute(ARG_NAMES)) {
adviceDefinition.getPropertyValues().add(ARG_NAMES_PROPERTY, adviceElement.getAttribute(ARG_NAMES));
}
ConstructorArgumentValues cav = adviceDefinition.getConstructorArgumentValues();
cav.addIndexedArgumentValue(METHOD_INDEX, methodDef);
Object pointcut = parsePointcutProperty(adviceElement, parserContext);
if (pointcut instanceof BeanDefinition) {
cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcut);
beanDefinitions.add((BeanDefinition) pointcut);
} else if (pointcut instanceof String) {
RuntimeBeanReference pointcutRef = new RuntimeBeanReference((String) pointcut);
cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcutRef);
beanReferences.add(pointcutRef);
}
cav.addIndexedArgumentValue(ASPECT_INSTANCE_FACTORY_INDEX, aspectFactoryDef);
return adviceDefinition;
}
use of org.springframework.beans.factory.support.RootBeanDefinition in project spring-framework by spring-projects.
the class ConfigBeanDefinitionParser method createPointcutDefinition.
/**
* Creates a {@link BeanDefinition} for the {@link AspectJExpressionPointcut} class using
* the supplied pointcut expression.
*/
protected AbstractBeanDefinition createPointcutDefinition(String expression) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(AspectJExpressionPointcut.class);
beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
beanDefinition.setSynthetic(true);
beanDefinition.getPropertyValues().add(EXPRESSION, expression);
return beanDefinition;
}
Aggregations