I have this method that creates Consumer<TransformerEndpointSpec>
public static Consumer<TransformerEndpointSpec> buildTransformerSpec() {
return transformerEndpointSpec -> transformerEndpointSpec
.id(TRANSFORMER_NAME)
.transformer(Transformers.fromJson(ArtemisKpisMessageDto.class));
}
I would like to do a unit test for it, to test the TransformerEndpointSpec
id and transformer(). I would like to do this:
@Test
public void testBuildTransformer() {
final Consumer<TransformerEndpointSpec> consumer = TransformerUtil.buildTransformerSpec();
consumer.accept(new TransformerEndpointSpec()); // <=================== Here is my problem
}
But I’m unable to find a solution to get an instance of a TransformerEndpointSpec.
I found a simple solution by creating a static class that extends TransformerEndpointSpec
and exposes a constructor:
@Test
public void buildTransformerSpecTest() {
final Consumer<TransformerEndpointSpec> consumer = TransformerUtil.buildTransformerSpec();
final MyTransformerEndpointSpec t = new MyTransformerEndpointSpec();
consumer.accept(t);
Assertions.assertEquals("myTransform", t.getId());
}
private static class MyTransformerEndpointSpec extends TransformerEndpointSpec { }
The question and goal of that unit test does not make sense for the target project. The TransformerEndpointSpec
is a framework component which is really tested there in the library project.
For the target project it is better to test the final solution where all the Java DSL components are wired together. So, you create an IntegrationFlow
bean where you would use a transformWith()
API, set those options on the spec. And then you verify in the test that there is an endpoint bean with that TRANSFORMER_NAME
and there is a MessageTransformingHandler
with a bean name TRANSFORMER_NAME.handler
where its transformer
property is an instance of JsonToObjectTransformer
with a targetType
of that ArtemisKpisMessageDto
. Those props you can reach only via reflection, e.g. we provide TestUtils.getPropertyValue()
API. However this still feels like a framework responsibility to test this API and its behavior. This really has nothing to do with the logic of the target project.
Perhaps your requirements more complex, than you show right now and you really have some custom TransformerEndpointSpec
you would like to use in your code, e.g. via IntegrationFlowExtension
. Then yes, you would need to test that class since it belongs to your project. But that’s different story and we would need more clarification for your use-case to justify possible testing approach.