org.apache.camel.component.mock.MockEndpoint.expectedMessagesMatches(Predicate...) is implemented in such a way that, if the test at hand is run with an assert period, then an assertion will be run once before the assert period, but will then be run twice more (when it only needs to run once more) after the assertion period is up.
This is because the run() method of the org.apache.camel.component.mock.AssertionClause created in expectedMessagesMatches(Predicate...) calls AssertionClause.addPredicate(Predicate), which has not been implemented as an idempotent function.
Therefore, when the assertion is run before the assert period, the Predicate is added to the AssertionClause once, and when the assertion is run again after the assert period, the same Predicate is added again to the same AssertionClause, and so it is executed one more time than necessary.
This can be fixed by making the method AssertionClause.addPredicate(Predicate) idempotent. Suggestions for doing so are:
- to change the type of org.apache.camel.component.mock.AssertionClause.predicates from List<Predicate> to Set<Predicate> (java.util.LinkedHashSet will preserve insertion order), or
- to add a containment check in AssertionClause.addPredicate(Predicate), before adding the Predicate to the list.
I am not sure whether this will work with equality checking of anonymous Predicate classes with themselves.