package io.dropwizard.testing.junit5;

import org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.concurrent.ConcurrentSkipListSet;

import static org.assertj.core.api.Assertions.assertThat;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(DropwizardExtensionsSupport.class)
class ResourceExtensionRandomPortsTest {
    private final ResourceExtension resources = ResourceExtension.builder()
            .setTestContainerFactory(new GrizzlyTestContainerFactory())
            .build();

    ConcurrentSkipListSet<Integer> usedPorts = new ConcurrentSkipListSet<>();

    @RepeatedTest(10)
    public void eachTestShouldUseANewPort() {
        final int port = resources.target("/").getUri().getPort();
        assertThat(usedPorts).doesNotContain(port);

        usedPorts.add(port);
    }
}

@@ -135,375 +135,245 @@
 api.
-Repeated
 Test;%0A
+%0A
 import 
-org.junit.jupiter.api.TestInstance;%0Aimport org.junit.jupiter.api.extension.ExtendWith;%0A%0Aimport java.util.concurrent.ConcurrentSkipListSet;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;%0A%0A@TestInstance(TestInstance.Lifecycle.PER_CLASS)%0A@ExtendWith(DropwizardExtensionsSupport.class)%0Aclass ResourceExtensionRandomPortsTest %7B%0A    private
+java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;%0A%0Aclass ResourceExtensionRandomPortsTest %7B%0A%0A    @Test%0A    public void eachTestShouldUseANewPort() throws Throwable %7B%0A       
  fin
@@ -445,16 +445,20 @@
         
+    
 .setTest
@@ -521,16 +521,20 @@
         
+    
 .build()
@@ -539,31 +539,16 @@
 ();%0A
-%0A
     
-ConcurrentSkipList
+    
 Set%3C
@@ -576,104 +576,97 @@
 new 
-ConcurrentSkipListSet%3C%3E();%0A%0A    @RepeatedTest(10)%0A    public void eachTestShouldUseANewPort() %7B%0A
+HashSet%3C%3E();%0A%0A        for (int i = 0; i %3C 10; i++) %7B%0A            resources.before();%0A    
     
@@ -740,79 +740,102 @@
     
-assertThat(usedPorts).doesNotContain(port);%0A%0A        usedPorts.add(port
+    usedPorts.add(port);%0A        %7D%0A        assertThat(usedPorts).hasSizeGreaterThanOrEqualTo(2
 );%0A 

Make `ResourceExtensionRandomPortsTest` less flaky (#3897)