Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Example of JUnitParams integration with Spring

NotificationsYou must be signed in to change notification settings

Pragmatists/junitparams-spring-integration-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 runnerSpringJUnit4ClassRunner 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

Sincespring 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 becausefirst 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 ofCalculationEndpointTestbut without all annotations related to JUnitParams.

@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)publicclassCalculationEndpointTest {@AutowiredprivateTestRestTemplaterestTemplate;@ClassRulepublicstaticfinalSpringClassRuleSPRING_CLASS_RULE =newSpringClassRule();@RulepublicfinalSpringMethodRulespringMethodRule =newSpringMethodRule();@Testpublicvoidspring_injection()throwsException {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 jsonas 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 outof 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)publicclassCalculationEndpointTest {@AutowiredprivateTestRestTemplaterestTemplate;@ClassRulepublicstaticfinalSpringClassRuleSPRING_CLASS_RULE =newSpringClassRule();@RulepublicfinalSpringMethodRulespringMethodRule =newSpringMethodRule();@Test@Parameters({"2, 3, 6","3, 3, 9","0, 3, 0","3, 0, 0"    })publicvoidmultiply_values(intmultiplier,intvalue,intexpectedMultipliedValue)throwsException {MultiplyOperationResponseJsonmultiplyOperationResponseJson =restTemplate.postForObject("/multiply/",newMultiplyOperationRequestJson(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 implementSpringClassRule and SpringMethodRule by copying them from spring 4.2 to your project. You will also need to copyclass 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.

About

Example of JUnitParams integration with Spring

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp