ReactorContextTestExecutionListener should use named hooks

Fixes: gh-6075
This commit is contained in:
Aanuoluwapo Otitoola 2018-11-20 20:00:04 +01:00 committed by Rob Winch
parent 18594ef4e9
commit a28c677f88
2 changed files with 13 additions and 2 deletions

View File

@ -44,6 +44,7 @@ public class ReactorContextTestExecutionListener
extends DelegatingTestExecutionListener { extends DelegatingTestExecutionListener {
private static final String HOOKS_CLASS_NAME = "reactor.core.publisher.Hooks"; private static final String HOOKS_CLASS_NAME = "reactor.core.publisher.Hooks";
private static final String CONTEXT_OPERATOR_KEY = SecurityContext.class.getName();
public ReactorContextTestExecutionListener() { public ReactorContextTestExecutionListener() {
super(createDelegate()); super(createDelegate());
@ -59,12 +60,12 @@ public class ReactorContextTestExecutionListener
@Override @Override
public void beforeTestMethod(TestContext testContext) throws Exception { public void beforeTestMethod(TestContext testContext) throws Exception {
SecurityContext securityContext = TestSecurityContextHolder.getContext(); 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 @Override
public void afterTestMethod(TestContext testContext) throws Exception { public void afterTestMethod(TestContext testContext) throws Exception {
Hooks.resetOnLastOperator(); Hooks.resetOnLastOperator(CONTEXT_OPERATOR_KEY);
} }
private static class SecuritySubContext<T> implements CoreSubscriber<T> { private static class SecuritySubContext<T> implements CoreSubscriber<T> {

View File

@ -174,6 +174,16 @@ public class ReactorContextTestExecutionListenerTests {
assertThat(Mono.subscriberContext().block().isEmpty()).isTrue(); 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 @Test
public void orderWhenComparedToWithSecurityContextTestExecutionListenerIsAfter() { public void orderWhenComparedToWithSecurityContextTestExecutionListenerIsAfter() {
OrderComparator comparator = new OrderComparator(); OrderComparator comparator = new OrderComparator();