There's a DTO created on a builder method (assume it has only a "name" attribute with some random value) and I want to check if the returned object has the same value, but the response is null.
Unit test code:
class GetProductByIdRouteBuilderTest extends CamelTestSupport {
@Test
void testRoute() throws Exception {
var expected = DTOBuilder.getProductDetailsDTO();
getMockEndpoint("mock:result").expectedMessageCount(1);
getMockEndpoint("mock:result").expectedHeaderReceived("id", 1);
var response = template.requestBodyAndHeader(
"direct:getProductById",
null,
"id",
1L,
GetProductDetailsDTO.class
);
assertMockEndpointsSatisfied();
assertEquals(expected.getName(), response.getName());
}
@Override
protected RoutesBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:getProductById")
.routeId("getProductById")
.log("id = ${header.id}")
.to("mock:result")
.end();
}
};
}
}
Solution
Used the whenAnyExchangeReceived method:
getMockEndpoint("mock:result").whenAnyExchangeReceived(exchange -> {
exchange.getMessage().setBody(expected);
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.OK.value());
});
It sounds like a totally expected behavior since the body of the original message is
nulland your route doesn't transform it such that the result of your route isnull.Let's describe your code step by step to better understand:
The next code snippet, sends a message to the Consumer endpoint
direct:getProductByIdwithnullas body and1Las value of the headeridand tries to convert the resulting body into an object of typeGetProductDetailsDTO.Your route simply logs the value of the header
idand sends the message received by the consumer endpointdirect:getProductByIdas is to the producer endpointmock:result.So knowing that, we can see that your route will actually end with a message with
nullas body, and since Camel cannot convertnullto an object of typeGetProductDetailsDTO, you end up withnullas result of your route.I guess that your route is incomplete as it should somehow query your database.