Fix for pact test and use Junit5 insead of Hamcrest library

This commit is contained in:
anuragkumawat 2022-03-29 21:04:18 +05:30
parent c2e38da0fb
commit f89cbffe9d
4 changed files with 22 additions and 29 deletions

View File

@ -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<Foo> 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<Foo> 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<Foo> 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<String> 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<String> 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<String> 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<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
}
// HEAD

View File

@ -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")

View File

@ -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;

View File

@ -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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<Foo> 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<String> 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() {