org.junitext
Annotation Type Prerequisite


@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface Prerequisite

Test cases can be dependent of prerequisites, e.g. be online in Internet, have a database available, have database filled with specific test data, etc.

For these cases it can be very annoying to @Ignore the test cases, because it may be different for each individual run. For this requirement, the @Prerequisite annotation can be used.

Each test case annotated with @Prerequisite will be checked, whether the condition is fulfilled. If not, the test case will be marked as ignored.

For example:

 @Prerequisite(requires="isDBAvailable")
 @Test public void doDBTest() {
   // ...
 }
 

The method called (here: isDBAvailable) must be implemented with following signature:

Some examples:

    public boolean isNonProductionTest(Description desc) {
      // all production tests start with "prod" 
      return !desc.getDisplayName().startsWith("prod");
    }
    
    public boolean isActiveTest(String className, String methodName) {
      // for some reason, SpecialBsinessTest.test100() has to be taken out of test suite
      if (("SpecialBusinessTest".equals(className)) && ("test100".equals(methodName))) {
        return false;
      }
      return true;
    }
    
    public boolean isDBAvailable() {
      // we check, whether DB is available
      boolean available = ...;
      return available;
    }
 

This method can also be provided by a static helper class, when specifiying a callee attribute to annotation.

For example:

    @Prerequisite (requires="isDBAvailable", callee=DBHelper.class)
    @Test public void doDBTest() {
      ...
    }
    public class DBHelper {
      public static boolean isDBAvailable() {
        boolean available = ...;
        return available;
      }
    }
 
TODO: Design question 1: @Prerequisite can also be made an extension to @Ignore e.g. @Ignore (values="Check for database available", when="!isDBAvailable")
TODO: Design question 2: Current implementation handles a not fulfilled prerequisite as specified with @Ignore. Means, you cannot later distinguish between @Ignore and @Prerequisite runs.


Required Element Summary
 java.lang.String requires
          Attribute for the method to be called for the prerequsite.
 
Optional Element Summary
 java.lang.Class<?> callee
          The class to be called.
 

Element Detail

requires

public abstract java.lang.String requires
Attribute for the method to be called for the prerequsite. Will does NOT have a default name.

Returns:
the method name as string

callee

public abstract java.lang.Class<?> callee
The class to be called. The default value NoCallee means, call the test class itself.

Returns:
the class to be called when checking prerequisite
Default:
org.junitext.Prerequisite.NoCallee.class