JUnit Plan Runner
Starting with version 3.14, step includes a JUnit runner that can be used to seamlessly integrate step plans into JUnit tests.
Sample output
Here is an example of what the output of using this feature may look like.
The sections below refer to the structure and files shown in this screenshot. Note that the code examples are intentionally as short and specific as possible, as they are only meant to showcase the syntax and usage.
Maven dependency
<dependency>
<groupId>ch.exense.step</groupId>
<artifactId>step-junit</artifactId>
<version>3.14.0</version>
</dependency>
Inline plan definitions
Here is a simple example which uses the step JUnit runner to run a plan defined directly inside the Test file:
package com.example.step.junit.ex1;
import org.junit.runner.RunWith;
import step.junit.runner.Step;
import step.junit.runners.annotations.Plan;
@RunWith(Step.class) // (1)
public class ExampleTest1 {
@Plan("Sequence\nEcho \"OK\"\nEnd") // (2)
public void inlinePlan() {
}
}
Notice how (1) we are using the @RunWith(Step.class) annotation on the class to define the JUnit runner used for executing the entire Test class, and (2) how the actual plan to be executed is declared in the @Plan annotation of the test method.
Selective inclusion of plans, and parameterization
While inlining plan definitions as shown above is a quick and easy way to run plans as JUnit test cases, it is really only suitable for very short definitions.
Therefore, the step JUnit runner features a mechanism to automatically create test cases when the following conditions are met:
- The plan is defined in the @Plans annotations
- The plan definitions are located in the
resources
subdirectory matching the package of the JUnit test.
The test class below shows how to include specific (external) plans, and how to define execution parameters:
package com.example.step.junit.ex2;
import org.junit.runner.RunWith;
import step.junit.runner.Step;
import step.junit.runners.annotations.ExecutionParameters;
import step.junit.runners.annotations.Plans;
@RunWith(Step.class)
@Plans({"keyword_and_params.plan"})
@ExecutionParameters({"INPUT_PARAMETER", "Example"})
public class ExampleTest2 {
}
… and this is the content of the keyword_and_params.plan:
Sequence
Echo "${INPUT_PARAMETER} exists"
CustomKeyword
End
This plan will only execute correctly if:
- the parameter
INPUT_PARAMETER
is defined (using the @ExecutionParameters annotation) - the
CustomKeyword
keyword is found in the same package as the plan and Test class.
For completeness, here is the trivial implementation of the keyword:
package com.example.step.junit.ex2;
import step.handlers.javahandler.AbstractKeyword;
import step.handlers.javahandler.Keyword;
public class Keywords extends AbstractKeyword {
@Keyword
public void CustomKeyword() {
System.out.println("CustomKeyword called.");
}
}