Wednesday, May 4, 2011

New tool added - CallbackParams

On top of being yet-another-parameterized-testing-framework CallbackParams separates itself through at least three defining features ...

The framework takes care of combining the parameter-values. This might look like a cheap way to generate many tests with little code but as can be seen here and here the great advantage is the improved maintainability that is released when the burden of combining parameter-values is eliminated.

The combined parameter-values are not accessed directly but through a callback-interface by invoking any of its callback-methods. An invocation of a callback-method works as a composite invocation on those parameter-values that offer an implementation of the corresponding callback-interface.

This means that parameterized data is always accessed indirectly through callback-interfaces. (With traditional parameterization the parameterized data is usually primitive and accessed directly - each parameter-value separately.)

More pedagogic examples on how this actually works can be found in the tutorial article Patterns That Simplify Maintenance.

The preferred way to run CallbackParams tests is to annotate the test-class:

import org.callbackparams.junit4.CallbackParamsRunnerimport org.junit.runner.RunWith@RunWith(CallbackParamsRunner.class)public class MyParameterizedTest {

CallbackParamsRunner is an implementation of Runner, which is implemented by many other JUnit Add-Ons out there. Almost all of these runners assume you choose between different ~end-to-end~ runners for your test, i.e. a test can be run with prominent runners such as PowerMockRunner or SpringJUnit4ClassRunner - but not both at the same time!!!

CallbackParamsRunner, however, is much less instrusive, because it only concerns itself with test-parameterization and leaves the actual test-execution to some other suitable runner, which can be explicitly specified with the annotation @org.callbackparams.junit4.WrappedRunner:

@RunWith(CallbackParamsRunner.class)@WrappedRunner(PowerMockRunner.class)public class MyParameterizedPowerMockTest {

... or ...

@RunWith(CallbackParamsRunner.class)@WrappedRunner(SpringJUnit4ClassRunner.class)public class MyParameterizedSpringTest {

This approach to test-parameterization can be described in terms of AOP. JUnit's annotation @RunWith exposes a pointcut. The class CallbackParamsRunner acts as an advice for this pointcut by making the necessary modifications to the test-class and then delegates test-execution back to JUnit's built-in Runner implementation or to the specified third-party implementation, which in turn can be regarded as the next advice for the pointcut.

No comments:

Post a Comment