Skip to content

Commit b95b228

Browse files
committed
. d Added snippet for fake data
1 parent 9809e10 commit b95b228

File tree

2 files changed

+142
-148
lines changed

2 files changed

+142
-148
lines changed

approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java

Lines changed: 119 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -5,143 +5,134 @@
55

66
import java.util.List;
77

8-
public class LoadersAndSaversExamplesTest
9-
{
10-
public static class Step0
11-
{
12-
// begin-snippet: step0
13-
@Test
14-
public void senior_customer_list_includes_only_those_over_age_65()
15-
{
16-
DataBase database = initializeDatabase();
17-
MailServer mailServer = initializeMailServer();
18-
sendOutSeniorDiscounts(database, mailServer);
19-
Approvals.verifyAll("", mailServer.getRecipients());
20-
}
21-
// end-snippet
22-
private MailServer initializeMailServer()
23-
{
24-
return new MailServer();
25-
}
26-
private DataBase initializeDatabase()
27-
{
28-
return null;
29-
}
30-
private void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
31-
{
8+
public class LoadersAndSaversExamplesTest {
9+
public static class Step0 {
10+
// begin-snippet: step0
11+
@Test
12+
public void senior_customer_list_includes_only_those_over_age_65() {
13+
DataBase database = initializeDatabase();
14+
MailServer mailServer = initializeMailServer();
15+
sendOutSeniorDiscounts(database, mailServer);
16+
Approvals.verifyAll("", mailServer.getRecipients());
17+
}
18+
// end-snippet
19+
20+
private MailServer initializeMailServer() {
21+
return new MailServer();
22+
}
23+
24+
private DataBase initializeDatabase() {
25+
return null;
26+
}
27+
28+
private void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
29+
30+
}
3231
}
33-
}
34-
public static class Step0_5
35-
{
36-
@Test
37-
void test_dump_data()
38-
{
39-
MyDatabase database = new MyDatabase();
40-
// begin-snippet: step_capture_data
41-
List<Customer> seniorCustomers = database.getSeniorCustomers();
42-
seniorCustomers.stream().forEach(System.out::println);
43-
// end-snippet
44-
Approvals.verifyAll("", seniorCustomers, c -> c.toString());
32+
33+
public static class Step0_5 {
34+
@Test
35+
void test_dump_data() {
36+
MyDatabase database = new MyDatabase();
37+
// begin-snippet: step_capture_data
38+
List<Customer> seniorCustomers = database.getSeniorCustomers();
39+
seniorCustomers.stream().forEach(System.out::println);
40+
// end-snippet
41+
Approvals.verifyAll("", seniorCustomers, c -> c.toString());
42+
}
43+
public static class MyDatabase {
44+
public List<Customer> getSeniorCustomers() {
45+
return
46+
// begin-snippet: step_fake_data
47+
List.of(
48+
new Customer( "Bob, Jones, 123 Elm St., Tempe, AZ, 14-MAR-1958"),
49+
new Customer("Mary, Smith, 345 Oak St., Mason, VA, 04-MAY-1944"));
50+
// end-snippet
51+
}
52+
}
4553
}
46-
public static class MyDatabase
47-
{
48-
public List<Customer> getSeniorCustomers()
49-
{
50-
return List.of(new Customer("Bob, Jones, 123 Elm St., Tempe, AZ, 14-MAR-1958"),
51-
new Customer("Mary, Smith, 345 Oak St., Mason, VA, 04-MAY-1944"));
52-
}
54+
55+
class Step1 {
56+
// begin-snippet: step1
57+
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
58+
List<Customer> seniorCustomers = database.getSeniorCustomers();
59+
for (Customer customer : seniorCustomers) {
60+
Discount seniorDiscount = getSeniorDiscount();
61+
String message = generateDiscountMessage(customer, seniorDiscount);
62+
mailServer.sendMessage(customer, message);
63+
}
64+
}
65+
// end-snippet
5366
}
54-
}
55-
class Step1
56-
{
57-
// begin-snippet: step1
58-
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
59-
{
60-
List<Customer> seniorCustomers = database.getSeniorCustomers();
61-
for (Customer customer : seniorCustomers)
62-
{
63-
Discount seniorDiscount = getSeniorDiscount();
64-
String message = generateDiscountMessage(customer, seniorDiscount);
65-
mailServer.sendMessage(customer, message);
66-
}
67+
68+
class Step2 {
69+
// begin-snippet: step2
70+
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
71+
Loader<List<Customer>> seniorCustomerLoader = () -> database.getSeniorCustomers();
72+
List<Customer> seniorCustomers = seniorCustomerLoader.load();
73+
for (Customer customer : seniorCustomers) {
74+
Discount seniorDiscount = getSeniorDiscount();
75+
String message = generateDiscountMessage(customer, seniorDiscount);
76+
mailServer.sendMessage(customer, message);
77+
}
78+
}
79+
// end-snippet
6780
}
68-
// end-snippet
69-
}
70-
class Step2
71-
{
72-
// begin-snippet: step2
73-
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
74-
{
75-
Loader<List<Customer>> seniorCustomerLoader = () -> database.getSeniorCustomers();
76-
List<Customer> seniorCustomers = seniorCustomerLoader.load();
77-
for (Customer customer : seniorCustomers)
78-
{
79-
Discount seniorDiscount = getSeniorDiscount();
80-
String message = generateDiscountMessage(customer, seniorDiscount);
81-
mailServer.sendMessage(customer, message);
82-
}
81+
82+
class Step3 {
83+
// begin-snippet: step3
84+
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
85+
sendOutSeniorDiscounts(mailServer, database::getSeniorCustomers); // +
86+
} // +
87+
88+
// +
89+
public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader) { // +
90+
List<Customer> seniorCustomers = seniorCustomerLoader.load();
91+
for (Customer customer : seniorCustomers) {
92+
Discount seniorDiscount = getSeniorDiscount();
93+
String message = generateDiscountMessage(customer, seniorDiscount);
94+
mailServer.sendMessage(customer, message);
95+
}
96+
}
97+
// end-snippet
8398
}
84-
// end-snippet
85-
}
86-
class Step3
87-
{
88-
// begin-snippet: step3
89-
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
90-
{
91-
sendOutSeniorDiscounts(mailServer, database::getSeniorCustomers); // +
92-
} // +
93-
// +
94-
public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader)
95-
{ // +
96-
List<Customer> seniorCustomers = seniorCustomerLoader.load();
97-
for (Customer customer : seniorCustomers)
98-
{
99-
Discount seniorDiscount = getSeniorDiscount();
100-
String message = generateDiscountMessage(customer, seniorDiscount);
101-
mailServer.sendMessage(customer, message);
102-
}
99+
100+
private String generateDiscountMessage(Customer customer, Discount seniorDiscount) {
101+
return null;
103102
}
104-
// end-snippet
105-
}
106-
private String generateDiscountMessage(Customer customer, Discount seniorDiscount)
107-
{
108-
return null;
109-
}
110-
private Discount getSeniorDiscount()
111-
{
112-
return null;
113-
}
114-
private class DataBase
115-
{
116-
public List<Customer> getSeniorCustomers()
117-
{
118-
return List.of();
103+
104+
private Discount getSeniorDiscount() {
105+
return null;
119106
}
120-
}
121-
private static class MailServer
122-
{
123-
public void sendMessage(Customer customer, String message)
124-
{
107+
108+
private class DataBase {
109+
public List<Customer> getSeniorCustomers() {
110+
return List.of();
111+
}
125112
}
126-
public String[] getRecipients()
127-
{
128-
return new String[]{};
113+
114+
private static class MailServer {
115+
public void sendMessage(Customer customer, String message) {
116+
}
117+
118+
public String[] getRecipients() {
119+
return new String[] {};
120+
}
129121
}
130-
}
131-
private static class Customer
132-
{
133-
private final String s;
134-
public Customer(String s)
135-
{
136-
this.s = s;
122+
123+
private static class Customer {
124+
private final String s;
125+
126+
public Customer(String s) {
127+
this.s = s;
128+
}
129+
130+
@Override
131+
public String toString() {
132+
return s;
133+
}
137134
}
138-
@Override
139-
public String toString()
140-
{
141-
return s;
135+
136+
private class Discount {
142137
}
143-
}
144-
private class Discount
145-
{
146-
}
147138
}

approvaltests-util/docs/how_to/LoadersAndSavers.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,16 @@ We want to test the following method:
4141
<!-- snippet: step1 -->
4242
<a id='snippet-step1'></a>
4343
```java
44-
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
45-
{
46-
List<Customer> seniorCustomers = database.getSeniorCustomers();
47-
for (Customer customer : seniorCustomers)
48-
{
49-
Discount seniorDiscount = getSeniorDiscount();
50-
String message = generateDiscountMessage(customer, seniorDiscount);
51-
mailServer.sendMessage(customer, message);
52-
}
44+
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer) {
45+
List<Customer> seniorCustomers = database.getSeniorCustomers();
46+
for (Customer customer : seniorCustomers) {
47+
Discount seniorDiscount = getSeniorDiscount();
48+
String message = generateDiscountMessage(customer, seniorDiscount);
49+
mailServer.sendMessage(customer, message);
50+
}
5351
}
5452
```
55-
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L57-L68' title='Snippet source file'>snippet source</a> | <a href='#snippet-step1' title='Start of snippet'>anchor</a></sup>
53+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L56-L65' title='Snippet source file'>snippet source</a> | <a href='#snippet-step1' title='Start of snippet'>anchor</a></sup>
5654
<!-- endSnippet -->
5755

5856
In this case, we want to replace the functions that use the DataBase object with Loaders :
@@ -64,15 +62,14 @@ We start with the test:
6462
<a id='snippet-step0'></a>
6563
```java
6664
@Test
67-
public void senior_customer_list_includes_only_those_over_age_65()
68-
{
69-
DataBase database = initializeDatabase();
70-
MailServer mailServer = initializeMailServer();
71-
sendOutSeniorDiscounts(database, mailServer);
72-
Approvals.verifyAll("", mailServer.getRecipients());
65+
public void senior_customer_list_includes_only_those_over_age_65() {
66+
DataBase database = initializeDatabase();
67+
MailServer mailServer = initializeMailServer();
68+
sendOutSeniorDiscounts(database, mailServer);
69+
Approvals.verifyAll("", mailServer.getRecipients());
7370
}
7471
```
75-
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L12-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-step0' title='Start of snippet'>anchor</a></sup>
72+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L10-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-step0' title='Start of snippet'>anchor</a></sup>
7673
<!-- endSnippet -->
7774

7875
This test works against a live database with a live mail server.
@@ -94,7 +91,7 @@ Now we dump the data resulting from a successful query so that we can create a f
9491
List<Customer> seniorCustomers = database.getSeniorCustomers();
9592
seniorCustomers.stream().forEach(System.out::println);
9693
```
97-
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L40-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-step_capture_data' title='Start of snippet'>anchor</a></sup>
94+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L37-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-step_capture_data' title='Start of snippet'>anchor</a></sup>
9895
<!-- endSnippet -->
9996

10097
generates
@@ -110,9 +107,15 @@ Mary, Smith, 345 Oak St., Mason, VA, 04-MAY-1944
110107

111108
Step 3: Create a result object populated with these values (or at least enough of them to ensure the function using the data will be properly exercised).
112109

110+
<!-- snippet: step_fake_data -->
111+
<a id='snippet-step_fake_data'></a>
112+
```java
113+
List.of(
114+
new Customer( "Bob, Jones, 123 Elm St., Tempe, AZ, 14-MAR-1958"),
115+
new Customer("Mary, Smith, 345 Oak St., Mason, VA, 04-MAY-1944"));
113116
```
114-
List&lt;Customer&gt; seniorCustomers = List.of(new Customer("Bob", "Jones", '123 Elm St.", ...), /\* ... \*/);
115-
```
117+
<sup><a href='/approvaltests-util-tests/src/test/java/com/spun/util/persistence/LoadersAndSaversExamplesTest.java#L46-L50' title='Snippet source file'>snippet source</a> | <a href='#snippet-step_fake_data' title='Start of snippet'>anchor</a></sup>
118+
<!-- endSnippet -->
116119

117120
### Step 4: In the original method, replace the function call with a Loader
118121
<pre style="color: gray">

0 commit comments

Comments
 (0)