From 947e04af9dd4891b388106833dbf698cbca0c850 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Sat, 2 Nov 2019 18:47:30 +0330 Subject: [PATCH] Fixed the Port Already in Use Issue --- .../protobuf/ApplicationIntegrationTest.java | 31 +++++++++++-------- .../SpringContextIntegrationTest.java | 5 ++- .../java/org/baeldung/SpringContextTest.java | 5 ++- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationIntegrationTest.java b/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationIntegrationTest.java index f0e2473ec4..df750db1db 100644 --- a/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationIntegrationTest.java +++ b/spring-protobuf/src/test/java/com/baeldung/protobuf/ApplicationIntegrationTest.java @@ -1,11 +1,7 @@ package com.baeldung.protobuf; -import static org.hamcrest.CoreMatchers.containsString; -import static org.junit.Assert.assertThat; - -import java.io.IOException; -import java.io.InputStream; - +import com.baeldung.protobuf.BaeldungTraining.Course; +import com.googlecode.protobuf.format.JsonFormat; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -15,33 +11,38 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.client.RestTemplate; -import com.baeldung.protobuf.BaeldungTraining.Course; -import com.googlecode.protobuf.format.JsonFormat; +import java.io.IOException; +import java.io.InputStream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; @DirtiesContext @RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT) +@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) public class ApplicationIntegrationTest { - private static final String COURSE1_URL = "http://localhost:8080/courses/1"; - @Autowired private RestTemplate restTemplate; + @LocalServerPort + private int port; + @Test public void whenUsingRestTemplate_thenSucceed() { - ResponseEntity course = restTemplate.getForEntity(COURSE1_URL, Course.class); + ResponseEntity course = restTemplate.getForEntity(getUrl(), Course.class); assertResponse(course.toString()); } @Test public void whenUsingHttpClient_thenSucceed() throws IOException { - InputStream responseStream = executeHttpRequest(COURSE1_URL); + InputStream responseStream = executeHttpRequest(getUrl()); String jsonOutput = convertProtobufMessageStreamToJsonString(responseStream); assertResponse(jsonOutput); } @@ -74,4 +75,8 @@ public class ApplicationIntegrationTest { assertThat(response, containsString("number")); assertThat(response, containsString("type")); } + + private String getUrl() { + return "http://localhost:" + port + "/courses/1"; + } } \ No newline at end of file diff --git a/spring-protobuf/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-protobuf/src/test/java/org/baeldung/SpringContextIntegrationTest.java index 6d021eb400..8315d0ff6c 100644 --- a/spring-protobuf/src/test/java/org/baeldung/SpringContextIntegrationTest.java +++ b/spring-protobuf/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -1,16 +1,15 @@ package org.baeldung; +import com.baeldung.protobuf.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.protobuf.Application; - @DirtiesContext @RunWith(SpringRunner.class) -@SpringBootTest(classes = Application.class) +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class SpringContextIntegrationTest { @Test diff --git a/spring-protobuf/src/test/java/org/baeldung/SpringContextTest.java b/spring-protobuf/src/test/java/org/baeldung/SpringContextTest.java index 4f655277b7..62a708b60a 100644 --- a/spring-protobuf/src/test/java/org/baeldung/SpringContextTest.java +++ b/spring-protobuf/src/test/java/org/baeldung/SpringContextTest.java @@ -1,16 +1,15 @@ package org.baeldung; +import com.baeldung.protobuf.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.protobuf.Application; - @DirtiesContext @RunWith(SpringRunner.class) -@SpringBootTest(classes = Application.class) +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class SpringContextTest { @Test