From fd99e77b1d33bd8a83211b97b943cb6ff9183608 Mon Sep 17 00:00:00 2001 From: sagaofsilence Date: Sun, 9 Jun 2024 17:31:50 +0530 Subject: [PATCH] Added test for ThrowingConsumer Added pom and ignore files. --- junit/junit5/functional-interfaces/.gitignore | 35 +++++++ junit/junit5/functional-interfaces/pom.xml | 41 ++++++++ .../functional/ValidationException.java | 7 ++ .../functional/ThrowingConsumerTest.java | 93 +++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 junit/junit5/functional-interfaces/.gitignore create mode 100644 junit/junit5/functional-interfaces/pom.xml create mode 100644 junit/junit5/functional-interfaces/src/main/java/io/reflectoring/functional/ValidationException.java create mode 100644 junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ThrowingConsumerTest.java diff --git a/junit/junit5/functional-interfaces/.gitignore b/junit/junit5/functional-interfaces/.gitignore new file mode 100644 index 00000000..57fb42c8 --- /dev/null +++ b/junit/junit5/functional-interfaces/.gitignore @@ -0,0 +1,35 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +.vscode + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/junit/junit5/functional-interfaces/pom.xml b/junit/junit5/functional-interfaces/pom.xml new file mode 100644 index 00000000..5b33f1e7 --- /dev/null +++ b/junit/junit5/functional-interfaces/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + io.reflectoring + junit5-functional-interfaces + 1.0-SNAPSHOT + + + 17 + 17 + 5.9.0 + + + + + org.junit.jupiter + junit-jupiter-api + ${junit.version} + test + + + + org.junit.jupiter + junit-jupiter-params + ${junit.version} + test + + + + org.hamcrest + hamcrest-all + 1.3 + test + + + + + diff --git a/junit/junit5/functional-interfaces/src/main/java/io/reflectoring/functional/ValidationException.java b/junit/junit5/functional-interfaces/src/main/java/io/reflectoring/functional/ValidationException.java new file mode 100644 index 00000000..a5d4f5d3 --- /dev/null +++ b/junit/junit5/functional-interfaces/src/main/java/io/reflectoring/functional/ValidationException.java @@ -0,0 +1,7 @@ +package io.reflectoring.functional; + +public class ValidationException extends Throwable { + public ValidationException(String message) { + super(message); + } +} diff --git a/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ThrowingConsumerTest.java b/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ThrowingConsumerTest.java new file mode 100644 index 00000000..b29cdc3f --- /dev/null +++ b/junit/junit5/functional-interfaces/src/test/java/io/reflectoring/functional/ThrowingConsumerTest.java @@ -0,0 +1,93 @@ +package io.reflectoring.functional; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.text.MessageFormat; +import java.time.temporal.ValueRange; +import java.util.Arrays; +import java.util.Collection; +import java.util.function.Function; +import java.util.stream.Stream; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.function.ThrowingConsumer; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +public class ThrowingConsumerTest { + @ParameterizedTest + @CsvSource({"50,true", "130,false", "-30,false"}) + void testMethodThatThrowsCheckedException(int percent, boolean valid) { + // acceptable percentage range: 0 - 100 + ValueRange validPercentageRange = ValueRange.of(0, 100); + final Function message = + input -> + MessageFormat.format( + "Percentage {0} should be in range {1}", input, validPercentageRange.toString()); + + ThrowingConsumer consumer = + input -> { + if (!validPercentageRange.isValidValue(input)) { + throw new ValidationException(message.apply(input)); + } + }; + + if (valid) { + assertDoesNotThrow(() -> consumer.accept(percent)); + } else { + assertAll( + () -> { + ValidationException exception = + assertThrows(ValidationException.class, () -> consumer.accept(percent)); + assertEquals(exception.getMessage(), message.apply(percent)); + }); + } + } + + @TestFactory + Stream testDynamicTestsWithThrowingConsumer() { + // acceptable percentage range: 0 - 100 + ValueRange validPercentageRange = ValueRange.of(0, 100); + final Function message = + input -> + MessageFormat.format( + "Percentage {0} should be in range {1}", input, validPercentageRange.toString()); + + // Define the ThrowingConsumer that validates the input percentage + ThrowingConsumer consumer = + testCase -> { + if (!validPercentageRange.isValidValue(testCase.percent)) { + throw new ValidationException(message.apply(testCase.percent)); + } + }; + + ThrowingConsumer executable = + testCase -> { + if (testCase.valid) { + assertDoesNotThrow(() -> consumer.accept(testCase)); + } else { + assertAll( + () -> { + ValidationException exception = + assertThrows(ValidationException.class, () -> consumer.accept(testCase)); + assertEquals(exception.getMessage(), message.apply(testCase.percent)); + }); + } + }; + // Test data: an array of test cases with inputs and their validity + Collection testCases = + Arrays.asList(new TestCase(50, true), new TestCase(130, false), new TestCase(-30, false)); + + Function displayNameGenerator = + testCase -> "Testing percentage: " + testCase.percent; + + // Generate dynamic tests + return DynamicTest.stream(testCases.stream(), displayNameGenerator, executable); + } + + // Helper record to represent a test case + record TestCase(int percent, boolean valid) {} +}