Test Driven Development
The 3 Fundamental Test Clauses
Fundamental Clauses
- There exists an inferrable
- given,
- when,
- and then
- clause for every test.
Fundamental Clauses: given
- The given clause declares, initializes, and instantiates all known variable values.
- The given clause of a test sets the state of the “environment” for the entity that needs to be tested.
- The expected output is always given prior to testing. (How else could one test?)
- An object must set its state for the respective test
- A
Person
mustsetName
before attempting togetName
- An
Inventory
mustadd
items before attempting toget
an item - A
Game
must have aPlayer
before attempting toplay
- A
Fundamental Clauses: given
Example 1
- A
Person
mustsetName
before attempting togetName
@Test
public void testGetName() {
// given
Person person = new Person();
String expectedName = "Leon";
person.setName(expectedName);
}
Fundamental Clauses: given
Example 2
- An
Inventory
mustadd
items before attempting toget
an item
@Test
public void testGetItem() {
// given
Inventory inventory = new Inventory();
String expectedItem = "Milk";
inventory.add(expectedItem)
}
Fundamental Clauses: given
Example 3
- A
Game
mustadd
aPlayer
before attempting toplay
@Test public void testGetWinner() { // given Game game = new Game(); Player expectedWinner = new Player(); game.add(player); game.play(); }
Fundamental Clauses: when
- when clause invokes the method to be tested
- when clause stores output of method invokation.
void
methods should change state of an object.- output should be retrieved by supplementary method-invokation followed by the invokation of the method to be tested.
- by convention, the output of the method invokation is stored in a variable named
actual
.
Fundamental Clauses: when
- In simple test-classes, test-methods are named after the method to be tested in the when clause.
- The
getName
method of aPerson
class is tested bytestGetName
method of aPersonTest
class. - The
get
method of anInventory
class is tested bytestGet
method of aInventoryTest
class. - The
play
method of aGame
class is tested bytestPlay
method of aGameTest
class.
- The
Fundamental Clauses: when
Example 1
@Test
public void testGetName() {
// given
Person person = new Person();
String expectedName = "Leon";
person.setName(expectedName);
// when
String actualName = person.getName();
}
Fundamental Clauses: when
Example 2
@Test
public void testGetItem() {
// given
Inventory inventory = new Inventory();
String expectedItem = "Milk";
inventory.add(expectedItem)
// when
String expectedItem = inventory.get(0);
}
Fundamental Clauses: when
Example 3
@Test
public void testGetWinner() {
// given
Game game = new Game();
Player expectedWinner = new Player();
game.add(player);
game.play();
// when
Player actualWinner = game.getWinner()
}
Fundamental Clauses: then
- then clause will
assert
something to betrue
regarding the output of the method being tested- If these assertions fail, then we will be pointed to the defect in the code.
- If these assertions pass, then we know our implementation works as expected.
- Simple test cases typically
assert
theexpected
output to be equal to theactual
output.
Fundamental Clauses: then
Example 1
@Test
public void testGetName() {
// given
Person person = new Person();
String expectedName = "Leon";
person.setName(expectedName);
// when
String actualName = person.getName();
// then
Assert.assertEquals(expectedName, actualName);
}
Fundamental Clauses: then
Example 2
@Test
public void testGetItem() {
// given
Inventory inventory = new Inventory();
String expectedItem = "Milk";
inventory.add(expectedItem)
// when
String actualItem = inventory.get(0);
// then
Assert.assertEquals(expectedItem, actualItem);
}
Fundamental Clauses: then
Example 3
@Test
public void testGetWinner() {
// given
Game game = new Game();
Player expectedWinner = new Player();
game.add(player);
game.play();
// when
Player actualWinner = game.getWinner()
// then
Assert.assertEquals(expectedWinner, actualWinner)
}