Context Definition (Setup)
Definition
Definition |
---|
The SetUpInterpreter is used to simplify the creation of a particular state for the system under development. Once the state is created, we can focus on the business process to test. When a setup table is executed, enter data in the system under development to create the desired state.
|
Coloring
will visually show the test result by coloring each testing cell:
When the insertion has been executed successfully, add a green cell at the end of the data row with the word entered.
If the insertion has failed because the values specified does not respect a business rule, add a red cell at the end of the row with the word not entered.
If the insertion has failed because a specified value generates an error, colors the cell of the data in error yellow and provides information about the error.
If the system encounters an error not related to a specific data, add a yellow cell at the end of the data row and provides information about the error.
Writing fixtures for Setup tables
As we've seen in Setup Definition, a table of rules is used to simplify the creation of a particular state for the system under development. Once the state is created, we can focus on the business process to test.
This page shows the fixture code that supports the examples introduced in the Writing a Setup specification.
Fixture to create bank customers
Consider the example of setup table described in Writing a Setup specification:
setup | a group of customers | |||
number | type | first name | last name | balance |
11111-11111 | checking | Fred | Flintstone | $250.00 |
22222-22222 | savings | Fred | Flintstone | $10 000.00 |
44444-44444 | savings | Wilma | Flintstone | $10 000.00 |
55555-55555 | checking | Barney | Rubble | $999.54 |
The first cell of the first row indicates that the SetupInterpreter will be used to interpret the table. The next cell says that the enterRow() method to use is in the fixture named AGroupOfCustomers. The name of the fixture is the name of a Java class. The enterRow() will be used to enter the data.
Instead of writing a method enterRow(), you can annotate the setup method with the annotation @EnterRow. This way you can have a meaningful name for the setup method.
The second row, also known as the header row, designates the attribute columns of the data to be inserted. In our example, number, type, first name, last name and balance are the information to be created in the system under development.
The fixture code to support this example in Java is the class AGroupOfCustomersFixture shown below.
Show me the code
public class AGroupOfCustomersFixture { public AccountType type; public String number, firstName, lastName; public Money balance; public static Bank bank; public AGroupOfCustomersFixture() { bank=new Bank(); } @EnterRow public void setupAccount() { if(AccountType.SAVINGS == type) bank.openSavingsAccount(number, new Owner(firstName, lastName)).deposit(balance); else if(AccountType.CHECKING == type) bank.openCheckingAccount(number, new Owner(firstName, lastName)).deposit(balance); } }
How is the table processed?
When it runs this table, reads the first row to select the interpreter and fixture. It then reads the second row to know what the attribute columns are. Finally, it starts creating entries from the third row down to the end of the table.
For the third row carries out the following sequence of steps:
It assigns the data 11111-11111 to number, checking to type, Fred to first name, Flintstone to last name and $250.00 to balance.
It then calls the method enterRow() of the fixture AGroupOfCustomersFixture to open a bank account with the given data.