This is an experiment around a simple way to express and execute different branches in a single JUnit test method. The implementation supports dead end branches (that cut execution at their end), and merge back branches (that continue after the branch point).
I don't really no if this might useful at all. This came to my mind because of continuations (which it is not) and for defining some sorts of user interaction tests, that tend to have much flux ramification. I still have to try this, though.
This is implemented in a rather simple way by BranchRule, a JUnit method rule.
Branches are defined by passing Runnable
arguments to the rule methods branch
and branchAndMerge
. They can be nested and are visited exactly once, in depth-first order.
The iteration is done by executing the method just as many times as needed to visit each branch. Each execution respects the normal JUnit decorations, including @Before
, @After
and other @Rule
s.
- You've got to pass a new
Runnable
for eachbranch()
call. Don't reuse instances. - You should not nest a
branch()
call inside a loop: it would be visited just in the first pass.
In the following, the test method will be executed twice:
-
first entering the branch and terminating
-
then ignoring it and reaching the method's end.
public class BranchRuleTest {
@Rule public BranchRule brancher = new BranchRule(); @Test public void simpleBranch() throws Exception { System.out.println("restart"); brancher.branch(new Runnable() { public void run() { System.out.println("BRANCH-END"); } }); System.out.println("NORMAL-END"); }
}
It will print
restart
BRANCH-END
restart
NORMAL-END
If we change, in the previous example, the call from
brancher.branch(new Runnable() { ...
to
brancher.branchAndMerge(new Runnable() { ...
the execution of the branch continues after the Runnable
block is done.
It will then print
restart
BRANCH-END
NORMAL-END
restart
NORMAL-END
Now both executions reach the method's end.
Check the project tests for more examples.
This a Maven Project. Install it the usual way.
Please file issues in the standard Issues Section of the project.
All the work is licensed under Apache 2.0 license, available at http://www.apache.org/licenses/LICENSE-2.0.