Merge pull request #7951 from dkapil/task/BAEL-18132

BAEL-18132 Fix the integrations tests in spring-boot
This commit is contained in:
Josh Cummings 2019-10-05 16:31:55 -06:00 committed by GitHub
commit b0df7d1d5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 12 deletions

View File

@ -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<String> response = restTemplate.getForEntity("http://localhost:8080/contacts", String.class);
ResponseEntity<String> 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<String> response = restTemplate.getForEntity("http://localhost:8080/contacts/javaUtilDate", String.class);
ResponseEntity<String> 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<String> response = restTemplate.getForEntity("http://localhost:8080/contacts/plainWithJavaUtilDate", String.class);
ResponseEntity<String> 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<String> response = restTemplate.getForEntity("http://localhost:8080/contacts/plain", String.class);
ResponseEntity<String> response = restTemplate.getForEntity(url + "/contacts/plain", String.class);
assertEquals(200, response.getStatusCodeValue());

View File

@ -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<String> response = restTemplate.getForEntity("http://localhost:8080/contacts/plainWithJavaUtilDate", String.class);
ResponseEntity<String> 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<String> response = restTemplate.getForEntity("http://localhost:8080/contacts/plain", String.class);
ResponseEntity<String> response = restTemplate.getForEntity(url + "/contacts/plain", String.class);
assertEquals(200, response.getStatusCodeValue());