Pattern: Expected Result
Intent
Ensure that a function with a given arguments returns the expected result or changes the state of object into expected state.
Also Known As
Motivation
Applicability
Use Expected Result to:
- check that function returns consistent values for different arguments
- verify that method leaves the object (or the whole object graph) in the correct state
Example
In the following test we will check that creates a correct instance of :
public void testAccountManagerCreatesGoodAccount() throws Exception {
Account account = accountManager.createAccount("testAccount");
assertNotNull("Null created account", account);
assertEquals("Wrong created account name", account.getName());
}
Related Patterns
- [[Descriptive Assertions]] should be used to improve readability of the test.
- If it is needed to check that function throws an exception for certain input - use [[Test for Failures]].
- [[Journalling]] can be used if it is needed to check that state was achieved within a correct sequence of state changes.
Related Antipatterns
- If the result of the function or expected state is a complex object test might result in [[Complex Expected Result]] antipattern.
- Quite often the dislike of having many tests in test case may result in [[Multiple Tests In One]] antipattern.