Skip to content

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:

importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
List<Order> orders = listOfSize(5, // (1)sequence -> newOrderBuilder() // (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:

importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
importstaticcom.github.hippoom.tdb.Location.IN_STOREimportstaticcom.github.hippoom.tdb.Location.TAKE_AWAYList<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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:

importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
importstaticcom.github.hippoom.tdb.Location.TAKE_AWAYList<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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:

importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
importstaticcom.github.hippoom.tdb.Location.TAKE_AWAYList<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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):

importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
importstaticcom.github.hippoom.tdb.Location.TAKE_AWAY;
List<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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:

importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
importstaticcom.github.hippoom.tdb.Location.TAKE_AWAY;
List<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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
importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
importstaticcom.github.hippoom.tdb.Location.TAKE_AWAY;
List<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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()
importstaticcom.github.hippoom.tdb.GenericTestDataListBuilder.listOfSize;
List<Order> orders = listOfSize(5, sequence -> newOrderBuilder())
.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.

About

A tiny library to simplify test data building.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages