Domain Story as Spock Test Specification

Darko Kantic
Domain-Driven Stories
3 min readMay 24, 2020

Using domain stories to easily create Spock tests

In my previous story, I showed you an example of a Domain Story. What is interesting is that by reading the diagram we can construct a collection of sentences, ordered consecutively, describing what's happening in the domain we are modelling. Remember, this is not exhaustive process modelling, but one example of what is typically happening in this system. The Domain Story looks like this:

And the set of sentences extracted is:

  1. The Sales Assistant enters sold items into the Stock Management System.

2. The Manager calculates the total of sold items in the Stock Management System.

3. The Manager creates an order in the Stock Management System.

4. The Manager sends an email with an order to the Supplier System.

5. The supplier system sends an email with a delivery items list to the stock management system.

6. A member of staff, the Sales Assistant, compares the actual items delivered with the stock items list, confirms that they are the same, and records confirmation in the Stock Management System.

Handily these sentences can be used as a Behaviour Driven Development (BDD) test specification. To illustrate we will use the Spock testing framework, but it can be used with any BDD tool. Spock is a testing and specification framework for Java and Groovy application.

So let us create a Spock test specification:

Create a Groovy class called StockOrderingSpecification and copy and paste the above six sentences into the body of the class. Now put quotes around each sentence and you are halfway to creating your BDD test specification. Add the Groovy boilerplate code around your sentences and here we go:

import spock.lang.Specification

class StockOrderingSpecification extends Specification {

def "1.The Sales Assistant enters sold items into Stock Management System."() {
given: ""
when
: ""
then
: ""
}
def "2. The Manager calculates the total of sold items in the Stock Management System."() {
given: ""
when
: ""
then
: ""
}
def "3. The Manager creates an order in the Stock Management System"() {
given: ""
when
: ""
then
: ""
}
def "4. The Manager sends an email with an order to the Supplier System."() {
given: ""
when
: ""
then
: ""
}
def "5. The supplier system sends an email with a delivery items list to the stock management system."() {
given: ""
when
: ""
then
: ""
}
def "6. The member of staff, the Sales Assistant, compares the actual items delivered with the stock items list and confirms they are the same and records confirmation in the Stock Management System."() {
given: ""
when
: ""
then
: ""
}
}

Of course, you need to write some actual Groovy code to perform an action on your system, I tend to write code that calls REST services, but it is possible to integrate Selenium Web Driver and run tests for the GUI of your application. Explaining how to write Groovy tests is beyond the scope of this story, I wanted to demonstrate the connection between Domain Storytelling and BDD and how easy it is to translate a Domain Story into a BDD test specification.

--

--