Skip to content

Commit f36ef5e

Browse files
author
Ciobanu, Andrei-Nicolin (UK - EDC)
committed
Merge remote-tracking branch 'origin/master'
2 parents 6740c05 + 7481ea2 commit f36ef5e

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

README.md

+63-8
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,38 @@ DZone Article(s):
4040

4141
### 1. Mocking Real-World Objects
4242

43-
The library supports several ways of filling/mocking the "model layer" of your application with relevant data.
43+
The library supports several ways of filling/mocking the "model layer" of your application with relevant data. (*Note*: In the following examples `User` is a *custom* bean class from the "model layer").
4444

4545
a) Using some "lambda magic" and referencing the setter methods:
4646

4747
```java
4848
MockNeat m = MockNeat.threadLocal();
4949

50-
User user1 = m.filler(() -> new User())
50+
User user1 = m.filler(() -> new User()) // Object is created throught the supplier
5151
.setter(User::setUserName,m.users())
5252
.setter(User::setFirstName, m.names().first())
5353
.setter(User::setLastName, m.names().last())
5454
.setter(User::setCreated, m.localDates().thisYear().toUtilDate())
5555
.setter(User::setModified, m.localDates().thisMonth().toUtilDate())
56-
.val();
56+
.val(); // When val() is called all the setters are applied in order over the object created with the supplier
5757

5858
// Output:
5959
// User{userName='toomjefferey', firstName='Lia', lastName='Noyd', created=Tue May 08 00:00:00 EEST 2018, modified=Fri Mar 23 00:00:00 EET 2018}
6060
```
6161

62+
Creating a `List<User>` is easy. We just collect the results using the `list()` method. Actually we can collect the results in a different structures and collections. Check the `MockUnit<T>` [interface documentation](https://github.com/nomemory/mockneat/wiki/MockUnit) for all the options:
63+
64+
```java
65+
List<User> users = m.filler(() -> new User())
66+
.setter(User::setUserName,m.users())
67+
.setter(User::setFirstName, m.names().first())
68+
.setter(User::setLastName, m.names().last())
69+
.setter(User::setCreated, m.localDates().thisYear().toUtilDate())
70+
.setter(User::setModified, m.localDates().thisMonth().toUtilDate())
71+
.list(() -> new ArrayList<>(), 10) // Collecting all the results ina List of 10 elements.
72+
.val();
73+
```
74+
6275
b) Using reflection at setter level:
6376

6477
```java
@@ -68,13 +81,12 @@ User user2 = m.reflect(User.class)
6881
.field("lastName", m.names().last())
6982
.field("created", m.localDates().thisYear().toUtilDate())
7083
.field("modified", m.localDates().thisMonth().toUtilDate())
71-
.val();
84+
.val(); // Nohting happens until you call val()
7285
// Output:
7386
// User{userName='serelovella', firstName='Hassan', lastName='Reich', created=Sat Sep 15 00:00:00 EEST 2018, modified=Tue Mar 20 00:00:00 EET 2018}
87+
```
7488

75-
```
76-
77-
c) Using reflection at constructor level:
89+
c) Using reflection at constructor level. The invocation assumes a constructor having the exact set of arguments exists:
7890

7991
```java
8092
User user3 = m.constructor(User.class)
@@ -85,7 +97,7 @@ User user3 = m.constructor(User.class)
8597
m.localDates().thisYear().toUtilDate(),
8698
m.localDates().thisMonth().toUtilDate()
8799
)
88-
.val();
100+
.val(); // Nothing happens until you call val()
89101
//Output:
90102
// User{userName='meetswipple', firstName='Florine', lastName='Buchmiller', created=Tue May 15 00:00:00 EEST 2018, modified=Wed Mar 14 00:00:00 EET 2018}
91103
```
@@ -169,6 +181,25 @@ From under the couch and put her finger on her forehead. Her words seemed to reg
169181

170182
### 5. Fill-up complex structures
171183

184+
Generate a "matrix" (`Integer[][]`) of "0" and "1":
185+
186+
```java
187+
// Generating a matrix of random integers with 5 rows and 5 columns
188+
Integer[][] a = m.from(new Integer[]{0, 1})
189+
.array(() -> new Integer[5]) // Cols
190+
.array(() -> new Integer[5][]) // Rows
191+
.val();
192+
193+
// Possible Output:
194+
// 1 1 1 0 1
195+
// 1 1 0 1 0
196+
// 1 1 1 0 0
197+
// 0 1 1 0 1
198+
// 0 1 1 1 1
199+
```
200+
201+
Filling-up a complex structure:
202+
172203
```java
173204
MockNeat mock = MockNeat.threadLocal();
174205

@@ -188,3 +219,27 @@ Possible Output (figure that!):
188219
```
189220
{3D2Ly=[{[1188658698, -57519401, -1864634426]=[2052830538, 366685266], [-133985978, -2085629065, 1907531435]=[1485192096, 605946545]}, {[450717932, -1027874751, -549281331]=[900908182, -1603177013], [742214495, -1457376922, 1024095212]=[86581883, 271158555]}], tTobl=[{[-1886416467, 548674791, -593491043]=[-1631835207, 127044558], [2070408663, 1969285421, 1886566844]=[2029888013, 1401655408]}, {[-2086648400, -305706082, -707025980]=[178357740, 1657815118], [235507533, 63522348, 1439128176]=[-1800049424, -1714421491]}], qIQLs=[{[1106366866, 663699257, 368333112]=[-1857289744, 600277178], [1526858982, -1690364246, 28655773]=[358915749, -1177167700]}, {[2006554761, -1416799941, -1912526788]=[-1768470769, 1934286466], [-1679536093, -1582849360, 35999417]=[1795480034, -705569340]}], w3LIX=[{[1859659934, 1564658075, -1996131138]=[-791077342, 1086818886], [1843489282, 423382881, 1587909770]=[-1350074159, -304332972]}, {[921761090, -376877683, 34301027]=[1680999098, 1039071483], [1696152588, 387405184, 363183726]=[1040085467, 1395835033]}]}
190221
```
222+
223+
### 6. Generating data that always match a certain (simple) regex:
224+
225+
Generating a [`LOLs`](https://en.wikipedia.org/wiki/LOL) with varying levels of intensity:
226+
227+
```java
228+
mock.regex("LO{1,15}L!").consume(10, (i, s) -> System.out.println(s));
229+
```
230+
231+
Possible output:
232+
233+
```
234+
LOOL!
235+
LOOOL!
236+
LOL!
237+
LOOL!
238+
LOL!
239+
LOL!
240+
LOOOOL!
241+
LOOOOOOOOOOL!
242+
LOL!
243+
LOOL!
244+
```
245+

0 commit comments

Comments
 (0)