-1

I have the following Spring route:

 <camelContext xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="activemq:topic:inbox" />
            <choice>
                <when>
                    <simple>${in.header.Value}</simple>
                    <log message="Cc: ${in.header.Value}" />
                </when>
            </choice>
            <to uri="mock:result" />
        </route>
 </camelContext>

I have the requirement to use Spring Testing (CamelSpringJUnit4ClassRunner), although I have found easy to understand examples on how to test the condition with Java DSL. My Test Class is like this:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration(locations = "file:src/main/resources/META-INF/spring/camel-context-activemq-embedded.xml")
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)

public class MyTest{

    private Logger LOG = LogManager.getLogger(MyTest.class.getName());

    protected Exchange exchange;
    protected CustomComponent customComponent= new CustomComponent();

    @Produce(uri = "activemq:topic:inbox")
    protected ProducerTemplate template;

    @EndpointInject(uri = "mock:result")
    protected MockEndpoint resultEndpoint;

@Test
    public void tes1() throws InterruptedException {
        String headerValue= MyComponent.Value;
        EmailAddress recipients = new EmailAddress("recipient@example.com");
        template.sendBodyAndHeader("activemq:topic:inbox", recipients.toString(), headerValue);
        resultEndpoint.expectedBodiesReceived(headerValue);
        resultEndpoint.expectedHeaderReceived("header value", headerValue);
        resultEndpoint.expectedMessageCount(1);
    }

I am struggling to understand how to test the actual condition dictated by the CBR, but more importantly I am doubting whether this is even the right way to test it. MyComponent.VALUEConstant is a property specified in my custom component and the above test is actually passing. However, if I instantiated the headerValue with a different property on my component and therefore the condition is supposed to fail, the test passes. Can you please help?

Thank you,

I

4

1 回答 1

1

Well, the first thing that I can see is that your simple expression lacks a comparison - it should probably be ${in.header.value} == 'wanted value'.

As far as testing is concerned - that really depends on the type of test. Here, you're doing integration testing so I'd verify that effects are as expected - DB changed the way it should etc. But since your route is only doing some logging then I'd change:

<log message="Cc: ${in.header.Value}" />

into

<log message="Cc: ${in.header.Value}" />
<to uri="mock:choice-triggered" />

and then verify that mock:choice-triggered endpoint received the message (or didn't, depending on the scenario). But in a test of a real-world route you'd probably want to verify that some data has been inserted into the DB or some message published to the MQ or that an email has been sent.

As far as your constant is concerned, I'd advise you to use externalized properties - both Camel and Spring have excellent support for property placeholders, I suggest you give them a try.

于 2016-11-14T16:03:39.920 回答