All Projects → Pragmatists → junitparams-spring-integration-example

Pragmatists / junitparams-spring-integration-example

Licence: other
Example of JUnitParams integration with Spring

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to junitparams-spring-integration-example

QshOni
The QShell on IBM i library contains useful CL wrapper commands to allow QShell and PASE apps to be called and consumed from regular IBM i jobs via CL, RPG or COBOL programs.
Stars: ✭ 34 (+126.67%)
Mutual labels:  runner
uiLogos-sketch-plugin
Sketch plugin to Insert professionally designed dummy logos of companies and 190+ country flag into SketchApp
Stars: ✭ 26 (+73.33%)
Mutual labels:  runner
python-appium-framework
Complete Python Appium framework in 360 degree
Stars: ✭ 43 (+186.67%)
Mutual labels:  runner
land
Run Deno X module without installation.
Stars: ✭ 39 (+160%)
Mutual labels:  runner
jest-runner-go
A Golang runner for Jest
Stars: ✭ 22 (+46.67%)
Mutual labels:  runner
t-rex-game-bot
A bot that plays the Google Chrome T-Rex game for you
Stars: ✭ 60 (+300%)
Mutual labels:  runner
Extension Create
Create modern cross-browser extensions with no build configuration.
Stars: ✭ 167 (+1013.33%)
Mutual labels:  runner
Elaina
🔮 Docker-based remote code runner. / 基于 Docker 的远程代码运行器
Stars: ✭ 36 (+140%)
Mutual labels:  runner
EasyJob
🔨 EasyJob - keep and execute your PowerShell and BAT scripts from one interface
Stars: ✭ 228 (+1420%)
Mutual labels:  runner
cargo-limit
Cargo with less noise: warnings are skipped until errors are fixed, Neovim integration, etc.
Stars: ✭ 105 (+600%)
Mutual labels:  runner
nano-staged
Tiny tool to run commands for modified, staged, and committed files in a GIT repository.
Stars: ✭ 347 (+2213.33%)
Mutual labels:  runner
dash.nvim
Script runner for quick iteration. Bring your scripting to the next level.
Stars: ✭ 37 (+146.67%)
Mutual labels:  runner
EllaTheGame
Ella - The Game
Stars: ✭ 39 (+160%)
Mutual labels:  runner
await
28Kb, small memory footprint, single binary that run list of commands in parallel and waits for their termination
Stars: ✭ 73 (+386.67%)
Mutual labels:  runner
duty
A simple task runner.
Stars: ✭ 36 (+140%)
Mutual labels:  runner
Task Easy
A simple, customizable, and lightweight priority queue for promises.
Stars: ✭ 244 (+1526.67%)
Mutual labels:  runner
coderun
⏯️ Code runner CLI that can run any languages
Stars: ✭ 23 (+53.33%)
Mutual labels:  runner
instant-mocha
☕️ Build tests with Webpack and run with Mocha in one command
Stars: ✭ 39 (+160%)
Mutual labels:  runner
taskrunner
🍑 a configurable task runner written in go
Stars: ✭ 28 (+86.67%)
Mutual labels:  runner
Swatch
Watcher for Unit Tests written in Swift
Stars: ✭ 55 (+266.67%)
Mutual labels:  runner

JUnitParams integration with Spring

Problems with integration SpringJUnit4ClassRunner with JUnitParamsRunner

There was a known problem with running tests which start spring context combined with JUnitParams. Main reason was that running spring tests required usage of runner SpringJUnit4ClassRunner and it eliminates option to use JUnitParamsRunner. JUnitParams runner must be top junit runner. Combining it with spring was impossible until now.

Alternative way to start spring context

Since spring version 4.2 there were introduced two classes that help us start spring without any special runner defined:

According to its documentation to fully replace SpringJUnit4ClassRunner we need to always defined both of these rules because first supports all annotations at class level and second all annotations at instance and method level.

Example test which uses this mechanism can be modified version of CalculationEndpointTest but without all annotations related to JUnitParams.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CalculationEndpointTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Test
    public void spring_injection() throws Exception {
        assertThat(restTemplate).isNotNull();
    }

}

Running JUnitParamsRunner with spring

Now it is easy to combine JUnitParamsRunner with spring. As demonstrated in CalculationEndpointTest we just need to add proper JUnitParams annotations. Example is quite straightforward. It starts spring boot application with simple rest controller which takes json as parameter and multiplies passed multiplier and value and returns it as json response. We supply test (with @Parameters annotation) with set of multiplier value and expected multiplied value f.e. 2, 3, 6 which means we expect 6 out of multiplication of 2 and 3. Started spring boot indicates that annotation @SpringBootTest was applied as well as injecting restTemplate with @Autowired.

@RunWith(JUnitParamsRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CalculationEndpointTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Test
    @Parameters({
            "2, 3, 6",
            "3, 3, 9",
            "0, 3, 0",
            "3, 0, 0"
    })
    public void multiply_values(int multiplier, int value, int expectedMultipliedValue) throws Exception {
        MultiplyOperationResponseJson multiplyOperationResponseJson = restTemplate.postForObject(
                "/multiply/", new MultiplyOperationRequestJson(multiplier, value), MultiplyOperationResponseJson.class
        );

        assertThat(multiplyOperationResponseJson.multipliedValue).isEqualTo(expectedMultipliedValue);
    }
}

Older version of spring and JUnitParams

What if you are using older version of spring and you can not migrate to newer one. Generally it is possible to implement SpringClassRule and SpringMethodRule by copying them from spring 4.2 to your project. You will also need to copy class RunPrepareTestInstanceCallbacks.

In SpringClassRule you need to remove line

statement = withProfileValueCheck(statement, testClass);

from apply method (and related methods code).

In SpringMethodRule you need also small modifications. Remove

statement = withPotentialRepeat(statement, frameworkMethod, testInstance);
statement = withPotentialTimeout(statement, frameworkMethod, testInstance);
statement = withProfileValueCheck(statement, frameworkMethod, testInstance);

from apply method (and related methods code).

Thanks to these you can now use your own created rules but as you see you are losing support for annotations:

  • @Repeat
  • @IfProfileValue
  • @Timed

If you have any problems with these modifications let us know.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].