Concordion > Annotations >

Before and After Hooks

Since: Concordion 2.0.0

Concordion provides hooks to invoke fixture methods before or after an example, specification or suite.

Example hooks

Annotate a fixture method with @BeforeExample to invoke it before each example, or @AfterExample to invoke it after each example.

Optionally, the method can have a String parameter annotated with @ExampleName, which will be supplied with the name of the example. For example:

@BeforeExample
public void setupExample(@ExampleName exampleName) {
}   

We have two examples below. Each example increments an AtomicInteger that is scoped differently and echoes the value.

Test that increments counters

The same test again

Specification hooks

Annotate a fixture method with @BeforeSpecification to invoke it before any of the examples are executed, or @AfterSpecification to invoke it after all the examples are executed.

Suite hooks

Annotate a fixture method with @BeforeSuite to invoke it before any of the specifications run, or @AfterSuite to invoke it after all the specifications run.

Note that for the suite hooks to be run, they must exist on the fixture that is being executed directly by the test runner (eg. by JUnit).

Example

Running the specification BeforeAndAfterLogging which logs messages in its @BeforeSpecification, @BeforeExample, @AfterExample and @AfterSpecification methods, as well as in the specification and examples, results in the following :

text
Before specification
In before example
Before [Outer]
In specification
After [Outer]
In before example
Before example1
In example 1
After example1
In before example
Before example2
In example 2
After example2
After specification

Example With Listener

Running the specification BeforeAndAfterLoggingWithListener which in addition logging the before and after methods, also has two extensions which listen out for the same events and logs those, results in the following :

text
Extension 1: beforeProcessingSpecification
Extension 2: beforeProcessingSpecification
Before specification
Extension 1: Before outer example [Outer]
Extension 2: Before outer example [Outer]
Before [Outer]
In specification
After [Outer]
Extension 2: After outer example [Outer]
Extension 1: After outer example [Outer]
Extension 1: Before example example1
Extension 2: Before example example1
Before example1
In example 1
After example1
Extension 2: After example example1
Extension 1: After example example1
Extension 1: Before example example2
Extension 2: Before example example2
Before example2
In example 2
After example2
Extension 2: After example example2
Extension 1: After example example2
After specification
Extension 1: afterProcessingSpecification
Extension 2: afterProcessingSpecification