Search in sources :

Example 6 with TestFactory

use of org.junit.jupiter.api.TestFactory in project connect-utils by jcustenborder.

the class ValueHelperTest method value.

@TestFactory
Stream<DynamicTest> value() {
    List<TestCase> tests = new ArrayList<>();
    of(tests, Schema.FLOAT64_SCHEMA, new Double(Double.MAX_VALUE), new Double(Double.MAX_VALUE));
    of(tests, Schema.FLOAT64_SCHEMA, new Double(Double.MIN_VALUE), new Double(Double.MIN_VALUE));
    of(tests, Schema.INT8_SCHEMA, new Byte(Byte.MAX_VALUE), new Byte(Byte.MAX_VALUE));
    of(tests, Schema.INT8_SCHEMA, new Byte(Byte.MIN_VALUE), new Byte(Byte.MIN_VALUE));
    of(tests, Schema.INT16_SCHEMA, new Short(Short.MAX_VALUE), new Short(Short.MAX_VALUE));
    of(tests, Schema.INT16_SCHEMA, new Short(Short.MIN_VALUE), new Short(Short.MIN_VALUE));
    of(tests, Schema.INT32_SCHEMA, new Integer(Integer.MAX_VALUE), new Integer(Integer.MAX_VALUE));
    of(tests, Schema.INT32_SCHEMA, new Integer(Integer.MIN_VALUE), new Integer(Integer.MIN_VALUE));
    of(tests, Schema.INT64_SCHEMA, new Long(Long.MAX_VALUE), new Long(Long.MAX_VALUE));
    of(tests, Schema.INT64_SCHEMA, new Long(Long.MIN_VALUE), new Long(Long.MIN_VALUE));
    of(tests, Schema.STRING_SCHEMA, "", "");
    of(tests, Schema.STRING_SCHEMA, "mirror", "mirror");
    for (int SCALE = 3; SCALE < 30; SCALE++) {
        Schema schema = Decimal.schema(SCALE);
        of(tests, schema, new BigDecimal("12345").setScale(SCALE), new BigDecimal("12345").setScale(SCALE));
        of(tests, schema, new BigDecimal("0").setScale(SCALE), new BigDecimal("0").setScale(SCALE));
        of(tests, schema, new BigDecimal("-12345.001").setScale(SCALE), new BigDecimal("-12345.001").setScale(SCALE));
    }
    Schema structSchema = SchemaBuilder.struct().field("firstName", Schema.STRING_SCHEMA).field("lastName", Schema.STRING_SCHEMA).build();
    of(tests, structSchema, ImmutableMap.of("firstName", "example", "lastName", "user"), new Struct(structSchema).put("firstName", "example").put("lastName", "user"));
    of(tests, structSchema, new Struct(structSchema).put("firstName", "example").put("lastName", "user"), new Struct(structSchema).put("firstName", "example").put("lastName", "user"));
    return tests.stream().map(testCase -> dynamicTest(testCase.toString(), () -> {
        final Object actual = ValueHelper.value(testCase.schema, testCase.input);
        assertEquals(testCase.expected, actual);
    }));
}
Also used : Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Struct(org.apache.kafka.connect.data.Struct) TestFactory(org.junit.jupiter.api.TestFactory)

Example 7 with TestFactory

use of org.junit.jupiter.api.TestFactory in project junit5 by junit-team.

the class ExtensionContextTests method configurationParameter.

@TestFactory
Stream<DynamicTest> configurationParameter() throws Exception {
    ConfigurationParameters echo = new EchoParameters();
    String key = "123";
    Optional<String> expected = Optional.of(key);
    UniqueId engineUniqueId = UniqueId.parse("[engine:junit-jupiter]");
    JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(engineUniqueId);
    UniqueId classUniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]");
    ClassTestDescriptor classTestDescriptor = new ClassTestDescriptor(classUniqueId, getClass());
    Method method = getClass().getDeclaredMethod("configurationParameter");
    UniqueId methodUniqueId = UniqueId.parse("[engine:junit-jupiter]/[class:MyClass]/[method:myMethod]");
    TestMethodTestDescriptor methodTestDescriptor = new TestMethodTestDescriptor(methodUniqueId, getClass(), method);
    return // 
    Stream.of(// 
    (ExtensionContext) new JupiterEngineExtensionContext(null, engineDescriptor, echo), // 
    new ClassExtensionContext(null, null, classTestDescriptor, echo, null), // 
    new MethodExtensionContext(null, null, methodTestDescriptor, echo, null, null)).map(context -> dynamicTest(context.getClass().getSimpleName(), () -> assertEquals(expected, context.getConfigurationParameter(key))));
}
Also used : UniqueId(org.junit.platform.engine.UniqueId) Method(java.lang.reflect.Method) ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) NestedClassTestDescriptor(org.junit.jupiter.engine.descriptor.NestedClassTestDescriptor) ClassTestDescriptor(org.junit.jupiter.engine.descriptor.ClassTestDescriptor) JupiterEngineDescriptor(org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) MethodExtensionContext(org.junit.jupiter.engine.descriptor.MethodExtensionContext) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) ClassExtensionContext(org.junit.jupiter.engine.descriptor.ClassExtensionContext) JupiterEngineExtensionContext(org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext) TestMethodTestDescriptor(org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor) TestFactory(org.junit.jupiter.api.TestFactory)

