-
-
Notifications
You must be signed in to change notification settings - Fork 47
CustomMockUnits
Andrei Ciobanu edited this page Nov 13, 2018
·
5 revisions
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.Using the library:
Real World Examples: