I was working recently on a annotation based retry mechanism. The idea is simple: when a method invocation fails, it should retry the call based on some retry policy. Sample code:
I won’t dive into details about the aspect handling the retry mechanism. Notice that the destroySession method is annotated with @Retryable annotation which is provided with the spring bean id retry.default representing the retry policy.
The interesting question is how could I unit test this? How to prove that the implementation of my code indeed works as intended?
At the first glance, the only approach seems to be an integration tests. But suppose that ServiceBroker has many heavy dependencies which might be hard to configure. In the same time, all you want is to test the retry mechanism only, without worrying about anything else.
The solution is a combination of integration & unit test. Instead of loading a spring application context with all beans configured, the following trick can be applied:
The above code shows how the unit test is set up. Instead of using the manually instantiated ServiceBroker, we use the one initialized by spring using AutowireCapableBeanFactory. This trick, allows to unit test the proxy of the ServiceBroker, instead the ServiceBroker itself. Thus we can test the retry mechanism itself (continuation of ServiceBrokerTest):
The above unit test, asserts that the remoteService was invoked expected number of times when it fails.
Conclusion
The above example shows an approach of easily unit testing aspects in a spring based application.
comments powered by Disqus