Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

unit testing - How Do I Test A Camel Route That Reads From A Directory?

I am creating a Camel application that reads files dropped into a directory on an FTP server, converts them and uploads to a REST API.

If I create a route that reads a file like this:

    from("file://target/input?delay=5s")
      .choice()
      .when(header(Exchange.FILE_NAME).endsWith(".xml"))
        .unmarshal(jaxb)

The target/input directory has to exist which then makes testing a bit awkward as my test resources might not be in the correct directory.

In my test it would be easier to just get the contents of the test file and put it into the unmarshal part of the route

    from("direct:xml")
      .unmarshal(jaxb)

How do I make my Route flexible enough to test without just copying the whole route to a test class and modifying the input component?

question from:https://stackoverflow.com/questions/65904733/how-do-i-test-a-camel-route-that-reads-from-a-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply make your from endpoint configurable and configure it through application properties.

For example if you use Spring:

@Value("${endpoint.consumer}")
private String consumerEndpoint;
...
from(consumerEndpoint)
...

You can then provide a test configuration that configures the endpoint as direct:input and use a ProducerTemplate to send test messages with different payloads to this endpoint.

Real application properties:

endpoint.consumer=file://target/input?delay=5s

Test properties:

endpoint.consumer=direct:input

Like this you get rid of any file endpoints in your tests.

If you also got to endpoints, you can make them configurable too. You can turn them into mocks in your tests by configure them as mock:whatever.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...