It can be done with times method:
@Test
void "should execute closure twice"() {
int i = 0
2.times {
i++
}
assertThat(i).isEqualTo(2)
}
Another method is upto which executes given method from one number to another in inclusive way:
@Test
void "should execute closure twice with upto"() {
int i = 0
0.upto(1, {
i++
})
assertThat(i).isEqualTo(2)
}
Under the hood both are implemented as simple for loops. However, in writing 2.times or 0.upto is more friendly way to do that.