You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by yuncil <jo...@hotmail.de> on 2015/11/19 11:46:56 UTC

How to get properties working in camelTest and spring

I have a problem with the property-mechanism in JUnit-Tests. In my
application I just have to annotade my config-class to use property
placeholders.

@Configuration
@PropertySource("classpath:my.properties")
public class MyServiceConfigurator extends WsConfigurerAdapter {

...

}

Afterwards I can use properties this way: "{{someProperty}}", pretty cool
and simple.

I tried the same in my Junit-Test-Class.

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyServiceConfigurator.class,
MyServiceTests.TestConfig.class}, loader =
CamelSpringDelegatingTestContextLoader.class)
@MockEndpointsAndSkip(value = "spring-ws:*")
@PropertySource("classpath:my.properties")
public class MyServiceTests {

...

}

But this annotation hasn't any effects there. So I end up doing this:

//static class within the Junits-test-class
    @Configuration
    @TestPropertySource("classpath:my.properties")
    public static class TestConfig extends SingleRouteCamelConfiguration {
        
        @Bean
        @Override
        public RouteBuilder route() {
            return new MyServiceRoutes();
        }
        
        @Bean
        public static PropertySourcesPlaceholderConfigurer configurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }

Now I can use my properties but not with the known syntax: "{{myProperty}}"
but with "${myProperty}". Consequently I must change the code everytime I
want to use the JunitTests.

Has someone a simple explanation how to use properties within a Camel,
Spring-Boot, Webservice-Project, especially within the JunitTest-classes.

My project setup:

Camel 2.16.0
Spring-Boot 1.2.7

Thanks in advance

best regards





--
View this message in context: http://camel.465427.n5.nabble.com/How-to-get-properties-working-in-camelTest-and-spring-tp5774102.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to get properties working in camelTest and spring

Posted by yuncil <jo...@hotmail.de>.
I finally got my JUnit-Tests working and the properties are correctly
injected now. I did it with a lot of annotations:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyServiceTests.class)
@ActiveProfiles("test")
@EnableAutoConfiguration
@ComponentScan
@MockEndpointsAndSkip(value = "spring-ws:*")
public class MyServiceTests extends TestCase {

...

}

Also I added the following dependency:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.2.7.RELEASE</version>
</dependency>

Furthermore the property-file must be in the classpath and must named
application-test.properties. With this configuration I needn't the static
inner class TestConfig anymore. The solution is described here:
http://tech.willhaben.at/2015/10/testing-apache-camel-applications-with.html

Nevertheless I would like to log a JIRA ticket and I'm looking forward to a
camel-test-spring module, for a more lean solution.

Thanks for your effort, Claus.



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-get-properties-working-in-camelTest-and-spring-tp5774102p5774298.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to get properties working in camelTest and spring

Posted by Claus Ibsen <cl...@gmail.com>.
Can you log a JIRA ticket so we can improve testing camel-spring-boot.
We may end up having to do a camel-test-spring-boot module etc. There
has been a few other reports on some testing stuff that don't work as
well when you use a bunch of spring annotations etc.

You can find a link to jira here
http://camel.apache.org/support.html

On Thu, Nov 19, 2015 at 11:46 AM, yuncil <jo...@hotmail.de> wrote:
> I have a problem with the property-mechanism in JUnit-Tests. In my
> application I just have to annotade my config-class to use property
> placeholders.
>
> @Configuration
> @PropertySource("classpath:my.properties")
> public class MyServiceConfigurator extends WsConfigurerAdapter {
>
> ...
>
> }
>
> Afterwards I can use properties this way: "{{someProperty}}", pretty cool
> and simple.
>
> I tried the same in my Junit-Test-Class.
>
> @RunWith(CamelSpringJUnit4ClassRunner.class)
> @ContextConfiguration(classes = {MyServiceConfigurator.class,
> MyServiceTests.TestConfig.class}, loader =
> CamelSpringDelegatingTestContextLoader.class)
> @MockEndpointsAndSkip(value = "spring-ws:*")
> @PropertySource("classpath:my.properties")
> public class MyServiceTests {
>
> ...
>
> }
>
> But this annotation hasn't any effects there. So I end up doing this:
>
> //static class within the Junits-test-class
>     @Configuration
>     @TestPropertySource("classpath:my.properties")
>     public static class TestConfig extends SingleRouteCamelConfiguration {
>
>         @Bean
>         @Override
>         public RouteBuilder route() {
>             return new MyServiceRoutes();
>         }
>
>         @Bean
>         public static PropertySourcesPlaceholderConfigurer configurer() {
>             return new PropertySourcesPlaceholderConfigurer();
>         }
>     }
>
> Now I can use my properties but not with the known syntax: "{{myProperty}}"
> but with "${myProperty}". Consequently I must change the code everytime I
> want to use the JunitTests.
>
> Has someone a simple explanation how to use properties within a Camel,
> Spring-Boot, Webservice-Project, especially within the JunitTest-classes.
>
> My project setup:
>
> Camel 2.16.0
> Spring-Boot 1.2.7
>
> Thanks in advance
>
> best regards
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-get-properties-working-in-camelTest-and-spring-tp5774102.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2