GH-165 - Introduce ScenarioCustomizer extension.
We now provide a ScenarioCustomizer that can be used to prepare Scenario instances for test methods with a common customizer. This avoids the need to call ….customize(…) for all Scenarios declared in a test class with the same logic.
This commit is contained in:
@@ -74,6 +74,8 @@ public class Scenario {
|
||||
private final ApplicationEventPublisher publisher;
|
||||
private final AssertablePublishedEvents events;
|
||||
|
||||
private Function<ConditionFactory, ConditionFactory> defaultCustomizer;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Scenario} for the given {@link TransactionTemplate}, {@link ApplicationEventPublisher} and
|
||||
* {@link AssertablePublishedEvents}.
|
||||
@@ -95,6 +97,7 @@ public class Scenario {
|
||||
this.transactionOperations = new TransactionTemplate(transactionTemplate.getTransactionManager(), definition);
|
||||
this.publisher = publisher;
|
||||
this.events = events;
|
||||
this.defaultCustomizer = Function.identity();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,7 +200,22 @@ public class Scenario {
|
||||
|
||||
Assert.notNull(stimulus, "Stimulus must not be null!");
|
||||
|
||||
return new When<>(stimulus, __ -> {}, Function.identity());
|
||||
return new When<>(stimulus, __ -> {}, defaultCustomizer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension hook to allow registration of a global customizer. If none configured we will fall back to
|
||||
* {@link Function#identity()}.
|
||||
*
|
||||
* @param customizer must not be {@literal null}.
|
||||
* @see org.springframework.modulith.test.ScenarioCustomizer
|
||||
*/
|
||||
Scenario setDefaultCustomizer(Function<ConditionFactory, ConditionFactory> customizer) {
|
||||
|
||||
Assert.notNull(customizer, "Customizer must not be null!");
|
||||
|
||||
this.defaultCustomizer = customizer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public class When<T> {
|
||||
@@ -263,6 +281,10 @@ public class Scenario {
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the execution of the scenario. The given customizer will be added to the default one registered via a
|
||||
* {@link org.springframework.modulith.test.ScenarioCustomizer}. In other words, multiple invocations will replace
|
||||
* registrations made in previous calls but always be chained after the default customizations registered.
|
||||
*
|
||||
* @param customizer must not be {@literal null}.
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
@@ -270,7 +292,7 @@ public class Scenario {
|
||||
|
||||
Assert.notNull(customizer, "Customizer must not be null!");
|
||||
|
||||
return new When<T>(stimulus, cleanup, customizer);
|
||||
return new When<T>(stimulus, cleanup, defaultCustomizer.andThen(customizer));
|
||||
}
|
||||
|
||||
// Expect event
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.modulith.test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.awaitility.core.ConditionFactory;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.InvocationInterceptor;
|
||||
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A JUnit {@link InvocationInterceptor} to register a default customizer to be applied to all {@link Scenario}
|
||||
* instances associated with that test case.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public interface ScenarioCustomizer extends InvocationInterceptor {
|
||||
|
||||
/**
|
||||
* Return a customizer to be applied to the {@link Scenario} instance handed into the given method.
|
||||
*
|
||||
* @param method will never be {@literal null}.
|
||||
* @param context will never be {@literal null}.
|
||||
* @return must not be {@literal null}.
|
||||
*/
|
||||
Function<ConditionFactory, ConditionFactory> getDefaultCustomizer(Method method, ApplicationContext context);
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.junit.jupiter.api.extension.InvocationInterceptor#interceptTestTemplateMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext, org.junit.jupiter.api.extension.ExtensionContext)
|
||||
*/
|
||||
@Override
|
||||
default void interceptTestTemplateMethod(Invocation<Void> invocation,
|
||||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
|
||||
|
||||
prepareScenarioInstance(invocationContext, extensionContext);
|
||||
|
||||
invocation.proceed();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.junit.jupiter.api.extension.InvocationInterceptor#interceptTestFactoryMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext, org.junit.jupiter.api.extension.ExtensionContext)
|
||||
*/
|
||||
@Override
|
||||
default <T> T interceptTestFactoryMethod(Invocation<T> invocation,
|
||||
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
|
||||
|
||||
prepareScenarioInstance(invocationContext, extensionContext);
|
||||
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.junit.jupiter.api.extension.InvocationInterceptor#interceptTestMethod(org.junit.jupiter.api.extension.InvocationInterceptor.Invocation, org.junit.jupiter.api.extension.ReflectiveInvocationContext, org.junit.jupiter.api.extension.ExtensionContext)
|
||||
*/
|
||||
@Override
|
||||
default void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
|
||||
ExtensionContext extensionContext) throws Throwable {
|
||||
|
||||
prepareScenarioInstance(invocationContext, extensionContext);
|
||||
|
||||
invocation.proceed();
|
||||
}
|
||||
|
||||
private void prepareScenarioInstance(ReflectiveInvocationContext<Method> invocationContext,
|
||||
ExtensionContext extensionContext) {
|
||||
|
||||
invocationContext.getArguments().stream()
|
||||
.filter(Scenario.class::isInstance)
|
||||
.map(Scenario.class::cast)
|
||||
.forEach(it -> {
|
||||
|
||||
var context = SpringExtension.getApplicationContext(extensionContext);
|
||||
var customizer = getDefaultCustomizer(invocationContext.getExecutable(), context);
|
||||
Assert.state(customizer != null, "Customizer must not be null!");
|
||||
|
||||
it.setDefaultCustomizer(customizer);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user