Skip to content

Commit 4d9b8b4

Browse files
authored
docs: Object Mother explanation (iluwatar#2634)
1 parent 479b2e6 commit 4d9b8b4

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

object-mother/README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tag:
77
---
88

99
## Object Mother
10-
Define a factory of immutable content with separated builder and factory interfaces.
10+
It is used to define a factory of immutable content with separated builder and factory interfaces.
1111

1212
## Class diagram
1313
![alt text](./etc/object-mother.png "Object Mother")
@@ -19,6 +19,69 @@ Use the Object Mother pattern when
1919
* You want to reduce code for creation of objects in tests
2020
* Every test should run with fresh data
2121

22+
23+
## Understanding the Object Mother Pattern
24+
25+
### Real-World Scenario
26+
Imagine you're developing a Java application for a travel agency. In your system, there are different types of travelers, such as tourists, business travelers, and travel agents, each with specific attributes and behaviors. To perform thorough testing, you need to create and manipulate these traveler objects in various contexts. The Object Mother Pattern can help you generate consistent and predefined traveler objects for testing purposes, ensuring that your tests are based on known, reliable data.
27+
28+
### In Plain Terms
29+
The Object Mother Pattern is a design pattern used in Java to simplify the creation of objects with specific configurations, especially for testing. Instead of manually constructing objects with varying properties for each test case, you create a dedicated "Object Mother" class or method that produces these objects with predefined settings. This ensures that you have consistent and predictable test data, making your tests more reliable and easier to manage.
30+
31+
### Overview from a Testing Perspective
32+
The Object Mother Pattern is a testing-related design pattern that assists in maintaining a consistent and reliable testing environment. It allows you to define and create objects with specific attributes, helping you ensure that your tests produce consistent and predictable results, making it easier to spot issues and maintain your test suite.
33+
34+
### Practical Usage in Testing
35+
In software testing, especially unit testing, the Object Mother Pattern is invaluable. It helps ensure that your tests are not influenced by unpredictable data, thus making your tests more robust and repeatable. By centralizing the creation of test objects in an Object Mother, you can easily adapt your test data to different scenarios.
36+
37+
### Example in Java
38+
Here's an illustrative Java example of the Object Mother Pattern within the context of a travel agency application:
39+
40+
```java
41+
class Traveler {
42+
private String name;
43+
private int age;
44+
private boolean isBusinessTraveler;
45+
46+
// Constructor and methods for the traveler
47+
// ...
48+
49+
// Getter and setter methods
50+
// ...
51+
}
52+
53+
class TravelerMother {
54+
public static Traveler createTourist(String name, int age) {
55+
Traveler traveler = new Traveler();
56+
traveler.setName(name);
57+
traveler.setAge(age);
58+
traveler.setBusinessTraveler(false);
59+
return traveler;
60+
}
61+
62+
public static Traveler createBusinessTraveler(String name, int age) {
63+
Traveler traveler = new Traveler();
64+
traveler.setName(name);
65+
traveler.setAge(age);
66+
traveler.setBusinessTraveler(true);
67+
return traveler;
68+
}
69+
}
70+
71+
public class TravelAgency {
72+
public static void main(String[] args) {
73+
// Using the Object Mother to create traveler objects for testing
74+
Traveler tourist = TravelerMother.createTourist("Alice", 28);
75+
Traveler businessTraveler = TravelerMother.createBusinessTraveler("Bob", 35);
76+
77+
// Now you have consistent traveler objects for testing.
78+
}
79+
}
80+
81+
```
82+
83+
In this example, TravelerMother is the Object Mother class responsible for generating predefined Traveler objects with specific configurations. This approach ensures that you have consistent test data for various scenarios in a travel agency application, enhancing the reliability and effectiveness of your testing efforts.
84+
2285
## Credits
2386

2487
* [Answer by David Brown](http://stackoverflow.com/questions/923319/what-is-an-objectmother) to the stackoverflow question: [What is an ObjectMother?](http://stackoverflow.com/questions/923319/what-is-an-objectmother)

0 commit comments

Comments
 (0)