Arrange-Act-Assert and naming
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
structure each test so it reads in seconds.
Planned topics
- Arrange: set up data and collaborators. Act: call the thing under test once. Assert: check the outcome.
- Separate the three parts with blank lines or comments.
- Name tests by behavior:
rejectsOrderWhenStockIsZero, nottest1orplaceOrderTest. - One reason to fail per test; a name with "and" often means two tests.
- Assert on outcomes, not on how the code got there.
Draft outline details
Demonstration
@Test
void rejectsOrderWhenStockIsZero() {
var product = aProduct().withStock(0).build(); // arrange
var result = service.place(orderOf(product)); // act
assertThat(result.status()).isEqualTo(REJECTED); // assert
}Check your understanding
What does the "Act" step do?
Show answer
Calls the code under test, once.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.