Skip to content

Commit ecce4cc

Browse files
authored
Merging parts of #4696 separately to ease merging into 3.0 (#4707)
1 parent ef871bb commit ecce4cc

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fasterxml.jackson.databind.testutil.failure;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* <p>
12+
* Annotation used to indicate that a JUnit-5 based tests method is expected to fail.
13+
*
14+
* <p>
15+
* When a test method is annotated with {@code @JacksonTestFailureExpected}, the
16+
* {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution.
17+
* If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test,
18+
* indicating that the test was expected to fail but didn't.
19+
* </p>
20+
*
21+
* <h3>Usage Example:</h3>
22+
*
23+
* <pre><code>
24+
*
25+
* &#64;Test
26+
* &#64;JacksonTestFailureExpected
27+
* public void testFeatureNotYetImplemented() {
28+
* // Test code that is expected to fail
29+
* }
30+
* }
31+
* </code></pre>
32+
*
33+
* <p>
34+
*
35+
* @since 2.19
36+
*/
37+
@Target({ElementType.METHOD})
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@ExtendWith(JacksonTestFailureExpectedInterceptor.class)
40+
public @interface JacksonTestFailureExpected { }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fasterxml.jackson.databind.testutil.failure;
2+
3+
import org.junit.jupiter.api.extension.ExtensionContext;
4+
import org.junit.jupiter.api.extension.InvocationInterceptor;
5+
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
6+
7+
import java.lang.reflect.Method;
8+
import java.util.List;
9+
10+
/**
11+
* Custom {@link InvocationInterceptor} that intercepts test method invocation.
12+
* To pass the test ***only if*** test fails with an exception, and fail the test otherwise.
13+
*
14+
* @since 2.19
15+
*/
16+
public class JacksonTestFailureExpectedInterceptor
17+
implements InvocationInterceptor
18+
{
19+
20+
@Override
21+
public void interceptTestMethod(Invocation<Void> invocation,
22+
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext)
23+
throws Throwable {
24+
try {
25+
invocation.proceed();
26+
} catch (Throwable t) {
27+
// do-nothing, we do expect an exception
28+
return;
29+
}
30+
handleUnexpectePassingTest(invocationContext);
31+
}
32+
33+
private void handleUnexpectePassingTest(ReflectiveInvocationContext<Method> invocationContext) {
34+
// Collect information we need
35+
Object targetClass = invocationContext.getTargetClass();
36+
Object testMethod = invocationContext.getExecutable().getName();
37+
List<Object> arguments = invocationContext.getArguments();
38+
39+
// Create message
40+
String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod);
41+
42+
// throw exception
43+
throw new JacksonTestShouldFailException(message);
44+
}
45+
46+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.fasterxml.jackson.databind.testutil.failure;
2+
3+
/**
4+
* Exception used to alert that a test is passing, but should be failing.
5+
*
6+
* WARNING : This only for test code, and should never be thrown from production code.
7+
*
8+
* @since 2.19
9+
*/
10+
public class JacksonTestShouldFailException
11+
extends RuntimeException
12+
{
13+
public JacksonTestShouldFailException(String msg) {
14+
super(msg);
15+
}
16+
}

0 commit comments

Comments
 (0)