|
| 1 | +package test.dataprovider.issue3237; |
| 2 | + |
| 3 | +import java.util.UUID; |
| 4 | +import java.util.concurrent.atomic.AtomicInteger; |
| 5 | +import org.testng.Assert; |
| 6 | +import org.testng.IRetryAnalyzer; |
| 7 | +import org.testng.ITestResult; |
| 8 | +import org.testng.annotations.DataProvider; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +public class SampleCacheTestCase { |
| 12 | + |
| 13 | + private final AtomicInteger invocationCount = new AtomicInteger(0); |
| 14 | + private String currentUuid; |
| 15 | + |
| 16 | + @Test(dataProvider = "dp", retryAnalyzer = MyRetry.class) |
| 17 | + public void testMethod(String uuid) { |
| 18 | + // Verify that fresh UUID is generated on each retry (no caching) |
| 19 | + Assert.assertEquals(uuid, currentUuid, "Received stale data from DataProvider!"); |
| 20 | + |
| 21 | + // Fail on first invocation to trigger retry, pass on second |
| 22 | + if (invocationCount.getAndIncrement() < 1) { |
| 23 | + throw new RuntimeException("Failed for " + uuid); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @DataProvider(name = "dp", cacheDataForTestRetries = false) |
| 28 | + public Object[][] getData() { |
| 29 | + currentUuid = UUID.randomUUID().toString(); |
| 30 | + return new Object[][] {{currentUuid}}; |
| 31 | + } |
| 32 | + |
| 33 | + public static class MyRetry implements IRetryAnalyzer { |
| 34 | + private int count = 0; |
| 35 | + |
| 36 | + @Override |
| 37 | + public boolean retry(ITestResult result) { |
| 38 | + return count++ < 1; |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments