Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext setAllowBeanDefinitionOverriding

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext setAllowBeanDefinitionOverriding

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext setAllowBeanDefinitionOverriding.

Prototype

public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) 

Source Link

Document

Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former.

Usage

From source file:com.fitbur.jestify.junit.spring.IntegrationTestRunner.java

@Override
protected Object createTest() throws Exception {
    try {//from  w ww  .j a  v  a 2 s  .  c o m
        TestClass testClass = getTestClass();
        Class<?> javaClass = testClass.getJavaClass();
        String name = javaClass.getSimpleName();

        TestDescriptor testDescriptor = testClassContexts.computeIfAbsent(javaClass, p -> {
            Object instance;
            try {
                instance = super.createTest();
                TestContext context = new TestContext(name, javaClass, instance);
                Set<Field> candidatesFields = of(javaClass.getDeclaredFields()).parallel()
                        .filter(f -> f.isAnnotationPresent(Cut.class)).collect(toSet());

                if (candidatesFields.isEmpty()) {
                    checkState(false, "Class under test not defined in %s. "
                            + "Please annotated a single field with @Cut.", name);
                } else if (candidatesFields.size() != 1) {
                    checkState(false, "Found more than one class under test in %s. "
                            + "Please annotated only a single field with @Cut.", name);
                }

                Field cutField = candidatesFields.iterator().next();
                ClassReader testReader = new ClassReader(javaClass.getName());
                ClassReader cutReader = new ClassReader(cutField.getType().getName());

                testReader.accept(new TesAnalyzer(context), EXPAND_FRAMES);
                cutReader.accept(new CutAnalyzer(context), EXPAND_FRAMES);

                return new TestDescriptor(instance, context);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
        appContext.setAllowBeanDefinitionOverriding(true);
        appContext.setId(name);
        appContext.setDisplayName(name);

        Object instance = testDescriptor.getInstance();
        TestContext context = testDescriptor.getContext();
        IntegrationTestReifier reifier = new IntegrationTestReifier(appContext, instance);
        IntegrationTestCreator integrationTestCreator = new IntegrationTestCreator(context, reifier,
                appContext);
        integrationTestCreator.create();

        applicationContexts.put(javaClass, appContext);
        return instance;
    } catch (IllegalStateException e) {
        notifier.pleaseStop();
        throw e;
    }
}

From source file:com.fitbur.testify.integration.SpringIntegrationTest.java

@Override
protected Statement methodBlock(FrameworkMethod method) {
    TestClass testClass = getTestClass();
    Class<?> javaClass = testClass.getJavaClass();

    Object testInstance;//ww w  .j av a  2s. co m

    try {
        testInstance = createTest();
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }

    TestContext testContext = getTestContext(javaClass);
    testContext.setTestInstance(testInstance);
    String testClassName = testContext.getTestClassName();

    AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
    appContext.setId(testClassName);
    appContext.setAllowBeanDefinitionOverriding(true);
    appContext.setAllowCircularReferences(false);

    serviceLocator = new SpringServiceLocator(appContext, serviceAnnotations);

    methodTestNeeds = new TestNeeds(testContext, method.getName(), NeedScope.METHOD);
    methodTestNeeds.init();

    methodTestNeedContainers = new TestNeedContainers(testContext, method.getName(), NeedScope.METHOD);
    methodTestNeedContainers.init();

    TestCaseInstance testCaseInstance = new TestCaseInstance(method.getName(), testInstance);
    serviceLocator.addConstant(testCaseInstance.getTestName(), testCaseInstance);

    SpringServicePostProcessor postProcessor = new SpringServicePostProcessor(serviceLocator, methodTestNeeds,
            methodTestNeedContainers, classTestNeeds, classTestNeedContainers);

    appContext.addBeanFactoryPostProcessor(postProcessor);

    IntegrationTestReifier reifier = new IntegrationTestReifier(testContext, serviceLocator, testInstance);
    IntegrationTestCreator creator = new IntegrationTestCreator(testContext, reifier, serviceLocator);

    if (testContext.getCutDescriptor() != null) {
        creator.cut();
    }

    Set<FieldDescriptor> real = testContext.getFieldDescriptors().values().parallelStream()
            .filter(p -> !p.getInstance().isPresent())
            .filter(p -> p.hasAnnotations(serviceAnnotations.getInjectors())).collect(toSet());

    creator.real(real);

    IntegrationTestVerifier verifier = new IntegrationTestVerifier(testContext, LOGGER);
    verifier.wiring();

    Statement statement = methodInvoker(method, testInstance);
    statement = possiblyExpectingExceptions(method, testInstance, statement);
    statement = withBefores(method, testInstance, statement);
    statement = withAfters(method, testInstance, statement);
    statement = withRules(method, testInstance, statement);

    return statement;
}