Giter Site home page Giter Site logo

parameters-extended's Introduction

JUnit 5 - parameters extended

By default, in JUnit 5, it is possible to inject parameters into a test method in different ways. This project extends the possibility in a very simple manner to introduce a new behavior.

Purpose

In JUnit 5, when a method annotated with @BeforeAll is used for creating objects used throughout the tests, the created instances need to be stored in static fields.

To encapsulate and remove possibility of side effect, where multiple tests may (for some reason) modify such variables, this project has been introduced.

Additionally, it adds functionality of named parameters.

Usage

To use extension, annotate your test class with:

@ExtendWith(ParamsExtended.class)
void TestClass { ... }

In set up method annotated with @Parameters.Setup, parameters can be set following way:

import org.junit.jupiter.api.BeforeAll;

@ExtendWith(ParamsExtended.class)
void TestClass {

    @Parameters.Setup
    private static void setUp(Parameters parameters) {
        parameters
                .add(new Integer(5))
                .addNamed("another", "Hello World!");
    }

    /* [...] */
}

Then, a test method can intercept the registered parameters:

import org.junit.jupiter.api.Test;

    /* [...] */
    @Test
    void test(Integer unnamed, @Named("another") String named) {/*[...]*/}
    /* [...] */

Test method can also directly get Parameters instance, however modification is only possible in a method annotated with @Parameters.Setup.

import org.junit.jupiter.api.Test;

    /* [...] */
    @Test
    void test2(Parameters parameters) {
        /*
        parameters.add(...) and parameters.addNamed(...) are not allowed,
        as instance injected into the test methods is read only.
        */

        final Integer unnamed = parameters.get(Integer.class);
        final String named = parameters.get("another");
    }
    /* [...] */

parameters-extended's People

Contributors

adinef avatar

Watchers

 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.