org.wiremock.spring.internal.WireMockSpringJunitExtension Maven / Gradle / Ivy
package org.wiremock.spring.internal;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.platform.commons.support.AnnotationSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.InjectWireMock;
/**
* JUnit 5 extension that sets {@link com.github.tomakehurst.wiremock.WireMockServer} instances
* previously registered with {@link org.wiremock.spring.ConfigureWireMock} on test class fields.
*
* @author Maciej Walkowiak
*/
public class WireMockSpringJunitExtension
implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockSpringJunitExtension.class);
@Override
public void beforeEach(final ExtensionContext extensionContext) throws Exception {
// reset all wiremock servers associated with application context
Store.INSTANCE.findAllInstances(extensionContext).forEach(WireMockServer::resetAll);
// inject properties into test class fields
injectWireMockInstances(extensionContext, InjectWireMock.class, InjectWireMock::value);
this.configureWireMockForDefaultInstance(extensionContext);
}
private void configureWireMockForDefaultInstance(final ExtensionContext extensionContext) {
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy