Search in sources :

Example 1 with ThrowingConsumer

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);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) TestFactory(org.junit.jupiter.api.TestFactory) Iterator(java.util.Iterator) Collection(java.util.Collection) Random(java.util.Random) DynamicNode(org.junit.jupiter.api.DynamicNode) Function(java.util.function.Function) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DynamicContainer.dynamicContainer(org.junit.jupiter.api.DynamicContainer.dynamicContainer) DynamicTest(org.junit.jupiter.api.DynamicTest) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Tag(org.junit.jupiter.api.Tag) ThrowingConsumer(org.junit.jupiter.api.function.ThrowingConsumer) Random(java.util.Random) Iterator(java.util.Iterator) TestFactory(org.junit.jupiter.api.TestFactory)

Example 2 with ThrowingConsumer

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);
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Stream(java.util.stream.Stream) TestFactory(org.junit.jupiter.api.TestFactory) Collection(java.util.Collection) UserDAO(com.stackify.daos.UserDAO) Assertions(org.junit.jupiter.api.Assertions) DynamicTest(org.junit.jupiter.api.DynamicTest) User(com.stackify.models.User) Function(java.util.function.Function) ThrowingConsumer(org.junit.jupiter.api.function.ThrowingConsumer) User(com.stackify.models.User) UserDAO(com.stackify.daos.UserDAO) TestFactory(org.junit.jupiter.api.TestFactory)

Aggregations

Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 List (java.util.List)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 DynamicTest (org.junit.jupiter.api.DynamicTest)2 TestFactory (org.junit.jupiter.api.TestFactory)2 ThrowingConsumer (org.junit.jupiter.api.function.ThrowingConsumer)2 UserDAO (com.stackify.daos.UserDAO)1 User (com.stackify.models.User)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 IntStream (java.util.stream.IntStream)1 Assertions (org.junit.jupiter.api.Assertions)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 DynamicContainer.dynamicContainer (org.junit.jupiter.api.DynamicContainer.dynamicContainer)1 DynamicNode (org.junit.jupiter.api.DynamicNode)1