Skip to content

marciomazza/junit-test-branches

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

JUnit Test Branches

Experiment

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).

Purpose

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.

Implementation

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 @Rules.

Limitations

  1. You've got to pass a new Runnable for each branch() call. Don't reuse instances.
  2. You should not nest a branch() call inside a loop: it would be visited just in the first pass.

Examples

Dead end branch

In the following, the test method will be executed twice:

  1. first entering the branch and terminating

  2. 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

Merge back

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.

More examples

Check the project tests for more examples.

Installation

This a Maven Project. Install it the usual way.

Issues

Please file issues in the standard Issues Section of the project.

License

All the work is licensed under Apache 2.0 license, available at http://www.apache.org/licenses/LICENSE-2.0.

About

An experiment on execution branching in JUnit test methods.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages