From 3103d0f7a982adfcbb9809820de03072d5da8c6f Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Tue, 29 Mar 2022 21:04:18 +0530 Subject: [PATCH] Fix for pact test and use Junit5 insead of Hamcrest library --- .../client/TestRestTemplateBasicLiveTest.java | 17 +++++------ .../PactConsumerDrivenContractUnitTest.java | 2 +- .../baeldung/pact/PactProviderLiveTest.java | 2 +- .../RestTemplateBasicLiveTest.java | 30 ++++++++----------- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/client/TestRestTemplateBasicLiveTest.java b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/client/TestRestTemplateBasicLiveTest.java index 86f188d27a..65c15d882b 100644 --- a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/client/TestRestTemplateBasicLiveTest.java +++ b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/client/TestRestTemplateBasicLiveTest.java @@ -1,8 +1,5 @@ package com.baeldung.client; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - import com.baeldung.resttemplate.web.dto.Foo; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -37,7 +34,7 @@ public class TestRestTemplateBasicLiveTest { public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -46,7 +43,7 @@ public class TestRestTemplateBasicLiveTest { restTemplateBuilder.configure(restTemplate); TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder); ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -55,7 +52,7 @@ public class TestRestTemplateBasicLiveTest { restTemplateBuilder.build(); TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder); ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -65,7 +62,7 @@ public class TestRestTemplateBasicLiveTest { TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder, "user", "passwd"); ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -73,7 +70,7 @@ public class TestRestTemplateBasicLiveTest { TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -81,7 +78,7 @@ public class TestRestTemplateBasicLiveTest { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity response = testRestTemplate.withBasicAuth("user", "passwd"). getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -90,7 +87,7 @@ public class TestRestTemplateBasicLiveTest { HttpClientOption.ENABLE_COOKIES); ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } // HEAD diff --git a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java index bda7d09c49..6e8703be0d 100644 --- a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java +++ b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java @@ -22,7 +22,7 @@ import au.com.dius.pact.consumer.junit5.PactTestFor; import au.com.dius.pact.model.RequestResponsePact; @ExtendWith(PactConsumerTestExt.class) -@PactTestFor(providerName = "test_provider", hostInterface="localhost", port = "8080") +@PactTestFor(providerName = "test_provider", hostInterface="localhost") public class PactConsumerDrivenContractUnitTest { @Pact(provider="test_provider", consumer = "test_consumer") diff --git a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactProviderLiveTest.java b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactProviderLiveTest.java index 044bdbd7fd..c59a4ea59f 100644 --- a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactProviderLiveTest.java +++ b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/pact/PactProviderLiveTest.java @@ -17,7 +17,7 @@ import au.com.dius.pact.provider.junit5.PactVerificationContext; import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider; @Provider("test_provider") -@PactFolder("pacts") +@PactFolder("target/pacts") public class PactProviderLiveTest { private static ConfigurableWebApplicationContext application; diff --git a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java index 74f7c29d92..a433ac73c3 100644 --- a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java +++ b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java @@ -2,10 +2,6 @@ package com.baeldung.resttemplate; import static org.apache.commons.codec.binary.Base64.encodeBase64; import static com.baeldung.client.Consts.APPLICATION_PORT; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; @@ -58,7 +54,7 @@ public class RestTemplateBasicLiveTest { public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException { final ResponseEntity response = restTemplate.getForEntity(fooResourceUrl + "/1", Foo.class); - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK); } @Test @@ -69,15 +65,15 @@ public class RestTemplateBasicLiveTest { final ObjectMapper mapper = new XmlMapper(); final JsonNode root = mapper.readTree(response.getBody()); final JsonNode name = root.path("name"); - assertThat(name.asText(), notNullValue()); + Assertions.assertNotNull(name.asText()); } @Test public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException { final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class); - assertThat(foo.getName(), notNullValue()); - assertThat(foo.getId(), is(1L)); + Assertions.assertNotNull(foo.getName()); + Assertions.assertEquals(foo.getId(), 1L); } // HEAD, OPTIONS @@ -147,7 +143,7 @@ public class RestTemplateBasicLiveTest { // Check that Resource was updated final ResponseEntity updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); final Foo foo = updateResponse.getBody(); - assertThat(foo.getName(), is(updatedInstance.getName())); + Assertions.assertEquals(foo.getName(), updatedInstance.getName()); } @Test @@ -157,7 +153,7 @@ public class RestTemplateBasicLiveTest { // Create entity ResponseEntity response = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); - assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED); // Update entity final Foo updatedInstance = new Foo("newName"); @@ -170,7 +166,7 @@ public class RestTemplateBasicLiveTest { // Check that entity was updated response = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); final Foo foo = response.getBody(); - assertThat(foo.getName(), is(updatedInstance.getName())); + Assertions.assertEquals(foo.getName(), updatedInstance.getName()); } // PATCH @@ -198,7 +194,7 @@ public class RestTemplateBasicLiveTest { // Check that Resource was updated final ResponseEntity updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); final Foo foo = updateResponse.getBody(); - assertThat(foo.getName(), is(updatedResource.getName())); + Assertions.assertEquals(foo.getName(), updatedResource.getName()); } // DELETE @@ -207,7 +203,7 @@ public class RestTemplateBasicLiveTest { public void givenFooService_whenCallDelete_thenEntityIsRemoved() { final Foo foo = new Foo("remove me"); final ResponseEntity response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class); - assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED); final String entityUrl = fooResourceUrl + "/" + response.getBody() .getId(); @@ -216,7 +212,7 @@ public class RestTemplateBasicLiveTest { restTemplate.getForEntity(entityUrl, Foo.class); fail(); } catch (final HttpClientErrorException ex) { - assertThat(ex.getStatusCode(), is(HttpStatus.INTERNAL_SERVER_ERROR)); + Assertions.assertEquals(ex.getStatusCode(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -232,10 +228,10 @@ public class RestTemplateBasicLiveTest { ResponseEntity response = restTemplate.postForEntity( fooResourceUrl+"/form", request , String.class); - assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); + Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED); final String fooResponse = response.getBody(); - assertThat(fooResponse, notNullValue()); - assertThat(fooResponse, is("10")); + Assertions.assertNotNull(fooResponse); + Assertions.assertEquals(fooResponse, "10"); } private HttpHeaders prepareBasicAuthHeaders() {