GithubHelp home page GithubHelp logo

junit5-junitparams's Introduction

JUnit5-JunitParams

JUnitParams for JUnit5 Jupiter.

uncheck Annotation @Test is ignored by the extension as JUnit5 Jupiter discovers it as a standalone test, whereas the @Parameters one ships with concurrent @TestTemplate.

check Classic tests annotated with @Test only are discovered and run as usual.

Samples

@Parameters({
        "17, false",
        "22, true"})
public void isAdultAgeDirect(int age, boolean valid) throws Exception {
    assertThat(new Person(age).isAdult()).isEqualTo(valid);
}

@Test // annotation is ignored, method is only called twice as there is two set of parameters
@Parameters(method = "adultValues")
public void isAdultAgeDefinedMethod(int age, boolean valid) throws Exception {
    assertThat(new Person(age).isAdult()).isEqualTo(valid);
}

private Object[] adultValues() {
    return new Object[]{new Object[]{17, false}, new Object[]{22, true}};
}

@Parameters
public void isAdultAgeDefaultMethod(int age, boolean valid) throws Exception {
    assertThat(new Person(age).isAdult()).isEqualTo(valid);
}

@SuppressWarnings("unused")
private Object[] parametersForIsAdultAgeDefaultMethod() {
    return adultValues();
}

JUnit5 Jupiter current limitations

  • There is no proper way to retrieve the type of test from the ExtensionContext (isContainer, isTemplate)

    • JUnitParamsExtension extension currently use breakable internal class comparison
    private static Class<?> TEMPLATE_CONTEXT_CLASS =
      ReflectionUtils.loadClass(
          "org.junit.jupiter.engine.descriptor.TestTemplateContainerExtensionContext").get();
    
    boolean parentContextIsTemplateOne = TEMPLATE_CONTEXT_CLASS.equals(context.getParent().get().getClass());

    This is breakable as internal class are package-protected, and are not intended to be use from outside the framework

  • It is not possible to invoke non-static methods on the test instance from extensions

    • As JUnit5 Jupiter supports constructor parameter injection, all extensions except TestInstancePostProcessor run before test class instantiation
    • JUnitParamsExtension extension currently use breakable test class instantiation with default JUnit5 extensions (see ExtensionRegistry#DEFAULT_EXTENSIONS)
    Constructor<?> constructor = ReflectionUtils.getDeclaredConstructor(testClass);
    Object instance = executableInvoker
      .invoke(constructor,
              extensionContext,
              ExtensionRegistry.createRegistryWithDefaultExtensions(new EmptyConfigurationParameters()));

    This is breakable as both explicit and registered (by ServiceLoader mechanism) extensions cannot be used for constructor parameter injection for classes containing @Parameters tests.

junit5-junitparams's People

Contributors

ledoyen avatar

Watchers

 avatar  avatar  avatar

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.