-3

Consider this test

@Test
    public void testGetClientIdTwiceReturnsDifferentClientIdsForSameUser() {
        final UniqueIdGenerator uniqueIdGenerator = new UniqueIdGenerator("testClientId");
        final String email = "uniqueEmail@gmail.com";
        final String userExternalId = UUID.randomUUID().toString();

        assertNotEquals(uniqueIdGenerator.getClientId(email, userExternalId), uniqueIdGenerator.getClientId(email, userExternalId));
    }

I want to run it 100 times with same data, just make sure that every time the ids generated are different.

I saw in Spring there is @Repeat annotation, do we have something similar for JUnit?

daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • Wrap the 4 lines of code in a for loop? – Dan Dosch Aug 09 '14 at 00:07
  • No, we do not have @Repeat in JUnit, you can create your own Runner and override getFrameworkMethods to add a method N times. So the value of N can be determined by a custom annotation on each test. I will post my custom runner once I reach home. – Maas Aug 09 '14 at 00:14
  • Another way is to use Parametrized runner available in JUnit and create a field of reference as an array of N elements. – Maas Aug 09 '14 at 00:17

1 Answers1

3

You want to run it 100 times, but you don't want to run each iteration independently, because you want to compare the outputs of each iteration with each other ( to ensure they're all unique), so parameterization isn't the best way.

I would just loop 100 times:

Set<String> set = new HashSet<Set>();
for (int i = 0; i < 100; i++) { 
    assertTrue(set.add(uniqueIdGenerator.getClientId(email, userExternalId)));

FYI, set.add() returns false if the element being added is already in the set.

Bohemian
  • 412,405
  • 93
  • 575
  • 722