1

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)}

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"); });
sherring
  • 121
  • 1
  • 1
  • 11
  • What happens if you add the `@Autowired` annotation to the `camelContext` field in the `SampleRouter1Test1` class? – Chin Huang Feb 05 '21 at 01:31
  • so i do have that ``` @Autowired public CamelContext camelContext; ``` but its always null or it yells that it can't figure out how to autowire it – sherring Feb 05 '21 at 03:46
  • Does this answer your question? [Spring Boot Apache Camel Routes testing](https://stackoverflow.com/questions/42275732/spring-boot-apache-camel-routes-testing) – Roman Vottner Feb 05 '21 at 07:40
  • Hi Roman, it doesn't at least not fully. i tried using the CamelAutoConfiguration but it doesnt seem to want to auto wire that object. and from what im seeing UT is re-building the route, which isn't exactly what id like to do. id like to send to the route and let the code flow that way vs redoing it in a test class – sherring Feb 05 '21 at 22:30

1 Answers1

1

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");
    }

}
sherring
  • 121
  • 1
  • 1
  • 11