javaspring-bootgradleapache-camelcamel-test

Apache Camel - DirectConsumerNotAvailableException: No consumers available on endpoint. Exchange[]


Someone that can help my with this error and explain me what is wrong?

I'm developing a simple camel route to process an information and then store it in a file.

Technologies that I'm using:

When I want to test the camel route I got the following error.

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[]

    at org.apache.camel.CamelExecutionException.wrapCamelExecutionException(CamelExecutionException.java:47)
    at org.apache.camel.support.ExchangeHelper.extractResultBody(ExchangeHelper.java:687)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:568)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:564)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:189)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:430)
    at com.facturacionx.ms.invoice.camel.CamelTest.testMethod(CamelTest.java:40)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:54)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://my-route. Exchange[]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:59)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:212)
    at org.apache.camel.processor.SharedCamelInternalProcessor$1.process(SharedCamelInternalProcessor.java:110)
    at org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:107)
    at org.apache.camel.impl.engine.DefaultProducerCache.send(DefaultProducerCache.java:185)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.send(DefaultProducerTemplate.java:176)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.send(DefaultProducerTemplate.java:172)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.send(DefaultProducerTemplate.java:153)
    at org.apache.camel.impl.engine.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:187)
    ... 39 more

This is my route

@Component
public class XmlRoute extends RouteBuilder {

    @Autowired
    private MyProcessor myProcessor;

    private JaxbDataFormat jaxb = new JaxbDataFormat(true);

    @Override
    public void configure() throws Exception {

        from("direct:my-route")
                .process(myProcessor)
                .marshal(jaxb)
                .log(LoggingLevel.INFO, "${body}")
                .setHeader(Exchange.CONTENT_TYPE, constant(MediaType.TEXT_XML_VALUE))
                .setHeader(Exchange.FILE_NAME, constant("output.xml"))
            .to("file:outputFolder");
    }
}

This my test case

@RunWith(CamelSpringBootRunner.class)
@ContextConfiguration
public class CamelTest extends CamelTestSupport {

    @Produce("direct:my-route")
    protected ProducerTemplate template;

    @EndpointInject("mock:file:outputFolder")
    protected MockEndpoint mock;

    @DirtiesContext
    @Test
    public void testMethod() throws InterruptedException, NullPointerException, JsonProcessingException {
        // Arrange
        ObjectMapper objectMapper = new ObjectMapper();

        mock.expectedBodyReceived();
        Invoice invoice = InvoiceTestDataBuilder.anInvoice().build();

        // Act
        template.sendBody(objectMapper.writeValueAsString(invoice));

        // Assert
        mock.assertIsSatisfied();
    }
}

This my gradle file

/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    id 'org.springframework.boot' version '2.3.0.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'org.unbroken-dome.xjc' version '1.4.3'
}

repositories {
    mavenCentral()
}

xjcGenerate {
    source = fileTree('src/main/resources/XSD/maindoc') { include '*.xsd' }
}


dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation 'org.springframework.boot:spring-boot-starter-undertow'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

    // Camel
    implementation 'org.apache.camel.springboot:camel-spring-boot-starter:3.3.0'
    implementation 'org.apache.camel.springboot:camel-jaxb-starter:3.3.0'
    implementation 'org.apache.camel.springboot:camel-zipfile-starter:3.3.0'
    implementation 'org.apache.camel.springboot:camel-direct:3.3.0'

    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'

    implementation 'com.github.joschi.jackson:jackson-datatype-threetenbp:2.6.4'
    implementation 'javax.validation:validation-api:1.1.0.Final'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:1.5.22.RELEASE'
    testImplementation 'org.apache.camel:camel-test-spring:3.3.0'

}

group = 'com.ejemplo.ms'
version = '1.0.0'
description = 'swagger-spring'
sourceCompatibility = '11'
targetCompatibility = '11'


tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

Solution

  • I found the solution,

    I add two annotation class: @SpringBootTest and @DirtiesContext

    Also I added a property: @Autowired CamelContext camelContext; And I added another property, the route: @InjectMocks XmlRoute xmlRoute;

    @SpringBootTest
    @RunWith(CamelSpringBootRunner.class)
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
    public class CamelTest {
    
        @Autowired
        private CamelContext camelContext;
    
        @Produce(value = "direct:my-route")
        protected ProducerTemplate template;
    
        @EndpointInject("mock:file:outputFolder")
        protected MockEndpoint mock;
    
        @InjectMocks
        protected XmlRoute xmlRoute;
    
        @Autowired
        private ObjectMapper objectMapper;
    
        @Test
        public void testMethod() throws InterruptedException, NullPointerException, JsonProcessingException {
            // Arrange
    
            mock.expectedBodyReceived();
            Invoice invoice = InvoiceTestDataBuilder.anInvoice().build();
    
            // Act
            template.sendBody(invoice);
    
            // Assert
            mock.assertIsNotSatisfied();
        }
    }
    

    With these changes, the test work well.

    thanks all of you for your help.