9 Ekim 2022 Pazar

Datafaker

Giriş
Birim testleri için test verisi üreten iki kütüphane var.
1. Datafaker

Maven
Örnek
Şu satırı dahil ederiz
<dependency>
  <groupId>net.datafaker</groupId>
  <artifactId>datafaker</artifactId>
  <version>1.6.0</version>
</dependency>
Örnek
Şu satırı dahil ederiz. Datafaker 2.0 sürümü sadece Java 17  ve üstü ile çalışıyor
<dependency>
  <groupId>net.datafaker</groupId>
  <artifactId>datafaker</artifactId>
  <version>2.0.1</version>
</dependency>
Faker Sınıfı
collection metodu
Örnek
Şöyle yaparız. 2 veya 6 eleman uzunluğunda bir liste yaratır. Listede isim veya şehir ismi bulunur
Faker faker = new Faker();

List<String> names =
  faker.collection(
    () -> faker.name().fullName(),
    () -> faker.address().city())
  .len(2, 6)
  .generate();

System.out.println(names);
lorem metodu
Örnek - words
Şöyle yaparız
Faker faker = new Faker();
faker.lorem().words(10)
number metodu
Örnek - numberBetween
Şöyle yaparız
Faker faker = new Faker();
for (int i = 0; i < 1000; i++) {
  String name = faker.name().fullName();
  String gender = faker.gender().binaryTypes().toUpperCase();
  int age = faker.number().numberBetween(18, 65);
  int externalId = faker.number().numberBetween(100000, 999999);
  ...  
}
stream metodu
Örnek
Şöyle yaparız. Stream içinde 20 - 22 değerleri arasında double bulunur
Faker faker = new Faker();

Stream<Object> objects =
  faker.<Object>stream(() -> faker.random().nextDouble(20, 22))
  .generate();

objects.forEach(System.out::println);
Örnek
Şöyle yaparız. Stream içinde 20 - 22 değerleri arasında double bulunur. Stream büyüklüğü 3 veya 5 arasındadır
Faker faker = new Faker();

Stream<Object> objects =
  faker.<Object>stream(() -> faker.random().nextDouble(20, 22))
  .len(3, 5)
  .generate();

objects.forEach(System.out::println);
CsvTransformer Sınıfı
Açıklaması şöyle
The CsvTransformer builder also gives you several parameters to use, such as: 

- quote(), in case you want to change the default(") quote.

- separator(), the character which delimits columns in rows.

- header() the boolean parameter, which toggles the generation of the header in the resulting CSV.
generate metodu
Örnek
Şöyle yaparız
// Datafaker supports any locale from the Locale package
Faker faker = new Faker(Locale.GERMANY); 

// Define a schema, which we can use in any Transformer
Schema<Object, ?> schema = Schema.of( 
  field("firstName", () -> faker.name().firstName()),
  field("lastName", () -> faker.name().lastName()),
  field("phoneNumber", () -> faker.phoneNumber().phoneNumberInternational()));

// Instantiate CsvTransformer using the appropriate builder.
CsvTransformer<Object> csvTransformer = CsvTransformer.builder().build(); 

// This is where the magic happens. Call the `generate` method on the 
// transformer with your schema plus the number of records to get the result as a string.
System.out.println(csvTransformer.generate(schema, 5)); 
Çıktı şöyle
"firstName";"lastName";"phoneNumber"

"Jannik";"Ripken";"+49 9924 780126"

"Frederike";"Birkemeyer";"+49 2933 945975"

"Michelle";"Steuk";"+49 7033 814683"

"Ellen";"Semisch";"+49 9537 991422"

"Oskar";"Habel";"+49 3626 169891"
JavaObjectTransformer Sınıfı
apply metodu
Örnek
Şöyle yaparız
// Java Record with 3 properties
public record Client(String firstName, String lastName, String phoneNumber) {}

// provide the Schema for our class.
Faker faker = new Faker();
JavaObjectTransformer jTransformer = new JavaObjectTransformer();

Schema<Object, ?> schema = Schema.of(
  field("firstName", () -> faker.name().firstName()),
  field("lastName", () -> faker.name().lastName()),
  field("phoneNumber", () -> faker.phoneNumber().phoneNumberInternational())
);

System.out.println(jTransformer.apply(Client.class, schema));

// Output 
// Client{firstName='Elton', lastName='Conroy', phoneNumber='+1 808-239-0480'}
BaseFaker Sınıfı
populate metodu - Class + Class Üzerinde @FakeForSchema Anotasyonu
Örnek 
Şöyle yaparız
// com.datafaker.DatafakerSchema class
public static Schema<Object, ?> defaultSchema() {
  var faker = new Faker(Locale.UK, new RandomService(new Random(1)));
  return Schema.of(
    field("firstName", () -> faker.name().firstName()),
    field("lastName", () -> faker.name().lastName()),
    field("phoneNumber", () -> faker.phoneNumber().phoneNumberInternational())
  );
}

@FakeForSchema("com.datafaker.DatafakerSchema#defaultSchema")
public record Client(String firstName, String lastName, String phoneNumber) { }

// Then you can use net.datafaker.providers.base.BaseFaker.populate(java.lang.Class<T>) 
// to populate the object with the default predefined schema.
Client client = BaseFaker.populate(Client.class);
populate metodu - Class + Schema
Örnek 
Şöyle yaparız
// Alternatively, you can use 
// net.datafaker.providers.base.BaseFaker.populate(
//  java.lang.Class<T>, 
//   net.datafaker.schema.Schema<java.lang.Object, ?>) 
//  to populate the object with a custom schema:
Client client = BaseFaker.populate(Client.class, Schema.of(
  field("firstName", () -> faker.name().firstName()),
  field("lastName", () -> faker.name().lastName()),
  field("phoneNumber", () -> faker.phoneNumber().phoneNumberInternational())
));








Hiç yorum yok:

Yorum Gönder