Skip to content

Junit5 ‐ Repeated tests

Devrath edited this page Oct 23, 2023 · 2 revisions

github-header-image (1)

Code

@RepeatedTest(100)
fun `Add multiple products, total price sum is correct`(){
        val productOne = Product(id = 1, name = "Product-1", price = 1.0)
        val productTwo = Product(id = 2, name = "Product-2", price = 2.0)
        cart.addProduct(productOne,1)
        cart.addProduct(productTwo,1)

        assertThat(cart.getTotalCost()).isEqualTo(3.0)
}

Why this is useful

  • We can use @RepeatedTest(100) for example to run a test 100 times or any number of times needed.
  • This ensures that the test is successful all the number of times.
  • It is especially useful because it helps to identify the flaky tests.
  • Flaky tests are the tests that run sometimes and fail sometimes.

Output

Screenshot 2023-10-23 at 11 39 02β€―PM