@Test public void propertyPlaceholderLocation() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-location.xml", getClass()); assertEquals("bar", applicationContext.getBean("foo")); assertEquals("foo", applicationContext.getBean("bar")); assertEquals("maps", applicationContext.getBean("spam")); }
@Test public void jaxb2ContextPathMarshaller() { Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ContextPathMarshaller", Jaxb2Marshaller.class); assertNotNull(jaxb2Marshaller); }
@Test public void testBeanDefinitionRegistryPostProcessorConfig() { ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDefinitionRegistryPostProcessorConfig.class); assertTrue(ctx.getBean("myTestBean") instanceof TestBean); }
@Test @SuppressWarnings("resource") public void testWithDependencyChecking() { ApplicationContext ctx = new ClassPathXmlApplicationContext(DEPENDENCY_CHECK_CONTEXT, getClass()); ctx.getBean("testBean"); }
private void testMetaClass(String xmlFile) { // expect the exception we threw in the custom metaclass to show it got invoked try { ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlFile); Calculator calc = (Calculator) ctx.getBean("delegatingCalculator"); calc.add(1, 2); fail("expected IllegalStateException"); } catch (IllegalStateException ex) { assertEquals("Gotcha", ex.getMessage()); } }
@Before public void onSetUp() throws Exception { ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect")); ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(injectedTestBean)); this.testBeanProxy = injectedTestBean; // we need the real target too, not just the proxy... this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); mockCollaborator = mock(AroundAdviceBindingCollaborator.class); aroundAdviceAspect.setCollaborator(mockCollaborator); }
@Before public void setup() { this.context = new ClassPathXmlApplicationContext( "scheduledTasksContext.xml", ScheduledTasksBeanDefinitionParserTests.class); this.registrar = this.context.getBeansOfType( ScheduledTaskRegistrar.class).values().iterator().next(); this.testBean = this.context.getBean("testBean"); }
@Test public void propertyPlaceholder() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-replace.xml", getClass()); assertEquals("bar", applicationContext.getBean("string")); assertEquals("null", applicationContext.getBean("nullString")); }
@Test public void jaxb2ClassesToBeBoundMarshaller() { Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ClassesMarshaller", Jaxb2Marshaller.class); assertNotNull(jaxb2Marshaller); }
@Test public void testBothBeansAreProxies() { Object tb = ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(tb)); Object tb2 = ctx.getBean("testBean2"); assertTrue(AopUtils.isAopProxy(tb2)); }
@Test public void testWithTwoClassesDefinedInTheOneGroovyFile_WrongClassFirst() throws Exception { try { ApplicationContext ctx = new ClassPathXmlApplicationContext("twoClassesWrongOneFirst.xml", getClass()); ctx.getBean("messenger", Messenger.class); fail("Must have failed: two classes defined in GroovyScriptFactory source, non-Messenger class defined first."); } // just testing for failure here, hence catching Exception... catch (Exception expected) { } }
@Test public void propertyPlaceholderIgnored() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "contextNamespaceHandlerTests-replace-ignore.xml", getClass()); assertEquals("${bar}", applicationContext.getBean("string")); assertEquals("null", applicationContext.getBean("nullString")); }
/** * Tests that Configuration classes are registered according to convention * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator#generateBeanName */ @Test public void defaultConfigClassBeanNameIsGeneratedProperly() { ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); // attempt to retrieve the instance by its generated bean name Config configObject = (Config) context.getBean("annotationConfigApplicationContextTests.Config"); assertNotNull(configObject); }
@Test public void withNonStaticBeanMethodAndInterface() { MyBeanImpl.initialized = false; ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class); MyBean bean = ctx.getBean("myBean", MyBean.class); assertFalse(MyBeanImpl.initialized); bean.doIt(); assertTrue(MyBeanImpl.initialized); }
@Test public void appliesAspectToClassWithComplexConstructor() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("ObjenesisProxyTests-context.xml", getClass()); ClassWithComplexConstructor bean = context.getBean(ClassWithComplexConstructor.class); bean.method(); DebugInterceptor interceptor = context.getBean(DebugInterceptor.class); assertThat(interceptor.getCount(), is(1L)); assertThat(bean.getDependency().getValue(), is(1)); }
@Test public void testRefreshableFactoryBean() { ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass()); Object factory = context.getBean("&refreshableFactory"); assertTrue(factory instanceof FactoryBean); Object result = context.getBean("refreshableFactory"); assertTrue(result instanceof String); assertEquals("test", result); }
/** * Tests that specifying @Configuration(value="foo") results in registering * the configuration class with bean name 'foo'. */ @Test public void explicitConfigClassBeanNameIsRespected() { ApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithCustomName.class); // attempt to retrieve the instance by its specified name ConfigWithCustomName configObject = (ConfigWithCustomName) context.getBean("customConfigBeanName"); assertNotNull(configObject); }
@Test public void withNonStaticBeanMethod() { MyBeanImpl.initialized = false; ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class); MyBean bean = ctx.getBean("myBean", MyBean.class); assertFalse(MyBeanImpl.initialized); bean.doIt(); assertTrue(MyBeanImpl.initialized); }
@Test public void testFactoryBean() { ApplicationContext context = new ClassPathXmlApplicationContext("groovyContext.xml", getClass()); Object factory = context.getBean("&factory"); assertTrue(factory instanceof FactoryBean); Object result = context.getBean("factory"); assertTrue(result instanceof String); assertEquals("test", result); }
@Test public void test() { ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, Bar.class); assertNotNull(ctx.getBean(Bar.class).foo); }