11package com .baeldung .rest .cucumber ;
22
3- import com .github .tomakehurst .wiremock .WireMockServer ;
4- import cucumber .api .java .en .Then ;
5- import cucumber .api .java .en .When ;
6- import org .apache .http .HttpResponse ;
7- import org .apache .http .client .methods .HttpGet ;
8- import org .apache .http .client .methods .HttpPost ;
9- import org .apache .http .entity .StringEntity ;
10- import org .apache .http .impl .client .CloseableHttpClient ;
11- import org .apache .http .impl .client .HttpClients ;
12-
13- import java .io .IOException ;
14- import java .io .InputStream ;
15- import java .util .Scanner ;
16-
173import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
184import static com .github .tomakehurst .wiremock .client .WireMock .configureFor ;
195import static com .github .tomakehurst .wiremock .client .WireMock .containing ;
2511import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
2612import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
2713import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
14+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
2815import static org .hamcrest .CoreMatchers .containsString ;
2916import static org .junit .Assert .assertEquals ;
3017import static org .junit .Assert .assertThat ;
3118
19+ import java .io .IOException ;
20+ import java .io .InputStream ;
21+ import java .util .Scanner ;
22+
23+ import org .apache .http .HttpResponse ;
24+ import org .apache .http .client .methods .HttpGet ;
25+ import org .apache .http .client .methods .HttpPost ;
26+ import org .apache .http .entity .StringEntity ;
27+ import org .apache .http .impl .client .CloseableHttpClient ;
28+ import org .apache .http .impl .client .HttpClients ;
29+
30+ import com .github .tomakehurst .wiremock .WireMockServer ;
31+
32+ import cucumber .api .java .en .Then ;
33+ import cucumber .api .java .en .When ;
34+
3235public class StepDefinition {
3336
3437 private static final String CREATE_PATH = "/create" ;
@@ -37,20 +40,20 @@ public class StepDefinition {
3740 private final InputStream jsonInputStream = this .getClass ().getClassLoader ().getResourceAsStream ("cucumber.json" );
3841 private final String jsonString = new Scanner (jsonInputStream , "UTF-8" ).useDelimiter ("\\ Z" ).next ();
3942
40- private final WireMockServer wireMockServer = new WireMockServer ();
43+ private final WireMockServer wireMockServer = new WireMockServer (options (). dynamicPort () );
4144 private final CloseableHttpClient httpClient = HttpClients .createDefault ();
4245
4346 @ When ("^users upload data on a project$" )
4447 public void usersUploadDataOnAProject () throws IOException {
4548 wireMockServer .start ();
4649
47- configureFor ("localhost" , 8080 );
50+ configureFor ("localhost" , wireMockServer . port () );
4851 stubFor (post (urlEqualTo (CREATE_PATH ))
4952 .withHeader ("content-type" , equalTo (APPLICATION_JSON ))
5053 .withRequestBody (containing ("testing-framework" ))
5154 .willReturn (aResponse ().withStatus (200 )));
5255
53- HttpPost request = new HttpPost ("http://localhost:8080 /create" );
56+ HttpPost request = new HttpPost ("http://localhost:" + wireMockServer . port () + " /create" );
5457 StringEntity entity = new StringEntity (jsonString );
5558 request .addHeader ("content-type" , APPLICATION_JSON );
5659 request .setEntity (entity );
@@ -67,11 +70,11 @@ public void usersUploadDataOnAProject() throws IOException {
6770 public void usersGetInformationOnAProject (String projectName ) throws IOException {
6871 wireMockServer .start ();
6972
70- configureFor ("localhost" , 8080 );
73+ configureFor ("localhost" , wireMockServer . port () );
7174 stubFor (get (urlEqualTo ("/projects/cucumber" )).withHeader ("accept" , equalTo (APPLICATION_JSON ))
7275 .willReturn (aResponse ().withBody (jsonString )));
7376
74- HttpGet request = new HttpGet ("http://localhost:8080 /projects/" + projectName .toLowerCase ());
77+ HttpGet request = new HttpGet ("http://localhost:" + wireMockServer . port () + " /projects/" + projectName .toLowerCase ());
7578 request .addHeader ("accept" , APPLICATION_JSON );
7679 HttpResponse httpResponse = httpClient .execute (request );
7780 String responseString = convertResponseToString (httpResponse );
0 commit comments