简体   繁体   中英

Mocking IdempotentConsumer

I'm testing my app, that sends files from ftp to my computer and I'm checking not to copy one file twice. For this i use IdempotentConsumer .
My problem is that i can't run test, because this IdempotentConsumer always gives me error.
How can this be fixed?

Class

@Component
public class Converter extends SpringRouteBuilder {

@Override
public void configure() throws Exception {

final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setTypeHints(String.valueOf("YES"));

from("ftp://Mike@localhost?" +
        "noop=true&binary=true&consumer.delay=5s&include=.*xml")
        .idempotentConsumer(header("CamelFileName"), FileIdempotentRepository.fileIdempotentRepository(new File("data", "repo.dat")))
        .marshal(xmlJsonFormat).to("file://data").process(
        new Processor() {
            //System.out.println();
        }
    });
   }
}    

Testing class

public class ConverterTest extends CamelTestSupport {

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new Converter();
}

@Test
public void testAdvisedMockEndpoints() throws Exception {
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
    @Override
    public void configure() throws Exception {
        replaceFromWith("direct:inputXML");
        interceptSendToEndpoint("file://data")
                .skipSendToOriginalEndpoint()
                .to("mock:outXML");
    }
});        

    context.start();
    getMockEndpoint("mock:outXML").expectedMessageCount(1);        
    template.sendBody("direct:inputXML", "Test data");        
    assertMockEndpointsSatisfied();
}
}

I'm getting the following error:

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: Test data]
Caused by: org.apache.camel.processor.idempotent.NoMessageIdException: No message ID could be found using expression: header(CamelFileName) on message exchange: Exchange[Message: Test data]

If you start the context normaly CamelFileName header is added by the ftp component. But since your are injecting the message using direct in the test this might not be the case.

Try to adapt your test by adding the header manually with .setHeader("CamelFileName", constant("msg01.txt")) .

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