Resolved: Why is my Camel JUnit test case producer template not able to send a body?


I am trying to test a route which is like this:
.process(exchange -> {exchange.getIn().setHeader(Exchange.FILE_NAME,MY_FILE_NAME);})
.log("File download Successful")
.to("file:" + FILE_PATH).routeId("mys3Route");
I have written my test like this:
public void testFileMovement() throws Exception {
    AdviceWith.adviceWith(context, "mys3Route", a -> {
        a.interceptSendToEndpoint("file:" + FILE_PATH).skipSendToOriginalEndpoint()
    MockEndpoint mockedToEndPoint = getMockEndpoint("mock:anotherLocation");
    template.sendBody("mock:s3Location", "Just Text");
Whenever I run this as unit test case, I get this error:

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

The error seems to be coming up here in: org.apache.camel.impl.engine.DefaultProducerTemplate.extractResultBody( (which is present in camel dependencies).
Any idea as to what I am doing wrong and how I can rectify it? Any help to resolve and understand this issue is greatly appreciated .


For starters you probably should not replace consumer/From endpoint with MockEndpoint just use direct endpoint. MockEndpoints only support producer endpoints (to) and should not be used as consumer endpoint (from). MockEndpoints are meant to be used as points on your route where you want to do assertions on things like message body, exchange properties, received messages etc.
Secondly if you’re using AdviceWith you should set the isUseAdviceWith to true and start the context manually just before you use template.send methods. How this is set varies a bit if you’re using spring boot annotations or not. Example below uses just simple CamelTestSupport.
Thirdly you rarely if ever need to use intercept on camel tests, use weaveById, weaveByToURI with replace instead. In this case you’re better off just fixing how your file-path and file-name is set by using property-placeholders instead. This way you can just use useOverridePropertiesWithPropertiesComponent and TemporaryFolder feature of junit. Also Apache-commons IO FileUtils is pretty handy if you need to read file-contents of a test file or copy something to a test folder.
Using Thread.Sleep with unit tests is hacky at best and should be avoided. For this case I see no reason why you would use it. RouteId is best placed at the top of the route.
package com.example;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class ExampleTest extends CamelTestSupport {
    public TemporaryFolder temporaryFolder = new TemporaryFolder();
    File outputFolder;

    static final String FILE_NAME = "testfile.txt";

    public void testFileMovement() throws Exception {

            .adviceWith(context, new AdviceWithRouteBuilder(){

            public void configure() throws Exception {


        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:result");

        template.sendBody("direct:start", "Just Text");

        File file = new File(outputFolder, FILE_NAME);
        assertEquals(true, file.exists());
        String fileContents = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
        assertEquals("Just Text", fileContents);


    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder(){

            public void configure() throws Exception {
                    .log("File download Successful")

    protected Properties useOverridePropertiesWithPropertiesComponent() {

        try {
            outputFolder = temporaryFolder.newFolder("output");
        } catch (IOException e) {

        Properties properties = new Properties();
        properties.put("filename", FILE_NAME);
        properties.put("filepath", outputFolder.getPath());

        return properties;

    public boolean isUseAdviceWith() {
        return true;

If you have better answer, please add a comment about this, thank you!