Navigation Menu

Skip to content

CustomMockUnits

Andrei Ciobanu edited this page Jan 10, 2019 · 5 revisions

Important Note: The library the documentation has moved to www.mockneat.com. The information found on this wiki is quite outdated. Please check the new site.


MockUnit<T> is a Functional Interface that contains only one abstract method: Supplier<T> supplier(). The supplier() method is the one that generates the data.

Example: Creating a MockUnit<Boolean> that will always return true:

MockUnit<Boolean> alwaysTrue = () -> () -> true;
// This creates a list of boolean values with length 100 that will always return true
System.out.println(alwaysTrue.list(100).val());

In a way we can consider a MockUnit<T> a Supplier<Supplier<T>>, because equally we can write:

Supplier<Supplier<Boolean>> supOfSup = () -> () -> true;

The biggest difference being the fact the MockUnit<T> has more convenient methods to transform data.

Example: Creating a custom MockUnitString that returns random strings with the following format: [letter][digit][letter]. Examples: "a2b", "w9z", "f8k", etc.

Supplier<String> supp = () -> {
    StringBuilder buff = new StringBuilder();
    buff.append(mock.chars().digits().val())
        .append(mock.chars().lowerLetters().val())
        .append(mock.chars().digits().val());
    return buff.toString();
};
MockUnitString mockUnit = () -> supp;

// The custom created mockUnit can now be used
// as any other MockUnitString
String cStr = mockUnit.val();
List<String> lStr = mockUnit.list(10).val();
//etc.