use of org.junit.jupiter.api.function.ThrowingConsumer in project junit5 by junit-team.
the class DynamicTestsDemo method generateRandomNumberOfTests.
@TestFactory
Stream<DynamicTest> generateRandomNumberOfTests() {
// Generates random positive integers between 0 and 100 until
// a number evenly divisible by 7 is encountered.
Iterator<Integer> inputGenerator = new Iterator<Integer>() {
Random random = new Random();
// end::user_guide[]
{
// Use fixed seed to always produce the same number of tests for execution on the CI server
random = new Random(23);
}
// tag::user_guide[]
int current;
@Override
public boolean hasNext() {
current = random.nextInt(100);
return current % 7 != 0;
}
@Override
public Integer next() {
return current;
}
};
// Generates display names like: input:5, input:37, input:85, etc.
Function<Integer, String> displayNameGenerator = (input) -> "input:" + input;
// Executes tests based on the current input value.
ThrowingConsumer<Integer> testExecutor = (input) -> assertTrue(input % 7 != 0);
// Returns a stream of dynamic tests.
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
use of org.junit.jupiter.api.function.ThrowingConsumer in project tutorials by eugenp.
the class DynamicTests method dynamicUserTestCollection.
@TestFactory
public Stream<DynamicTest> dynamicUserTestCollection() {
List<User> inputList = Arrays.asList(new User("john@yahoo.com", "John"), new User("ana@yahoo.com", "Ana"));
Function<User, String> displayNameGenerator = (input) -> "Saving user: " + input;
UserDAO userDAO = new UserDAO();
ThrowingConsumer<User> testExecutor = (input) -> {
userDAO.add(input);
assertNotNull(userDAO.findOne(input.getEmail()));
};
return DynamicTest.stream(inputList.iterator(), displayNameGenerator, testExecutor);
}
Aggregations