Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

requirements as code extract

With requirements as code extract, you can generate plain text documentation (e.g HTML pages) from a model inside the code. The model is defined with the requirements as code core project.

Getting started

If you are using Maven, include the following in your POM:


If you are using Gradle, include the following in your build.gradle:

compile 'org.requirementsascode:requirementsascodeextract:1.2.1'

This will put the following libraries on the classpath:

  • freemarker-2.3.28.jar (FreeMarker)
  • commons-lang3-3.5.jar (Apache Commons)
  • The current requirements as code extract jar
  • The current requirements as code core jar

Using requirements as code extract

Build a model

First, you need to build a model using the requirements as code core project:

Model model = Model.builder()
  .useCase("Use credit card")
    .flow("Withdraw again").after(REPAY)
    .flow("Cycle is over").anytime()
    .flow("Assign limit twice").condition(limitAlreadyAssigned)
    .flow("Too many withdrawals").condition(tooManyWithdrawalsInCycle) 

You have to use classes with special names in the model, as the engine will create documentation from these names.

For example, in step REPAY, the requestsRepay constant references the following class:

public class RequestsRepay {
    private BigDecimal amount;
    public RequestsRepay(BigDecimal amount) {
        this.amount = amount;
    public BigDecimal getAmount() {
        return amount;

The name of the class needs to be of the form VerbNoun, in third person singular. In the example, it is RequestsRepay. The documentation created from step REPAY is: "Repay: As long as account open: User requests repay. System repays."

Use template engine to generate documentation

You can create the engine like this:

FreeMarkerEngine engine = new FreeMarkerEngine(basePackagePath);

For basePackagePath, you specify your own package path in your classpath, where your FreeMarker templates are located. For example, if you use standard src/main/resources or src/test/resources folders, this is the package path below that folder.

You can generate the documentation with this call:

engine.extract(model, templateFileName, outputWriter);

The first parameter is the model, as shown above. The second parameter is the name of the template file, relative to the base package path (during construction). The third parameter is a that produces the output text.

Here's an example FreeMarker template file:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  <title>Requirements as Code - Extract Example</title>
  	<#list model.useCases as useCase>
  		<h1>Use Case: ${useCase?capitalize}</h1>
		<#list useCase.flows as f>
			<#list f.steps as s>
				<div>${s}. ${reactWhileOfStep(s)}${actorPartOfStep(s)}${userPartOfStep(s)}${systemPartOfStep(s)}</div>

The template file starts with the model instance provided by the engine, then it iterates over the model.

See the FreeMarker documentation for details. See this test class for details on how to use requirements as code extract.

Example document

Here's the full document generated from the above model:

Use Credit Card

Basic flow

Assign limit: User requests to assign limit. System assigns limit.
Withdraw: As long as account open: User requests withdrawal. System withdraws.
Repay: As long as account open: User requests repay. System repays.

Withdraw again

After Repay:
Withdraw again: User requests withdrawal. System withdraws.
Repeat: System continues at Withdraw.

Cycle is over

Close cycle: Handles RequestToCloseCycle: System closes cycle.

Assign limit twice

Anytime, when limit already assigned:
Assign limit twice: User requests to assign limit. System throws assign limit exception.

Too many withdrawals

Anytime, when too many withdrawals in cycle:
Withdraw too often: System throws too many withdrawals exception.
You can’t perform that action at this time.