diff --git a/pom.xml b/pom.xml index f31dee5ba..2fd64d15e 100644 --- a/pom.xml +++ b/pom.xml @@ -109,6 +109,18 @@ alternative support for serializing POJOs as XML and deserializing XML as pojos. test + + + org.junit.jupiter + junit-jupiter + test + + + org.junit.jupiter + junit-jupiter-api + test + + diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestFailureExpected.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestFailureExpected.java new file mode 100644 index 000000000..4c08bd727 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestFailureExpected.java @@ -0,0 +1,38 @@ +package com.fasterxml.jackson.dataformat.xml.testutil.failure; + +import java.lang.annotation.*; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + *

+ * Annotation used to indicate that a JUnit-5 based tests method is expected to fail. + * + *

+ * When a test method is annotated with {@code @JacksonTestFailureExpected}, the + * {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution. + * If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test, + * indicating that the test was expected to fail but didn't. + *

+ * + *

Usage Example:

+ * + *

+ *
+ *     @Test
+ *     @JacksonTestFailureExpected
+ *     @Test
+    public void testFeatureNotYetImplemented() {
+ *         // Test code that is expected to fail
+ *     }
+ * }
+ * 
+ * + *

+ * + * @since 2.19 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(JacksonTestFailureExpectedInterceptor.class) +public @interface JacksonTestFailureExpected { } diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestFailureExpectedInterceptor.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestFailureExpectedInterceptor.java new file mode 100644 index 000000000..e46d981c5 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestFailureExpectedInterceptor.java @@ -0,0 +1,43 @@ +package com.fasterxml.jackson.dataformat.xml.testutil.failure; + +import java.lang.reflect.Method; + +import org.junit.jupiter.api.extension.*; + +/** + * Custom {@link InvocationInterceptor} that intercepts test method invocation. + * To pass the test ***only if*** test fails with an exception, and fail the test otherwise. + * + * @since 2.19 + */ +public class JacksonTestFailureExpectedInterceptor + implements InvocationInterceptor +{ + @Override + public void interceptTestMethod(Invocation invocation, + ReflectiveInvocationContext invocationContext, ExtensionContext extensionContext) + throws Throwable + { + try { + invocation.proceed(); + } catch (Throwable t) { + // do-nothing, we do expect an exception + return; + } + handleUnexpectePassingTest(invocationContext); + } + + private void handleUnexpectePassingTest(ReflectiveInvocationContext invocationContext) { + // Collect information we need + Object targetClass = invocationContext.getTargetClass(); + Object testMethod = invocationContext.getExecutable().getName(); + //List arguments = invocationContext.getArguments(); + + // Create message + String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod); + + // throw exception + throw new JacksonTestShouldFailException(message); + } + +} diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestShouldFailException.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestShouldFailException.java new file mode 100644 index 000000000..1436001f3 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/failure/JacksonTestShouldFailException.java @@ -0,0 +1,18 @@ +package com.fasterxml.jackson.dataformat.xml.testutil.failure; + +/** + * Exception used to alert that a test is passing, but should be failing. + * + * WARNING : This only for test code, and should never be thrown from production code. + * + * @since 2.19 + */ +public class JacksonTestShouldFailException + extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public JacksonTestShouldFailException(String msg) { + super(msg); + } +}