Skip to content

Hippoom/test-data-builder

Repository files navigation

test-data-builder

Build StatusMaven Central A tiny library to simplify test data building.

Installation

You can download the binary from Maven Central Repository.

Usage

Building a List of test data

It is easy to build a list of test data with the static factory methodlistOfSize:

import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;

List<Order> orders = listOfSize(5, // (1)
    sequence -> new OrderBuilder() // (2)
).build();                         // (3)

(1) declaring the list should contain 5 elements

(2) a default Function<Integer, OrderBuilder> 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:

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<Order> 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<OrderBuilder, OrderBuilder> that customizes the element

(2) declaring the third element in the list applies a Function<OrderBuilder, OrderBuilder>

(3) declaring the last two elements apply a Function<OrderBuilder, OrderBuilder>

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:

import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY

List<Order> 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<OrderBuilder, OrderBuilder> that customizes the element

Customizing the multiple elements with all()

Sometimes you want to customize all elements. The all() can help you:

import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY

List<Order> orders = listOfSize(5, sequence -> new OrderBuilder())
    					.all().apply(builder -> builder.is(TAKE_AWAY)) // (1)
    					.build();

(1) declaring all elements should apply a Function<OrderBuilder, OrderBuilder> that customizes the element

You can also use the variation all(Function<T, T> function):

import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY;

List<Order> orders = listOfSize(5, sequence -> new OrderBuilder())
    					.all(builder -> builder.is(TAKE_AWAY)) // (1)
    					.build();

(1) declaring all elements should apply a Function<OrderBuilder, OrderBuilder> 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:

import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY;

List<Order> orders = listOfSize(5, sequence -> new OrderBuilder())
    					.allWithSeq(builder, seq -> builder.withId(seq)) // (1)
    					.build();

(1) declaring all elements should apply a BiFunction<OrderBuilder, Integer, OrderBuilder> that customizes the element

What if I want to customize a range of consecutive elements in the middle of the list
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
import static com.github.hippoom.tdb.Location.TAKE_AWAY;

List<Order> 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<OrderBuilder, OrderBuilder> that customizes the element

What if the Builder has a different build method other than build()
import static com.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;

List<Order> 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.