From a28c677f88c25468d4cd7d30a6919528a0fbd31f Mon Sep 17 00:00:00 2001 From: Aanuoluwapo Otitoola Date: Tue, 20 Nov 2018 20:00:04 +0100 Subject: [PATCH] ReactorContextTestExecutionListener should use named hooks Fixes: gh-6075 --- .../support/ReactorContextTestExecutionListener.java | 5 +++-- .../ReactorContextTestExecutionListenerTests.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java b/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java index 12808db9c7..aef4dd0af9 100644 --- a/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java +++ b/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java @@ -44,6 +44,7 @@ public class ReactorContextTestExecutionListener extends DelegatingTestExecutionListener { private static final String HOOKS_CLASS_NAME = "reactor.core.publisher.Hooks"; + private static final String CONTEXT_OPERATOR_KEY = SecurityContext.class.getName(); public ReactorContextTestExecutionListener() { super(createDelegate()); @@ -59,12 +60,12 @@ public class ReactorContextTestExecutionListener @Override public void beforeTestMethod(TestContext testContext) throws Exception { SecurityContext securityContext = TestSecurityContextHolder.getContext(); - Hooks.onLastOperator(Operators.lift((s, sub) -> new SecuritySubContext<>(sub, securityContext))); + Hooks.onLastOperator(CONTEXT_OPERATOR_KEY, Operators.lift((s, sub) -> new SecuritySubContext<>(sub, securityContext))); } @Override public void afterTestMethod(TestContext testContext) throws Exception { - Hooks.resetOnLastOperator(); + Hooks.resetOnLastOperator(CONTEXT_OPERATOR_KEY); } private static class SecuritySubContext implements CoreSubscriber { diff --git a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java index dd983f88d7..1b1a7e1bca 100644 --- a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java +++ b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java @@ -174,6 +174,16 @@ public class ReactorContextTestExecutionListenerTests { assertThat(Mono.subscriberContext().block().isEmpty()).isTrue(); } + @Test + public void afterTestMethodWhenDifferentHookIsRegistered() throws Exception { + Object obj = new Object(); + + Hooks.onLastOperator("CUSTOM_HOOK", p -> Mono.just(obj)); + this.listener.afterTestMethod(this.testContext); + + assertThat(Mono.subscriberContext().block()).isEqualTo(obj); + } + @Test public void orderWhenComparedToWithSecurityContextTestExecutionListenerIsAfter() { OrderComparator comparator = new OrderComparator();