Software Development

JUnit 4 – Run Test Method More than Once

In JUnit 5, they came up with an annotation that does just that – run a test method n times. Introducing @RepeatedTest(int value). You may read more about it in JUnit 5 User Guide.

[wp_ad_camp_5]

However, we will demonstrates how to perform repeated tests in JUnit 4 for this post. It involves creating a class that implements the TestRule interface, another class that implements Statement interface and a new annotation.

The Annotation class

This annotation will be used along side the @Test annotation.

RepeatableTestStatement class

[wp_ad_camp_4]

RepeatedTestRule class

Put it all together

[wp_ad_camp_3]

This outputs:

[wp_ad_camp_2]

Download the codes

https://github.com/Turreta/JUnit-4-Run-Test-Method-More-than-Once

References

[wp_ad_camp_1]

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like