Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/srang/data-factory
Test framework plugin for autofilling objects with dummy data
https://github.com/srang/data-factory
dummy-data faker hacktoberfest hacktoberfest2020 hacktoberfest2021 hacktoberfest2022 java testing utility
Last synced: 14 days ago
JSON representation
Test framework plugin for autofilling objects with dummy data
- Host: GitHub
- URL: https://github.com/srang/data-factory
- Owner: srang
- License: apache-2.0
- Created: 2016-11-05T19:24:28.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-10-22T13:56:21.000Z (about 3 years ago)
- Last Synced: 2024-10-04T21:17:35.537Z (about 1 month ago)
- Topics: dummy-data, faker, hacktoberfest, hacktoberfest2020, hacktoberfest2021, hacktoberfest2022, java, testing, utility
- Language: Java
- Homepage:
- Size: 54.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `data-factory`
Test framework for generating rich objects with dummy dataInspired and leverages [java-faker](https://github.com/DiUS/java-faker)
## Usage
In your pom.xml, add the following:```xml
com.github.srang
data-factory
1.0.3
```In your Java code define a class you want to generate for example:
```java
public class Fruit {
public String name;
public String description;public Fruit(String name, String description) {
this.name = name;
this.description = description;
}
}
```Then to start generating `Fruit` instances, you'll need to instantiate a `DataFactory`:
```java
DataFactory fruitFactory = new BaseFactory<>(Fruit.class);
```Then you can start generating objects:
```java
// generate one fruit
Fruit fruit = fruitFactory.generate();// or generate a list of fruits
List fruits = fruitFactory.generate(6);
```## Factory Customization
There are a number of ways to customize a `DataFactory`:
```java
// inline field filter
fruitFilter.addFilter(
(Field field) -> field.getType().equals(String.class)
&& field.getName().toLowerCase().contains("name"),
() -> "Apple");Fruit apple = fruitFactory.generate();
apple.getName(); // the name is "Apple"// add custom language/locale
Locale bos = new Locale("eng","boston"); // colloquialisms
DataFactory bostonFruitFactory = new BaseFactory<>(Fruit.class, bos);
bostonFruitFactory.generate().getName(); // wahtamelon
```## Nesting and Inheritance
A `DataFactory` can be used with complex objects that have `List` fields, nested objects, and/or inherit fields from parent objects. Going back to the `Fruit` example, let's create a couple supporting objects:
```java
public class FruitBasket {
public String material;
public List contents;public Fruit(String material, List contents) {
this.material = material;
this.contents = contents;
}
}public class Berry extends Fruit {
public Integer seedCount;
public Berry(String name, String description, Integer seedCount) {
super(name, description);
this.seedCount = seedCount;
}
}
```