1 Reply Latest reply on Dec 3, 2013 11:22 AM by ksimpson

    How to use external Karaf properties in local unit tests

    ksimpson Newbie

      Attempting to follow the paradigm described here:  https://access.redhat.com/site/documentation/en-US/Fuse_ESB_Enterprise/7.1/html/Configuring_and_Running_Fuse_ESB_Enterprise/files/ESBRuntimeCfgIntro.html

       

      Successfully deployed application into JBoss Fuse with following configuration:

        <!-- OSGi/Karaf properties configuration -->

        <osgix:cm-properties id="regjinOsgiConfig" persistent-id="com.obsglobal.partners.portland.regjin">

        <prop key="foo">testBar</prop>

        <prop key="regjin.broker.url">vm://someTestHost:61617</prop>

        </osgix:cm-properties>

       

        <!-- Inject the OSGi properties -->

        <ctx:property-placeholder properties-ref="regjinOsgiConfig"/>


        <!-- Other beans defined... -->


      <!-- Define CamelContext and build routes -->

        <camel:camelContext id="portland-esb-context">

             <camel:propertyPlaceholder id="camelRegjinOsgiConfig" location="ref:regjinOsgiConfig"/>

          

             <!-- install the Java DSL route builder(s) -->

             <camel:package>com.obsglobal.partners.portland.routes</camel:package>

             <camel:routeBuilder ref="ErrorHandlingRouter"/>

        </camel:camelContext>

       

      Properties defined in $FUSE_HOME/etc/com.obsglobal.partners.portland.regjin.cfg are resolved successfully.  However, when running Camel unit tests, they all fail with the stacktrace at bottom. 

       

      What is correct way to override Karaf properties configuration in unit tests extending CamelSpringTestSupport?

       

      The stacktrace:

      Error creating bean with name 'portland-esb-context': Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

              at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

              at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)

              at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)

              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

              at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:420)

              at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:947)

              at org.springframework.context.support.AbstractApplicationContext.registerListeners(AbstractApplicationContext.java:701)

              at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:377)

              at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)

              at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

              at com.obsglobal.partners.portland.routes.BaseRouteTester.createApplicationContext(BaseRouteTester.java:124)

              at org.apache.camel.test.junit4.CamelSpringTestSupport.doPreSetup(CamelSpringTestSupport.java:78)

              at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:216)

              at com.obsglobal.partners.portland.routes.BaseRouteTester.setUp(BaseRouteTester.java:92)

              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

              at java.lang.reflect.Method.invoke(Method.java:606)

              at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

              at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

              at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

              at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)

              at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)

              at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:48)

              at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)

              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)

              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)

              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

              at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)

              at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

              at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)

              at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)

              at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)

              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

              at java.lang.reflect.Method.invoke(Method.java:606)

              at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)

              at com.sun.proxy.$Proxy0.invoke(Unknown Source)

              at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)

              at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)

              at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

      Caused by: java.lang.reflect.MalformedParameterizedTypeException

              at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(ParameterizedTypeImpl.java:60)

              at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(ParameterizedTypeImpl.java:53)

              at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(ParameterizedTypeImpl.java:95)

              at sun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(CoreReflectionFactory.java:105)

              at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:140)

              at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)

              at sun.reflect.generics.repository.ClassRepository.getSuperInterfaces(ClassRepository.java:100)

              at java.lang.Class.getGenericInterfaces(Class.java:814)

              at com.sun.beans.TypeResolver.prepare(TypeResolver.java:289)

              at com.sun.beans.TypeResolver.<init>(TypeResolver.java:254)

              at com.sun.beans.TypeResolver.getTypeResolver(TypeResolver.java:235)

              at com.sun.beans.TypeResolver.resolve(TypeResolver.java:172)

              at com.sun.beans.TypeResolver.resolveInClass(TypeResolver.java:93)

              at java.beans.FeatureDescriptor.getParameterTypes(FeatureDescriptor.java:387)

              at java.beans.MethodDescriptor.setMethod(MethodDescriptor.java:114)

              at java.beans.MethodDescriptor.<init>(MethodDescriptor.java:72)

              at java.beans.MethodDescriptor.<init>(MethodDescriptor.java:56)

              at java.beans.Introspector.getTargetMethodInfo(Introspector.java:1130)

              at java.beans.Introspector.getBeanInfo(Introspector.java:414)

              at java.beans.Introspector.getBeanInfo(Introspector.java:161)

              at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:224)

              at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:149)

              at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:324)

              at org.springframework.beans.BeanWrapperImpl.getPropertyDescriptorInternal(BeanWrapperImpl.java:354)

              at org.springframework.beans.BeanWrapperImpl.isWritableProperty(BeanWrapperImpl.java:430)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1362)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)

              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)

              ... 47 more

        • 1. Re: How to use external Karaf properties in local unit tests
          ksimpson Newbie

          SOLUTION:

           

          1. Split application context (AppCtx) into two files:
            1. OSGi property injection, which imports the second
            2. Application beans
          2. Create AppCtx for unit tests, which:
            1. Declares a bridge property configurer (see snippet below)
            2. Imports the application beans AppCtx
          3. Load test AppCtx in base class for unit tests

           

          NOTE: Properties loaded programmatically (e.g. add locations to PropertiesComponent) cannot be refreshed in $FUSE_HOME/etc file.

           

          <bean id="regjinOsgiConfig" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">

            <property name="location" value="classpath:com.obsglobal.partners.portland.regjin.cfg"/>

          </bean>