All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wiremock.spring.internal.WireMockSpringExtension Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package org.wiremock.spring.internal;

import com.github.tomakehurst.wiremock.WireMockServer;
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.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.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 WireMockSpringExtension implements BeforeEachCallback, ParameterResolver {

  @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);
  }

  private static  void injectWireMockInstances(
      final ExtensionContext extensionContext,
      final Class annotation,
      final Function fn)
      throws IllegalAccessException {
    // getRequiredTestInstances() return multiple instances for nested tests
    for (final Object testInstance :
        extensionContext.getRequiredTestInstances().getAllInstances()) {
      final List annotatedFields =
          AnnotationSupport.findAnnotatedFields(testInstance.getClass(), annotation);
      for (final Field annotatedField : annotatedFields) {
        final T annotationValue = annotatedField.getAnnotation(annotation);
        annotatedField.setAccessible(true); // NOPMD

        final WireMockServer wiremock =
            Store.INSTANCE.findRequiredWireMockInstance(
                extensionContext, fn.apply(annotationValue));
        annotatedField.set(testInstance, wiremock);
      }
    }
  }

  @Override
  public boolean supportsParameter(
      final ParameterContext parameterContext, final ExtensionContext extensionContext) {
    return parameterContext.getParameter().getType() == WireMockServer.class
        && (parameterContext.isAnnotated(InjectWireMock.class));
  }

  @Override
  public Object resolveParameter(
      final ParameterContext parameterContext, final ExtensionContext extensionContext) {
    final String wireMockServerName =
        parameterContext.findAnnotation(InjectWireMock.class).get().value();
    return Store.INSTANCE.findRequiredWireMockInstance(extensionContext, wireMockServerName);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy