Fix for pact test and use Junit5 insead of Hamcrest library
This commit is contained in:
parent
c2e38da0fb
commit
f89cbffe9d
|
@ -1,8 +1,5 @@
|
||||||
package com.baeldung.client;
|
package com.baeldung.client;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
|
|
||||||
import com.baeldung.resttemplate.web.dto.Foo;
|
import com.baeldung.resttemplate.web.dto.Foo;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -37,7 +34,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() {
|
public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -46,7 +43,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
restTemplateBuilder.configure(restTemplate);
|
restTemplateBuilder.configure(restTemplate);
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
||||||
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -55,7 +52,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
restTemplateBuilder.build();
|
restTemplateBuilder.build();
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
||||||
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -65,7 +62,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder, "user", "passwd");
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder, "user", "passwd");
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
||||||
String.class);
|
String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -73,7 +70,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
|
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
||||||
String.class);
|
String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,7 +78,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
ResponseEntity<String> response = testRestTemplate.withBasicAuth("user", "passwd").
|
ResponseEntity<String> response = testRestTemplate.withBasicAuth("user", "passwd").
|
||||||
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
|
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -90,7 +87,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
HttpClientOption.ENABLE_COOKIES);
|
HttpClientOption.ENABLE_COOKIES);
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
||||||
String.class);
|
String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
// HEAD
|
// HEAD
|
||||||
|
|
|
@ -22,7 +22,7 @@ import au.com.dius.pact.consumer.junit5.PactTestFor;
|
||||||
import au.com.dius.pact.model.RequestResponsePact;
|
import au.com.dius.pact.model.RequestResponsePact;
|
||||||
|
|
||||||
@ExtendWith(PactConsumerTestExt.class)
|
@ExtendWith(PactConsumerTestExt.class)
|
||||||
@PactTestFor(providerName = "test_provider", hostInterface="localhost", port = "8080")
|
@PactTestFor(providerName = "test_provider", hostInterface="localhost")
|
||||||
public class PactConsumerDrivenContractUnitTest {
|
public class PactConsumerDrivenContractUnitTest {
|
||||||
|
|
||||||
@Pact(provider="test_provider", consumer = "test_consumer")
|
@Pact(provider="test_provider", consumer = "test_consumer")
|
||||||
|
|
|
@ -17,7 +17,7 @@ import au.com.dius.pact.provider.junit5.PactVerificationContext;
|
||||||
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
|
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
|
||||||
|
|
||||||
@Provider("test_provider")
|
@Provider("test_provider")
|
||||||
@PactFolder("pacts")
|
@PactFolder("target/pacts")
|
||||||
public class PactProviderLiveTest {
|
public class PactProviderLiveTest {
|
||||||
|
|
||||||
private static ConfigurableWebApplicationContext application;
|
private static ConfigurableWebApplicationContext application;
|
||||||
|
|
|
@ -2,10 +2,6 @@ package com.baeldung.resttemplate;
|
||||||
|
|
||||||
import static org.apache.commons.codec.binary.Base64.encodeBase64;
|
import static org.apache.commons.codec.binary.Base64.encodeBase64;
|
||||||
import static com.baeldung.client.Consts.APPLICATION_PORT;
|
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 static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -58,7 +54,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException {
|
public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException {
|
||||||
final ResponseEntity<Foo> response = restTemplate.getForEntity(fooResourceUrl + "/1", Foo.class);
|
final ResponseEntity<Foo> response = restTemplate.getForEntity(fooResourceUrl + "/1", Foo.class);
|
||||||
|
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
Assertions.assertEquals(response.getStatusCode(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -69,15 +65,15 @@ public class RestTemplateBasicLiveTest {
|
||||||
final ObjectMapper mapper = new XmlMapper();
|
final ObjectMapper mapper = new XmlMapper();
|
||||||
final JsonNode root = mapper.readTree(response.getBody());
|
final JsonNode root = mapper.readTree(response.getBody());
|
||||||
final JsonNode name = root.path("name");
|
final JsonNode name = root.path("name");
|
||||||
assertThat(name.asText(), notNullValue());
|
Assertions.assertNotNull(name.asText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException {
|
public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException {
|
||||||
final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
|
final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
|
||||||
|
|
||||||
assertThat(foo.getName(), notNullValue());
|
Assertions.assertNotNull(foo.getName());
|
||||||
assertThat(foo.getId(), is(1L));
|
Assertions.assertEquals(foo.getId(), 1L);
|
||||||
}
|
}
|
||||||
|
|
||||||
// HEAD, OPTIONS
|
// HEAD, OPTIONS
|
||||||
|
@ -147,7 +143,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
// Check that Resource was updated
|
// Check that Resource was updated
|
||||||
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||||
final Foo foo = updateResponse.getBody();
|
final Foo foo = updateResponse.getBody();
|
||||||
assertThat(foo.getName(), is(updatedInstance.getName()));
|
Assertions.assertEquals(foo.getName(), updatedInstance.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -157,7 +153,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
|
|
||||||
// Create entity
|
// Create entity
|
||||||
ResponseEntity<Foo> response = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
|
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
|
// Update entity
|
||||||
final Foo updatedInstance = new Foo("newName");
|
final Foo updatedInstance = new Foo("newName");
|
||||||
|
@ -170,7 +166,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
// Check that entity was updated
|
// Check that entity was updated
|
||||||
response = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
response = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||||
final Foo foo = response.getBody();
|
final Foo foo = response.getBody();
|
||||||
assertThat(foo.getName(), is(updatedInstance.getName()));
|
Assertions.assertEquals(foo.getName(), updatedInstance.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// PATCH
|
// PATCH
|
||||||
|
@ -198,7 +194,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
// Check that Resource was updated
|
// Check that Resource was updated
|
||||||
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||||
final Foo foo = updateResponse.getBody();
|
final Foo foo = updateResponse.getBody();
|
||||||
assertThat(foo.getName(), is(updatedResource.getName()));
|
Assertions.assertEquals(foo.getName(), updatedResource.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE
|
// DELETE
|
||||||
|
@ -207,7 +203,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
|
public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
|
||||||
final Foo foo = new Foo("remove me");
|
final Foo foo = new Foo("remove me");
|
||||||
final ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class);
|
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()
|
final String entityUrl = fooResourceUrl + "/" + response.getBody()
|
||||||
.getId();
|
.getId();
|
||||||
|
@ -216,7 +212,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
restTemplate.getForEntity(entityUrl, Foo.class);
|
restTemplate.getForEntity(entityUrl, Foo.class);
|
||||||
fail();
|
fail();
|
||||||
} catch (final HttpClientErrorException ex) {
|
} 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);
|
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();
|
final String fooResponse = response.getBody();
|
||||||
assertThat(fooResponse, notNullValue());
|
Assertions.assertNotNull(fooResponse);
|
||||||
assertThat(fooResponse, is("10"));
|
Assertions.assertEquals(fooResponse, "10");
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpHeaders prepareBasicAuthHeaders() {
|
private HttpHeaders prepareBasicAuthHeaders() {
|
||||||
|
|
Loading…
Reference in New Issue