I am trying to write unit tests for my Spring Boot with camel routes application. My Producer for the route is a Kafka Endpoint and the URI is defined in the application.properties file. I am trying to mock this Kafka producer endpoint, but it is not resolving the URI value.
@RunWith(SpringRunner.class)
@SpringBootTest
@MockEndpointsAndSkip("kafka:${kafka.servers}?*")
public class MyApplicationTests {
@Autowired
private CamelContext camelContext;
........
//Some debug code
// list available endpoints
camelContext.getEndpoints().forEach((Endpoint endpoint) -> log.info("Endpoint="+endpoint));
.............
}
In the above pseduocode, the kafka.servers value to @MockEndpointsAndSkip annotation is defined in application.properties file as kafka.servers=localhost:9092
When I print list of endpoints, I see the unresolved value Endpoint=mock://kafka:$%7Bkafka.servers%7D
I would expect: Endpoint=mock://kafka:localhost:9092
Is this the wrong way of doing it?
Thanx in advance for any help!!
Issue with Mock Testing (sorry am a camel newbie)
Here is my Camel routes that I want to unit test using MockEndpoints:
from("kafka:{{kafka.servers}}?topic={{kafka.consumer.topic}}&groupId={{kafka.consumer.groupId}}")
.to("direct:notify");
from("direct:notify")
.log("direct:notfy");
Here is my Test code which is failing the assertion of receiving 1 Exchange. It appears that the Producer is never sending the message to my Kafka Mockendpoint:
@RunWith(SpringRunner.class)
@SpringBootTest
@MockEndpointsAndSkip("kafka:{{kafka.servers}}?*|direct:notify")
public class NotificationApplicationTests {
@Autowired
private CamelContext context;
@Value("${kafka.servers}")
private String kafkaServers;
@EndpointInject(uri="mock:kafka:{{kafka.servers}}")
private MockEndpoint mockKafka;
@EndpointInject(uri="mock:direct:notify")
private MockEndpoint mockDirect;
@Autowired
private ProducerTemplate notiProducer;
@Test
public void testMockEndpoints() throws Exception{
mockDirect.setExpectedMessageCount(1);
mockDirect.whenAnyExchangeReceived( (Exchange exchange) -> {
//NEVER GETS HERE!!
log.info("MOCK DIRECT exchange received");
});
String payload = "{'email': '[email protected]', 'data': 'ABC1234'}";
notiProducer.sendBody(mockKafka, payload);
mockDirect.assertIsSatisfied();
}
}