Skip to content

MockUnit

Andrei Ciobanu edited this page Mar 11, 2017 · 21 revisions

Generic MockUnit<T>

Method Return Description
array() MockUnit<T[]> This method is used to return an array of arbitrary values of type T.
collection() MockUnit<Collection<T>> This method is used to return an arbitrary Collection<T>.
consume() void This method is used to consume the results of a MockUnit.
list() MockUnit<List<T>> This method is used to generate a MockUnit<List<T>>.
map() MockUnit<R> This method is used to translate a MockUnit<T> into a MockUnit<R>.
mapKeys() MockUnit<T, R> This method is used to generate a MockUnit<Map<T, R>> from a MockUnit<T>.
mapToDouble() MockUnitDouble This method is used to translate a MockUnit<T> into a MockUnitDouble.
mapToInt() MockUnitInt This method is used to translate a MockUnit<T> into a MockUnitInt.
mapToLong() MockUnitLong This method is used to translate a MockUnit<T> into a MockUnitLong.
mapToString() MockUnitString This method is used to translate a MockUnit<T> into a MockUnitString.
mapVals() MockUnit<Map<R, T>> This method is used to generate a MockUnit<Map<R, T>> from a MockUnit<T>.
set() MockUnit<Set<T>> This method is used to generate a MockUnit<Set<T>>.
stream() `MockUnit<Stream> This method is sued to generate a MockUnit<Stream<T>>.
val() T This method is used to obtain an individual value T from a MockUnit<T>.
valStr() String This method is used to generate a String from the MockUnit<T>.

array()

This method is used to generate a generic array T[] from a MockUnit<T>.

Example generating a String[] with size 100, where each String value is a first name:

String[] names = m.names()
                    .first()
                    .array(String.class, 100)
                    .val();

collection()

This method is used to generate a MockUnit<Collection<T>> from a MockUnit<T>.

The collection needs to have a public NO_ARGS constructor.

Example for generating a Vector<Boolean> with size 100, where each Boolean value has a 35.5% probability of being true:

Collection<Boolean> vector = mock.bools()
                                 .probability(35.50)
                                 .collection(Vector.class, 100)
                                 .val();

consume()

This method is used to "consume" the values that are generated from a MockUnit<T>:

Example of printing to stdout a List<String> where each entry is an URL:

m.urls().list(100).consume(System.out::println);
// Possible Output: 
// [http://www.frenziedchi.io, http://www.eyelinersbarry.gov, ..., http://www.sphygmicmillard.com]

list()

This method is used to create a MockUnit<List<T>> from a previous MockUnit<T>.

By default the implementation used is ArrayList<T>.

Example for generating a List<String> where every String is a country name. The list has 100 elements:

List<String> countries = m.countries().names().list(100).val();
// Possible Output: [Guam, Lao People's Democratic Republic, French Polynesia, Algeria, Somalia, Iraq, ...]

It is also possible to specify a different List<T> implementation. For example, in the previous example we can use a LinkedList<String> instead of the default ArrayList<String>:

List<String> countries = m.countries().names().list(LinkedList.class, 100).val();
// Possible Output: [Guam, Lao People's Democratic Republic, French Polynesia, Algeria, Somalia, Iraq, ...]

NOTE: The list() method only accepts List implementations that have a public NO_ARGS constructor.

map()

This method allows us to do pre-processing on the values before they are actually generated.

It is also possible to help translate a MockUnit<T> to a MockUnit<R> (of a different type).

The input parameter for the method is a Function<T, R>. T can be the same R.

Example for translating a MockUnit<Boolean> into a MockUnit<String> that holds the String values "true" and "false":

List<String> list = mock.bools().map((b) -> b.toString()).list(10).val();

mapKeys()

The method help us generating a MockUnit<Map<R, T> from a MockUnit<T>.

The keys can be obtained from:

  • Another MockUnit<R>;
  • From a Supplier<R>;
  • From an Iterable<R>;
  • From an generic array R[];
  • From primitives arrays: int[], double[], long[];

Example for obtaining a Map<Integer, Boolean> from an array of int[]:

int[] keys = { 100, 200, 300, 400, 500, 600 };
Map<Integer, Boolean> map = mock.bools().mapKeys(keys).val();
// Possible Output: {400=true, 100=false, 500=false, 200=true, 600=true, 300=true}

Example for obtaining a Map<Double, Boolean> (LinkedHashMap) from an Iterable<Double>:

Iterable<Double> iterable = unmodifiableList(asList(0.1, 0.2, 0.3, 0.4, 0.5));
Map<Double, Boolean> map = mock.bools().mapKeys(LinkedHashMap.class, iterable).val();
// Possible Output: {0.1=true, 0.2=true, 0.3=false, 0.4=false, 0.5=true}

mapToDouble()

This method is used to transform a MockUnit<T> into a more specific MockUnitDouble.

The input parameter is a Function<T, Double>.

Example for translating a MockUnit<Boolean> into a MockUnitDouble. If the value it's true, it should return a value between [1.0, 2.0), else it should return a value between [2.0, 3.0):

MockUnitDouble md = m.bools()
                     .mapToDouble((bool) -> {
                         if (bool)
                             return m.doubles().range(1.0, 2.0).val();
                         return m.doubles().range(2.0, 3.0).val();
                     });

Double d = md.val();

mapToInt()

This method is used to translate a MockUnit<T> into a more specific MockUnitInt.

The input parameter for the method is a Function<T, Integer>.

Example for translating a MockUnit<Boolean> into a MockUnitInt by transforming true into 1 and false into 0:

MockUnitInt zeroOrOne = m.bools()
                         .mapToInt((b) -> (b) ? 1 : 0);

List<Integer> list = zeroOrOne.list(5).val();
// Possible Output: [0, 0, 0, 1, 0]

mapToLong()

This method is used to translate a MockUnit<T> into a more specific MockUnitLong.

The input parameter of the method is a Function<T, Long>.

Example for translating a MockUnit<List<Long>> into a MockUnitLong by calculating the sum of the numbers in the List<Long>:

MockUnit<List<Long>> muList = m.longs()
                                .range(0l, 100l)
                                .list(10);

Long sum = muList.mapToLong(list -> list.stream()
                                        .mapToLong(Long::longValue)
                                        .sum())
                 .val();

mapToString()

This method is used to translate a MockUnit<T> into a more specific MockUnitString.

val()

This method is used to return a single <T> value from the MockUnit<T>.

Example for generating a single boolean value that has a 35.5% probability of being true:

MockUnit<Boolean> boolUnit = mock.bools().probability(35.50);
Boolean val = boolUnit.val();

We can write all of the above simpler without keeping the reference:

Boolean val = mock.bools().probability(35.50).val();

valStr()

This method works exactly like val() but instead of the value, it first calls toString() on the object.

If the generated object is null it returns empty String.

String alwaysTrue = m.bools().probability(100.0).valStr();
// Possible Output: "true"

If the generated object is null, an alternative String value can be specified:

String nullll = m.from(new String[]{ null, null, null})
                 .valStr("NULLLL");
// Output: "NULLLL"

mapVals()

This allows us to build Map<T,R>s, mapping our MockUnit generated as keys for a given set of values.

The method allow us to:

  • specify the number of elements in the map;
  • specify the map implementation;

The values can be obtained from:

  • Another MockUnit;
  • From a Supplier<K>;
  • From an Iterable<K>;
  • From an generic array K[];
  • From primitives arrays: int[], double[], long[];

Example 1 Obtaining a Map<Boolean, Integer> from an array of int[]:

int[] values = { 100, 200, 300, 400, 500, 600 };
Map<Boolean, Integer> map = mock.bools().mapKeys(keys).val();

Possible output:

{false=500, true=600}

PS: It's normal to have only two keys false/true.

Example 2 Obtaining a Map<Boolean, Long> from another Supplier<Long>

Supplier<Long> longSupplier = () -> System.currentTimeMillis();
Map<Boolean, Long> map = mock.bools().mapVals(10, longSupplier).val();

Possible output:

{false=1487951761873, true=1487951761873}

stream()

Instead of obtaining a Collection, List, Set or a Map we can use the MockUnit<Boolean> to generate a Stream<Boolean>:

Stream<Boolean> stream = mock.bools().stream().val();

The Stream is infinite.

Clone this wiki locally