Skip to content

Commit

Permalink
BAEL-4656: Get available port number for the test (eugenp#10096)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwoyke authored Sep 28, 2020
1 parent 53f02c4 commit 70ed204
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,38 @@
import au.com.dius.pact.model.RequestResponsePact;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

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

public class PactConsumerDrivenContractUnitTest {

private static int getAvailablePort() {
return new Random()
.ints(6000, 9000)
.filter(PactConsumerDrivenContractUnitTest::isFree)
.findFirst()
.orElse(8080);
}

private static boolean isFree(int port) {
try {
new ServerSocket(port).close();
return true;
} catch (IOException e) {
return false;
}
}

@Rule
public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("test_provider", "localhost", getAvailablePort(), this);

@Pact(consumer = "test_consumer")
public RequestResponsePact createPact(PactDslWithProvider builder) {
Expand Down

0 comments on commit 70ed204

Please sign in to comment.