Giter VIP home page Giter VIP logo

wiremock-junit-jupiter's People

Contributors

ddovgal avatar sparkmuse avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

wiremock-junit-jupiter's Issues

WiremockExtension#retrieveAnnotatedFields method throws ClassCastException

I have stumbled upon an issue when using the latest 1.1.18 version of current wirempock jupiter extension.
The problem is related to the case when it is time to reset wire mock stubs after each test - I provided a code snippet from com/github/sparkmuse/wiremock/WiremockExtension.java source file:

@Override
 public void afterEach(ExtensionContext extensionContext) {
     // because we are in afterEach, ExtensionContext actually is a MethodExtensionContext which always has non-empty parent
     @SuppressWarnings("OptionalGetWithoutIsPresent")
     ExtensionContext classContext = extensionContext.getParent().get();
     List<Field> serverFields = retrieveAnnotatedFields(classContext, Wiremock.class, WireMockServer.class);
     ExtensionContext.Store store = getStore(classContext);

     serverFields.stream()
             .map(field -> store.get(field.getName(), WireMockServer.class))
             .forEach(WireMockServer::resetAll);
 }

As it was stated in the comment extensionContext is instance of MethodExtensionContext and taking into account below context hierarchy:

MethodExtensionContext
TestTemplateExtensionContext
ClassExtensionContext
JupiterEngineExtensionContext

When attempt to retrive annotatated element is made in the downtream retrieveAnnotatedFields method - actual test method instance of java reflcetion Method type will be returned and subsequent try to cast it to Class type fails.

	private static List<Field> retrieveAnnotatedFields(ExtensionContext context,
                                                       Class<? extends Annotation> annotationType,
                                                       Class<?> fieldType) {
        return context.getElement()
                .map(Class.class::cast)
                .map(clazz -> findAnnotatedFields(clazz, annotationType, getFieldPredicate(fieldType)))
                .orElse(Collections.emptyList());
    }

Maybe - it will be sufficient to switch over to ClassExtensionContext or JupiterEngineExtensionContext (root) context once it is time to reset stubs.

Thanks!

afterEach method doesn't find serverFields

I Will try to be as simple as possible, WiremockExtension:

@Override
public void afterEach(ExtensionContext extensionContext) {
    List<Field> serverFields = retrieveAnnotatedFields(extensionContext.getRoot(), Wiremock.class, WireMockServer.class);
    ExtensionContext.Store store = getStore(extensionContext);

    serverFields.stream()
                .map(field -> store.get(field.getName(), WireMockServer.class))
                .forEach(WireMockServer::resetAll);
}

here serverFields is always empty. I suppose it's because extensionContext.getRoot() returns org.junit.jupiter.engine.descriptor.JupiterEngineExtensionContext instead of the org.junit.jupiter.engine.descriptor.ClassExtensionContext (as in postProcessTestInstance wich works normally for example).

Changing to

@Override
public void afterEach(ExtensionContext extensionContext) {
    List<Field> serverFields = retrieveAnnotatedFields(extensionContext.getParent().get(), Wiremock.class, WireMockServer.class);
    ExtensionContext.Store store = getStore(extensionContext.getParent().get());

    serverFields.stream()
                .map(field -> store.get(field.getName(), WireMockServer.class))
                .forEach(WireMockServer::resetAll);
}

makes method able to find field and execute resetAll, BUT as I'm not very familiar with JUnit extension API, I'm not sure that extensionContext.getParent() will always be non-empty optional and this is the right way to solve the issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.