简体   繁体   中英

Apache Camel with Spring boot unit test setup

I am trying to get a sample setup doing a unit test on a very basic route and all the entries I've tried so far do not get me the CamelContext to be auto wired.

I have this for my route and following is the unit test.

public class SampleRouter1 extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start") // a real entry will be jms:queue:testqueue
            .log("Direct Start Init")
            .process(new SampleProcessor1())
            .to("mock:output");
    }
}

unit test

@RunWith(CamelSpringBootRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
public class SampleRouter1Test1 {
    @Autowired
    protected CamelContext camelContext; // never gets wired in

edited to add that i am autowiring the context in UT.

Exception on running unit test is: qualifying bean of type 'org.apache.camel.CamelContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} qualifying bean of type 'org.apache.camel.CamelContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

also question is, is this meant to test the SampleRouter or do you just unit test the process class and any other supporting classes? or do you use the following to change it so you can pass a message to the fake direct queue?

AdviceWith.adviceWith(camelContext, "jms:queue:testqueue", a -> { a.replaceFromWith("direct:start"); });

So with the link that was suggested to me i got the unit testing working with the following:

Spring boot 2.4.2 / Apache Camel 3.7.2

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-spring-junit5</artifactId>
            <version>3.7.2</version>
        </dependency>

Unit Testing annotations i needed:

@CamelSpringBootTest
@SpringBootTest // (webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource
// TODO: Might need the dirty context here . I have not ran into that issue just yet. 
class SampleRouter1Test {

    @Autowired
    private CamelContext camelContext;

    @BeforeEach
    public void setUp() throws Exception {
//        This is to allow you to test JMS queues by replacing their JMS:Queue: entry
//        Also the "Test Router 1" is what ever you named your router via .id().
//        AdviceWith.adviceWith(camelContext, "Test Router 1", a -> {
//            a.replaceFromWith("direct:start2");
//        });

    }

    @Produce("direct:start")
    private ProducerTemplate template;

    @EndpointInject("mock:output")
    private MockEndpoint mockDone;

    @Test
    public void t1() throws Exception {
        template.sendBodyAndHeaders("Testing", new HashMap<>());

        mockDone.expectedMessageCount(1);
    }
}

I didn't need anything else to autowire spring beans with the above. Also noticed if you have more JMS Routes, you will probably need to change their entries to keep from having to enable ActiveMQ (or which ever jms client).

Sample Router:

@Component
public class SampleRouter1 extends RouteBuilder {

    @Autowired
    private SampleProcessor1 sampleProcessor1;

    @Override
    public void configure() throws Exception {
        from("direct:start")
                .id("Test Router 1")
                .log("Direct Start Init")
                .process(sampleProcessor1)
                .to("mock:output");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM