[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

@DirtiesContext not resetting counters


I believe I have found a bug with the @DirtiesContext cleanup of the CamelContext.

I have tried every permutation of all the Spring test annotations and the result was always the same

A NullPointerException on getRouteDefinition(String routeId)

(The camel test is extremely fragmented across versions, examples and deprecations by the way. It can be hugely time consuming to get a test running from scratch using spring, due to all the permutations of attempts online. It’d be lovely if the doc page was updated. But I digress).

A NullPointerException!

Really, I should have realised what the NPE meant quicker, rather than bashing my head against the screen trying to get the right test configuration, when that (i have concluded) isn't the problem.

THE ISSUE: The counters aren’t reset in the CamelContext. I found this problem when two separate test classes interfered with each other.

My solution:


public void setup() throws Exception {

//I found this class with some digging around


context.getRouteDefinition("route2").adviceWith(context, new AdviceWithRouteBuilder() {


public void configure() throws Exception { }



I think this reset command needs to be added somewhere to a SpringTestExecutionListener (or whichever class ties into the DirtiesContext cleanup somewhere



Second issue: The @MockEndpoints annotation and the @UseAdviceWith result duplicate work when injecting a mock, so you get 2 mocks instead of one, and you don’t get a handle on the one receiving the traffic. It’s necessary to use a pattern to exclude the adviced mockendpoint. I guess this could be documented somewhere useful, as a solution.


@MockEndpoints(value = "/^myMock$/“)

Public class StopDoubleMockInjection {

@EndpointInject(uri = “mock:myMock")

MockEndpoint output;

@EndpointInject(uri = "mock:output”) // <— Let UseAdviceWith take care of this one.

MockEndpoint output;

context.getRouteDefinition("route3").adviceWith(context, new AdviceWithRouteBuilder() {


public void configure() throws Exception {






Many thanks,


Get Outlook for Android<>