-
-
Notifications
You must be signed in to change notification settings - Fork 47
Creating CSV files with arbitrary data
Important Note: The library the documentation has moved to www.mockneat.com. The information found on this wiki is quite outdated. Please check the new site.
Create a CVS file with arbitrary data that has the following structure:
id, firstName, lastName, email, salary (euro)
The file should contain 1000 lines.
We will use:
-
longSeq()
for generating line ids; -
names().first()
for generating first names; -
names().last()
for generating last names; -
emails()
for generating emails; -
money()
for generating salaries in EURO;
To group everything in a line we can use the fmt()
function. This works similar with String.format()
but enable developers to specify named parameters. It's also more efficient.
To generate exactly 1000 lines we can keep everything in a List<String>
with the list()
method.
The list will be then written in a file with the consume()
method.
Code:
MockNeat m = MockNeat.threadLocal();
final Path path = Paths.get("./test.csv");
m.fmt("#{id},#{first},#{last},#{email},#{salary}")
.param("id", m.longSeq())
.param("first", m.names().first())
.param("last", m.names().last())
.param("email", m.emails())
.param("salary", m.money().locale(GERMANY).range(2000, 5000))
.list(1000)
.consume(list -> {
try { Files.write(path, list, CREATE, WRITE); }
catch (IOException e) { e.printStackTrace(); }
});
The result:
0,Ailene,Greener,[email protected],4.995,59 €
1,Yung,Skovira,[email protected],2.850,23 €
2,Shanelle,Hevia,[email protected],2.980,19 €
3,Venice,Lepe,[email protected],4.611,83 €
4,Mi,Repko,[email protected],3.811,38 €
5,Leonie,Slomski,[email protected],4.584,28 €
6,Elisabeth,Blasl,[email protected],2.839,69 €
7,Ernestine,Syphard,[email protected],3.471,93 €
8,Honey,Winfrey,[email protected],4.276,56 €
9,Dian,Holecek,[email protected],3.643,66 €
10,Mitchell,Lawer,[email protected],3.260,92 €
11,Kayla,Labbee,[email protected],2.504,99 €
12,Jann,Grafenstein,[email protected],4.535,70 €
13,Shaunna,Uknown,[email protected],3.028,81 €
14,Paul,Gehri,[email protected],4.931,44 €
15,Janie,Hesselink,[email protected],4.879,61 €
16,Ena,Jordahl,[email protected],4.651,49 €
17,Min,Peterman,[email protected],2.211,34 €
18,Desiree,Valasek,[email protected],4.037,74 €
19,Darci,Nolen,[email protected],4.582,98 €
20,Irene,Pyper,[email protected],4.150,80 €
21,Beryl,Beydler,[email protected],4.297,74 €
22,Demetrius,Greaser,[email protected],3.032,22 €
23,Carli,Dowty,[email protected],2.011,43 €
....
....
....
995,Maxine,Vatalaro,[email protected],4.560,79 €
996,Carlie,Jessie,[email protected],4.100,61 €
997,Roxanna,Pozzi,[email protected],4.574,65 €
998,Hedwig,Vranek,[email protected],3.146,43 €
999,Autumn,Urso,[email protected],4.701,16 €
Note: You can additionaly use the escapeCsv()
method from the MockUnitString
functional interface to be 100% sure your strings are correctly escaped for the CSV format.
In the version 0.1.9
a new method was introduced: csvs()
The above example can be re-written like this:
M.csvs().column(M.intSeq())
.column(M.names().first())
.column(M.names().last())
.column(M.emails())
.column(M.money().locale(Locale.GERMANY).range(1000, 5000))
.separator(",")
.write("test.csv", 1000);
Using the library:
Real World Examples: