0

I have configured apache camel redelivery policy to one the camel routes. I need to write junit's to verify if the route is working properly in both success and failure scenarios.

I was able to write junit for the failure scenario and was able to verify that the maximum redelivery happened as per the configuration by mocking an service in the route to return an exception.

In positive scenario I want to mock a service in the route to return an exception on the first 3 attempts and on the last redelivery attempt (4) I want the mock service to return expected value without any exception. (3 attempts failed, but on 4th attempt it was success)

I did bit a research on this and couldn't find any solution on this, is there any way we can get hold of camel redelivery instance and change the mocks at run time during the test execution ?

Rohit
  • 647
  • 14
  • 30

1 Answers1

0

An easy solution, which is a pure Java one (I mean not involving Camel at all), would be to use the well-known ThreadLocal pattern (https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html).

Briefly explained, the TheadLocal pattern allows you to store data that will be accessible only by a particular thread.

In your particular case, the mock implementation of your service could store a (thread-bounded) counter being incremented on each query. Based on current counter value, your service could return an exception for value 1 to 3, and normal response for value 4.

TacheDeChoco
  • 3,683
  • 1
  • 14
  • 17