diff --git a/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppUnitTest.java similarity index 87% rename from spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppIntegrationTest.java rename to spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppUnitTest.java index 86af985d8a..7be85b5e4f 100644 --- a/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppUnitTest.java @@ -2,11 +2,14 @@ package com.baeldung.jsondateformat; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.ResponseEntity; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -22,23 +25,33 @@ import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = DEFINED_PORT, classes = ContactApp.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContactApp.class) @TestPropertySource(properties = { "spring.jackson.date-format=yyyy-MM-dd HH:mm:ss" }) -public class ContactAppIntegrationTest { +public class ContactAppUnitTest { private final ObjectMapper mapper = new ObjectMapper(); @Autowired TestRestTemplate restTemplate; + + @LocalServerPort + int port; + + String url; + @Before + public void before() { + url=String.format("http://localhost:%s", port); + } + @Test public void givenJsonFormatAnnotationAndJava8DateType_whenGet_thenReturnExpectedDateFormat() throws IOException, ParseException { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/contacts", String.class); + ResponseEntity response = restTemplate.getForEntity(url + "/contacts", String.class); assertEquals(200, response.getStatusCodeValue()); @@ -53,7 +66,7 @@ public class ContactAppIntegrationTest { @Test public void givenJsonFormatAnnotationAndLegacyDateType_whenGet_thenReturnExpectedDateFormat() throws IOException { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/contacts/javaUtilDate", String.class); + ResponseEntity response = restTemplate.getForEntity(url + "/contacts/javaUtilDate", String.class); assertEquals(200, response.getStatusCodeValue()); @@ -68,7 +81,7 @@ public class ContactAppIntegrationTest { @Test public void givenDefaultDateFormatInAppPropertiesAndLegacyDateType_whenGet_thenReturnExpectedDateFormat() throws IOException { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/contacts/plainWithJavaUtilDate", String.class); + ResponseEntity response = restTemplate.getForEntity(url + "/contacts/plainWithJavaUtilDate", String.class); assertEquals(200, response.getStatusCodeValue()); @@ -83,7 +96,7 @@ public class ContactAppIntegrationTest { @Test(expected = DateTimeParseException.class) public void givenDefaultDateFormatInAppPropertiesAndJava8DateType_whenGet_thenNotApplyFormat() throws IOException { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/contacts/plain", String.class); + ResponseEntity response = restTemplate.getForEntity(url + "/contacts/plain", String.class); assertEquals(200, response.getStatusCodeValue()); diff --git a/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppWithObjectMapperCustomizerIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppWithObjectMapperCustomizerUnitTest.java similarity index 82% rename from spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppWithObjectMapperCustomizerIntegrationTest.java rename to spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppWithObjectMapperCustomizerUnitTest.java index 554283d758..114f9bfc1a 100644 --- a/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppWithObjectMapperCustomizerIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/jsondateformat/ContactAppWithObjectMapperCustomizerUnitTest.java @@ -2,11 +2,14 @@ package com.baeldung.jsondateformat; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; @@ -19,20 +22,30 @@ import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = DEFINED_PORT, classes = ContactApp.class) -public class ContactAppWithObjectMapperCustomizerIntegrationTest { +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContactApp.class) +public class ContactAppWithObjectMapperCustomizerUnitTest { private final ObjectMapper mapper = new ObjectMapper(); @Autowired TestRestTemplate restTemplate; + @LocalServerPort + int port; + + String url; + + @Before + public void before() { + url=String.format("http://localhost:%s", port); + } + @Test public void givenDefaultDateFormatInAppPropertiesAndLegacyDateType_whenGet_thenReturnExpectedDateFormat() throws IOException { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/contacts/plainWithJavaUtilDate", String.class); + ResponseEntity response = restTemplate.getForEntity(url + "/contacts/plainWithJavaUtilDate", String.class); assertEquals(200, response.getStatusCodeValue()); @@ -47,7 +60,7 @@ public class ContactAppWithObjectMapperCustomizerIntegrationTest { @Test public void givenDefaultDateFormatInAppPropertiesAndJava8DateType_whenGet_thenReturnExpectedDateFormat() throws IOException { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/contacts/plain", String.class); + ResponseEntity response = restTemplate.getForEntity(url + "/contacts/plain", String.class); assertEquals(200, response.getStatusCodeValue());