Search in sources :

Example 11 with RootBeanDefinition

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));
    }
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition)

Example 12 with RootBeanDefinition

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;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition)

Example 13 with RootBeanDefinition

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();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 14 with RootBeanDefinition

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;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 15 with RootBeanDefinition

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;
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition)

Aggregations

RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)696 Test (org.junit.Test)521 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)308 TestBean (org.springframework.tests.sample.beans.TestBean)159 ITestBean (org.springframework.tests.sample.beans.ITestBean)143 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)133 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)84 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)79 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)77 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)74 Element (org.w3c.dom.Element)47 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)46 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)46 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)36 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)31 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)30 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)30 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)28 Properties (java.util.Properties)26 ManagedList (org.springframework.beans.factory.support.ManagedList)25