- Notifications
You must be signed in to change notification settings - Fork6
Pragmatists/junitparams-spring-integration-example
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
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(); }}
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); }}
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.