https://github.com/hippoom/test-data-builder
A tiny library to simplify test data building.
https://github.com/hippoom/test-data-builder
data-builder test-data-generator test-driven-development
Last synced: 5 months ago
JSON representation
A tiny library to simplify test data building.
- Host: GitHub
- URL: https://github.com/hippoom/test-data-builder
- Owner: Hippoom
- License: apache-2.0
- Created: 2017-09-24T12:16:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-18T03:02:54.000Z (almost 8 years ago)
- Last Synced: 2024-12-27T23:56:36.112Z (about 1 year ago)
- Topics: data-builder, test-data-generator, test-driven-development
- Language: Java
- Homepage:
- Size: 91.8 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# test-data-builder
[](https://travis-ci.org/Hippoom/test-data-builder)[](https://maven-badges.herokuapp.com/maven-central/com.github.hippoom/test-data-builder)
A tiny library to simplify test data building.
## Installation
You can download the binary from [Maven Central Repository](http://mvnrepository.com/artifact/com.github.hippoom/test-data-builder).
## Usage
###### Building a List of test data
It is easy to build a list of test data with the static factory method`listOfSize`:
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
List orders = listOfSize(5, // (1)
sequence -> new OrderBuilder() // (2)
).build(); // (3)
```
> (1) declaring the list should contain 5 elements
>
> (2) a default `Function` takes list sequence (starts from 1) and returns a data builder, the function will be used to generate a default value for each element
>
> (3) building the list, it calls `OrderBuild.build()` by default to generate `Order`
###### Customizing the element differs
Now each element has a default value, you just customize the element differ in the current test case:
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.IN_STORE
import static com.github.hippoom.tdb.Location.TAKE_AWAY
List orders = listOfSize(5, sequence -> new OrderBuilder())
.theFirst(2, builder -> builder.is(TAKE_AWAY)) // (1)
.number(3, builder -> builder.is(IN_STORE)) // (2)
.theLast(2, builder -> builder.paid()) // (3)
.build();
```
> (1) declaring the first two elements apply a `Function` that customizes the element
>
> (2) declaring the third element in the list applies a `Function`
>
> (3) declaring the last two elements apply a `Function`
###### Customizing the multiple elements with `number()`
Sometimes you want to customize multiple elements in the middle of the list. The `number()` has a overloaded variation to help you:
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY
List orders = listOfSize(5, sequence -> new OrderBuilder())
.number(2, 4).apply(builder -> builder.is(TAKE_AWAY)) // (1)
.build();
```
> (1) declaring the second and fourth element should apply a `Function` that customizes the element
###### Customizing the multiple elements with `all()`
Sometimes you want to customize all elements. The `all()` can help you:
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY
List orders = listOfSize(5, sequence -> new OrderBuilder())
.all().apply(builder -> builder.is(TAKE_AWAY)) // (1)
.build();
```
> (1) declaring all elements should apply a `Function` that customizes the element
You can also use the variation `all(Function function)`:
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY;
List orders = listOfSize(5, sequence -> new OrderBuilder())
.all(builder -> builder.is(TAKE_AWAY)) // (1)
.build();
```
> (1) declaring all elements should apply a `Function` that customizes the element
###### Customizing the multiple elements with `allWithSeq()`
Sometimes you want to customize all elements but with dynamic data based on the sequence of the element (starts from 1).
The `allWithSeq()` can help you:
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY;
List orders = listOfSize(5, sequence -> new OrderBuilder())
.allWithSeq(builder, seq -> builder.withId(seq)) // (1)
.build();
```
> (1) declaring all elements should apply a `BiFunction` that customizes the element
###### What if I want to customize a range of consecutive elements in the middle of the list
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY;
List orders = listOfSize(5, sequence -> new OrderBuilder())
.range(2, 4).apply(builder -> builder.is(TAKE_AWAY)) // (1)
.build();
```
> (1) declaring from the second(inclusive) to the fourth(inclusive) elements should apply a `Function` that customizes the element
###### What if the Builder has a different build method other than `build()`
```java
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
List orders = listOfSize(5, sequence -> new OrderBuilder())
.build(builder -> builder.anotherBuild()); //(1)
```
> (1) calls `anotherBuild()` instead of `build()` to generate the elements
## License
Licensed under Apache License (the "License"); You may obtain a copy of the License in the LICENSE file, or at [here](https://github.com/Hippoom/test-data-builder/blob/master/LICENSE).