Example 8 with TestFactory

use of org.junit.jupiter.api.TestFactory in project junit5 by junit-team.

the class ScriptTests method preconditionsAreChecked.

@TestFactory
Stream<DynamicTest> preconditionsAreChecked(TestInfo info) {
    Annotation annotation = info.getTestMethod().orElseThrow(Error::new).getAnnotation(TestFactory.class);
    Class<JUnitException> expected = JUnitException.class;
    return // 
    Stream.of(// 
    dynamicTest("0", () -> assertNotNull(new Script(annotation, "e", "s", "r"))), // 
    dynamicTest("1", () -> assertThrows(expected, () -> new Script(null, "e", "s", "r"))), // 
    dynamicTest("2", () -> assertNotNull(new Script(Test.class, "a", "e", "s", "r"))), // 
    dynamicTest("3", () -> assertThrows(expected, () -> new Script(null, "a", "e", "s", "r"))), // 
    dynamicTest("4", () -> assertThrows(expected, () -> new Script(Test.class, null, "e", "s", "r"))), // 
    dynamicTest("5", () -> assertThrows(expected, () -> new Script(Test.class, "a", null, "s", "r"))), // 
    dynamicTest("6", () -> assertThrows(expected, () -> new Script(Test.class, "a", "e", null, "r"))), // 
    dynamicTest("7", () -> assertThrows(expected, () -> new Script(Test.class, "a", "e", "s", null))), // 
    dynamicTest("8", () -> assertNotNull(new Script(Test.class, "", "e", "s", ""))), // 
    dynamicTest("9", () -> assertThrows(expected, () -> new Script(Test.class, "", "", "s", ""))), // 
    dynamicTest("A", () -> assertThrows(expected, () -> new Script(Test.class, "", "e", "", ""))));
}
Also used : Test(org.junit.jupiter.api.Test) DynamicTest.dynamicTest(org.junit.jupiter.api.DynamicTest.dynamicTest) DynamicTest(org.junit.jupiter.api.DynamicTest) JUnitException(org.junit.platform.commons.JUnitException) Annotation(java.lang.annotation.Annotation) TestFactory(org.junit.jupiter.api.TestFactory)

Example 9 with TestFactory

use of org.junit.jupiter.api.TestFactory 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 10 with TestFactory

use of org.junit.jupiter.api.TestFactory in project junit5 by junit-team.

the class ReflectionSupportTests method findAllClassesInClasspathRootDelegates.

@TestFactory
List<DynamicTest> findAllClassesInClasspathRootDelegates() throws Throwable {
    List<DynamicTest> tests = new ArrayList<>();
    List<Path> paths = new ArrayList<>();
    paths.add(Paths.get(".").toRealPath());
    paths.addAll(ReflectionUtils.getAllClasspathRootDirectories());
    for (Path path : paths) {
        URI root = path.toUri();
        String displayName = root.getPath();
        if (displayName.length() > 42) {
            displayName = "..." + displayName.substring(displayName.length() - 42);
        }
        tests.add(DynamicTest.dynamicTest(displayName, () -> assertEquals(ReflectionUtils.findAllClassesInClasspathRoot(root, allTypes, allNames), ReflectionSupport.findAllClassesInClasspathRoot(root, allTypes, allNames))));
    }
    return tests;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) URI(java.net.URI) DynamicTest(org.junit.jupiter.api.DynamicTest) TestFactory(org.junit.jupiter.api.TestFactory)

Aggregations

TestFactory (org.junit.jupiter.api.TestFactory)19 ArrayList (java.util.ArrayList)9 DynamicTest (org.junit.jupiter.api.DynamicTest)8 File (java.io.File)5 List (java.util.List)5 Arrays (java.util.Arrays)4 Stream (java.util.stream.Stream)4 Schema (org.apache.kafka.connect.data.Schema)4 BigDecimal (java.math.BigDecimal)3 Collection (java.util.Collection)3 Function (java.util.function.Function)3 DynamicTest.dynamicTest (org.junit.jupiter.api.DynamicTest.dynamicTest)3 ThrowingConsumer (org.junit.jupiter.api.function.ThrowingConsumer)3 Map (java.util.Map)2 Struct (org.apache.kafka.connect.data.Struct)2 Test (org.junit.jupiter.api.Test)2 Employee (com.baeldung.helpers.Employee)1 EmployeeDao (com.baeldung.helpers.EmployeeDao)1 ChangeKey (com.github.jcustenborder.kafka.connect.cdc.ChangeKey)1 AssertStruct.assertStruct (com.github.jcustenborder.kafka.connect.utils.AssertStruct.assertStruct)1