-
Notifications
You must be signed in to change notification settings - Fork 1
/
Instancio6CustomGeneratorTest.java
56 lines (45 loc) · 1.76 KB
/
Instancio6CustomGeneratorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.example;
import org.example.person.Address;
import org.example.person.Person;
import org.instancio.Instancio;
import org.instancio.Random;
import org.instancio.generator.AfterGenerate;
import org.instancio.generator.Generator;
import org.instancio.generator.Hints;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.instancio.Select.all;
import static org.instancio.Select.field;
/**
* An example of using a custom generator.
*/
class Instancio6CustomGeneratorTest {
private static final String ARGENTINA = "Argentina";
private static final int PHONES_SIZE = 5;
private static final Generator<Address> ADDRESS_GENERATOR = new Generator<Address>() {
@Override
public Address generate(final Random random) {
Address address = new Address();
address.setCountry(ARGENTINA);
return address;
}
@Override
public Hints hints() {
// The hint telling the engine to populate any field that has a null value
return Hints.afterGenerate(AfterGenerate.POPULATE_NULLS);
}
};
@Test
void usingCustomGenerator() {
Person person = Instancio.of(Person.class)
.supply(all(Address.class), ADDRESS_GENERATOR)
.generate(field(Address::getPhoneNumbers), gen -> gen.collection().size(PHONES_SIZE))
.create();
Address address = person.getAddress();
assertThat(address.getPhoneNumbers()).hasSize(PHONES_SIZE);
assertThat(address.getCountry()).isEqualTo(ARGENTINA);
// null fields were populated with random values
assertThat(address.getStreet()).isNotNull();
assertThat(address.getCity()).isNotNull();
}
}