Hi Camel Support
All endpoints are not always mocked with Camel 3.22.2 and after.
If you write a JUnit test using:
@CamelSpringBootTest
@ShutdownTimeout(5)
@SpringBootTest
@ExtendWith(SpringExtension.class)
@MockEndpoints("*")
@Transactional
@UseAdviceWith
containing some advices and a custom RouteBuilder having an
interceptSendToEndpoint.
Something like this:
public abstract class BaseRoute extends RouteBuilder {
public BaseRoute() {
this.interceptSendToEndpoint("*").process((exchange) ->
System.out.println("Intercept send to"));
}
}
then MockEndpoint.assertIsSatisfied(context)fails most of the time –
occasionally it succeeds.
The reason is, that alle the endpoints are not mocked.
The problem might be related to the changes in AbstractCamelContext between
version 3.22.2 and 3.22.1:
@Override
public void registerEndpointCallback(EndpointStrategy strategy) {
if (!endpointStrategies.contains(strategy)) {
// let it be invoked for already registered endpoints so it can
// catch-up.
endpointStrategies.add(strategy);
has been changed to:
@Override
public void registerEndpointCallback(EndpointStrategy strategy) {
if (endpointStrategies.add(strategy)) {
// let it be invoked for already registered endpoints so it can
// catch-up.
I have attached the files to BaseRoute.java, MyRoute.java and
CamelAdviceMockTest.java.
It should be possible to reproduce the problem with these files.
The same test works with Camel version 3.10.0
The same test fails with Camel version 4.10.4
Best regards,
Allan Petersen
package com.example.demo1.route;
import org.apache.camel.builder.RouteBuilder;
public abstract class BaseRoute extends RouteBuilder {
public BaseRoute() {
this.interceptSendToEndpoint("*").process((exchange) -> System.out.println("Intercept send to"));
}
}
package com.example.demo1.route;
import org.springframework.stereotype.Component;
@Component
public class MyRoute extends BaseRoute {
public static final int NUMBER_OF_ROUTES = 5;
@Override
public void configure() {
for (int i = 0; i < NUMBER_OF_ROUTES; i++) {
from("direct:route_" + i)
.routeId("route_" + i + "_id")
.startupOrder(i)
.log("to_" + i + "_id").id("to_" + i + "_id")
.end();
}
}
}
package com.example.demo1;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.MockEndpoints;
import org.apache.camel.test.spring.junit5.ShutdownTimeout;
import org.apache.camel.test.spring.junit5.UseAdviceWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import static com.example.demo1.route.MyRoute.NUMBER_OF_ROUTES;
@CamelSpringBootTest
@ShutdownTimeout(5)
@SpringBootTest
@ExtendWith(SpringExtension.class)
@MockEndpoints("*")
@Transactional
@UseAdviceWith
public class CamelAdviceMockTest {
public static final String MOCK_XXX = "mock:xxx";
@Autowired
private CamelContext context;
@Autowired
private ProducerTemplate template;
private Exchange exchange;
@BeforeEach
public void setUp() throws Exception {
advices(context);
if (context.isStopped()) {
context.start();
}
exchange = new DefaultExchange(context);
}
@Test
public void testMock() throws Exception {
for (int i = 0; i < NUMBER_OF_ROUTES; i++) {
final String send2Endpoint = "direct:route_" + i;
final MockEndpoint mockEndpoint = getMock(send2Endpoint);
mockEndpoint.setExpectedCount(1);
template.send(send2Endpoint, exchange);
}
MockEndpoint.assertIsSatisfied(context);
}
protected MockEndpoint getMock(final String endpoint) {
return context.getEndpoint("mock:" + endpoint, MockEndpoint.class);
}
public static void advices(final CamelContext context) throws Exception {
for (int i = 0; i < NUMBER_OF_ROUTES; i++) {
final String toId = "to_" + i + "_id";
int efi = i;
AdviceWith.adviceWith(context, "route_" + i + "_id", a -> {
a.weaveById(toId).replace().to(MOCK_XXX + efi).id(toId);
});
}
}
}