Unit Testing Expression based components
Any day I can remove code is a good day so I am excited to start moving our simpler message processors to use expressions instead of java methods. However, I am finding it difficult to unit test them. With the explicit java filters/routers/transformers/etc the unit tests are nicely isolated to the ref'd method with no wiring or mocks. When using expressions instead the expression should still be tested but isolated from the ExpressionEvaluatingFilter/Router/Transformer/etc.
What if the following interface was added
Code:
public interface ExpressionEvaluatingComponent {
Expression getExpression();
}
Then all relevant message processing components could implement this so unit tests like the following could be written.
Code:
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class PriorityRouterTest {
@Autowired
@Qualifier("priorityRouter")
private ExpressionEvaluatingComponent priorityRouter;
@Test
public void testRouter() {
Message<String> lowestPriority = MessageBuilder.withPayload("foo").setPriority(1).build();
...
Message<String> higestPriority = MessageBuilder.withPayload("baz").setPriority(9).build();
Expression expression = priorityRouter.getExpression();
assertEquals("StandardPriorityChannel", expression.getValue(lowestPriority));
assertEquals("HighPriorityChannel", expression.getValue(highestPriority));
}
}
Is there an easier way? I dont see any way to get the expression otherwise except by using an xpath against the config xml.