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.

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

RepeatedTestRule class

Put it all together

This outputs:

Download the codes




Karl San Gabriel

Professional Software Developer