Skip to content

Commit b0ac4c1

Browse files
committed
iluwatar#590 explanation for Arrange/Act/Assert
1 parent 0ead283 commit b0ac4c1

File tree

1 file changed

+117
-2
lines changed

1 file changed

+117
-2
lines changed

arrange-act-assert/README.md

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,137 @@ tags:
99
---
1010

1111
## Also known as
12+
1213
Given/When/Then
1314

1415
## Intent
15-
The Arrange/Act/Assert (AAA) is a pattern for organizing unit tests.
16+
17+
Arrange/Act/Assert (AAA) is a pattern for organizing unit tests.
1618
It breaks tests down into three clear and distinct steps:
19+
1720
1. Arrange: Perform the setup and initialization required for the test.
1821
2. Act: Take action(s) required for the test.
1922
3. Assert: Verify the outcome(s) of the test.
2023

24+
## Explanation
25+
26+
This pattern has several significant benefits. It creates a clear separation between a test's
27+
setup, operations, and results. This structure makes the code easier to read and understand. If
28+
you place the steps in order and format your code to separate them, you can scan a test and
29+
quickly comprehend what it does.
30+
31+
It also enforces a certain degree of discipline when you write your tests. You have to think
32+
clearly about the three steps your test will perform. It makes tests more natural to write at
33+
the same time since you already have an outline.
34+
35+
Real world example
36+
37+
> We need to write comprehensive and clear unit test suite for a class.
38+
39+
In plain words
40+
41+
> Arrange/Act/Assert is a testing pattern that organizes tests into three clear steps for easy
42+
> maintenance.
43+
44+
WikiWikiWeb says
45+
46+
> Arrange/Act/Assert is a pattern for arranging and formatting code in UnitTest methods.
47+
48+
**Programmatic Example**
49+
50+
Let's first introduce our `Cash` class to be unit tested.
51+
52+
```java
53+
public class Cash {
54+
55+
private int amount;
56+
57+
Cash(int amount) {
58+
this.amount = amount;
59+
}
60+
61+
void plus(int addend) {
62+
amount += addend;
63+
}
64+
65+
boolean minus(int subtrahend) {
66+
if (amount >= subtrahend) {
67+
amount -= subtrahend;
68+
return true;
69+
} else {
70+
return false;
71+
}
72+
}
73+
74+
int count() {
75+
return amount;
76+
}
77+
}
78+
```
79+
80+
Then we write our unit tests according to Arrange/Act/Assert pattern. Notice the clearly
81+
separated steps for each unit test.
82+
83+
```java
84+
public class CashAAATest {
85+
86+
@Test
87+
public void testPlus() {
88+
//Arrange
89+
var cash = new Cash(3);
90+
//Act
91+
cash.plus(4);
92+
//Assert
93+
assertEquals(7, cash.count());
94+
}
95+
96+
@Test
97+
public void testMinus() {
98+
//Arrange
99+
var cash = new Cash(8);
100+
//Act
101+
var result = cash.minus(5);
102+
//Assert
103+
assertTrue(result);
104+
assertEquals(3, cash.count());
105+
}
106+
107+
@Test
108+
public void testInsufficientMinus() {
109+
//Arrange
110+
var cash = new Cash(1);
111+
//Act
112+
var result = cash.minus(6);
113+
//Assert
114+
assertFalse(result);
115+
assertEquals(1, cash.count());
116+
}
117+
118+
@Test
119+
public void testUpdate() {
120+
//Arrange
121+
var cash = new Cash(5);
122+
//Act
123+
cash.plus(6);
124+
var result = cash.minus(3);
125+
//Assert
126+
assertTrue(result);
127+
assertEquals(8, cash.count());
128+
}
129+
}
130+
```
131+
21132
## Applicability
133+
22134
Use Arrange/Act/Assert pattern when
23135

24-
* you need to structure your unit tests so they're easier to read, maintain, and enhance.
136+
* You need to structure your unit tests so that they're easier to read, maintain, and enhance.
25137

26138
## Credits
27139

28140
* [Arrange, Act, Assert: What is AAA Testing?](https://blog.ncrunch.net/post/arrange-act-assert-aaa-testing.aspx)
29141
* [Bill Wake: 3A – Arrange, Act, Assert](https://xp123.com/articles/3a-arrange-act-assert/)
30142
* [Martin Fowler: GivenWhenThen](https://martinfowler.com/bliki/GivenWhenThen.html)
143+
* [xUnit Test Patterns: Refactoring Test Code](https://www.amazon.com/gp/product/0131495054/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0131495054&linkId=99701e8f4af2f7e8dd50d720c9b63dbf)
144+
* [Unit Testing Principles, Practices, and Patterns](https://www.amazon.com/gp/product/1617296279/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617296279&linkId=74c75cf22a63c3e4758ae08aa0a0cc35)
145+
* [Test Driven Development: By Example](https://www.amazon.com/gp/product/0321146530/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0321146530&linkId=5c63a93d8c1175b84ca5087472ef0e05)

0 commit comments

Comments
 (0)