Use URI#create instead of URI constructor where feasible in spring-test

This commit is contained in:
Sam Brannen 2022-12-09 12:25:47 -05:00
parent 4caf3c8ce0
commit a1a140f7d5
12 changed files with 78 additions and 115 deletions

View File

@ -18,7 +18,6 @@ package org.springframework.mock.http.client;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpRequest;
@ -51,12 +50,7 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
*/ */
public MockClientHttpRequest() { public MockClientHttpRequest() {
this.httpMethod = HttpMethod.GET; this.httpMethod = HttpMethod.GET;
try { this.uri = URI.create("/");
this.uri = new URI("/");
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.test.web.client; package org.springframework.test.web.client;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -38,17 +37,16 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* Unit tests for {@link DefaultRequestExpectation}. * Unit tests for {@link DefaultRequestExpectation}.
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class DefaultRequestExpectationTests { class DefaultRequestExpectationTests {
@Test @Test
public void match() throws Exception { void match() throws Exception {
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo")); RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
expectation.match(createRequest()); expectation.match(createRequest());
} }
@Test @Test
public void matchWithFailedExpectation() { void matchWithFailedExpectation() {
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo")); RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
expectation.andExpect(method(POST)); expectation.andExpect(method(POST));
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
@ -57,7 +55,7 @@ public class DefaultRequestExpectationTests {
} }
@Test @Test
public void hasRemainingCount() { void hasRemainingCount() {
RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo")); RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo"));
expectation.andRespond(withSuccess()); expectation.andRespond(withSuccess());
@ -69,7 +67,7 @@ public class DefaultRequestExpectationTests {
} }
@Test @Test
public void isSatisfied() { void isSatisfied() {
RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo")); RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo"));
expectation.andRespond(withSuccess()); expectation.andRespond(withSuccess());
@ -82,12 +80,7 @@ public class DefaultRequestExpectationTests {
private ClientHttpRequest createRequest() { private ClientHttpRequest createRequest() {
try { return new MockClientHttpRequest(HttpMethod.GET, URI.create("/foo"));
return new MockClientHttpRequest(HttpMethod.GET, new URI("/foo"));
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
} }
} }

View File

@ -18,7 +18,6 @@ package org.springframework.test.web.client;
import java.net.SocketException; import java.net.SocketException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -199,12 +198,7 @@ class SimpleRequestExpectationManagerTests {
private ClientHttpRequest createRequest(HttpMethod method, String url) { private ClientHttpRequest createRequest(HttpMethod method, String url) {
try { return new MockClientHttpRequest(method, URI.create(url));
return new MockClientHttpRequest(method, new URI(url));
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
} }
} }

View File

@ -17,7 +17,6 @@
package org.springframework.test.web.client; package org.springframework.test.web.client;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -121,12 +120,7 @@ class UnorderedRequestExpectationManagerTests {
private ClientHttpRequest createRequest(HttpMethod method, String url) { private ClientHttpRequest createRequest(HttpMethod method, String url) {
try { return new MockClientHttpRequest(method, URI.create(url));
return new MockClientHttpRequest(method, new URI(url));
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,28 +36,28 @@ import static org.hamcrest.Matchers.containsString;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen * @author Sam Brannen
*/ */
public class MockRestRequestMatchersTests { class MockRestRequestMatchersTests {
private final MockClientHttpRequest request = new MockClientHttpRequest(); private final MockClientHttpRequest request = new MockClientHttpRequest();
@Test @Test
public void requestTo() throws Exception { void requestTo() throws Exception {
this.request.setURI(new URI("http://www.foo.example/bar")); this.request.setURI(URI.create("http://www.foo.example/bar"));
MockRestRequestMatchers.requestTo("http://www.foo.example/bar").match(this.request); MockRestRequestMatchers.requestTo("http://www.foo.example/bar").match(this.request);
} }
@Test // SPR-15819 @Test // SPR-15819
public void requestToUriTemplate() throws Exception { void requestToUriTemplate() throws Exception {
this.request.setURI(new URI("http://www.foo.example/bar")); this.request.setURI(URI.create("http://www.foo.example/bar"));
MockRestRequestMatchers.requestToUriTemplate("http://www.foo.example/{bar}", "bar").match(this.request); MockRestRequestMatchers.requestToUriTemplate("http://www.foo.example/{bar}", "bar").match(this.request);
} }
@Test @Test
public void requestToNoMatch() throws Exception { void requestToNoMatch() {
this.request.setURI(new URI("http://www.foo.example/bar")); this.request.setURI(URI.create("http://www.foo.example/bar"));
assertThatThrownBy( assertThatThrownBy(
() -> MockRestRequestMatchers.requestTo("http://www.foo.example/wrong").match(this.request)) () -> MockRestRequestMatchers.requestTo("http://www.foo.example/wrong").match(this.request))
@ -65,21 +65,21 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void requestToContains() throws Exception { void requestToContains() throws Exception {
this.request.setURI(new URI("http://www.foo.example/bar")); this.request.setURI(URI.create("http://www.foo.example/bar"));
MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request); MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request);
} }
@Test @Test
public void method() throws Exception { void method() throws Exception {
this.request.setMethod(HttpMethod.GET); this.request.setMethod(HttpMethod.GET);
MockRestRequestMatchers.method(HttpMethod.GET).match(this.request); MockRestRequestMatchers.method(HttpMethod.GET).match(this.request);
} }
@Test @Test
public void methodNoMatch() throws Exception { void methodNoMatch() {
this.request.setMethod(HttpMethod.POST); this.request.setMethod(HttpMethod.POST);
assertThatThrownBy(() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request))
@ -88,14 +88,14 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void header() throws Exception { void header() throws Exception {
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
} }
@Test @Test
public void headerDoesNotExist() throws Exception { void headerDoesNotExist() throws Exception {
MockRestRequestMatchers.headerDoesNotExist(null).match(this.request); MockRestRequestMatchers.headerDoesNotExist(null).match(this.request);
MockRestRequestMatchers.headerDoesNotExist("").match(this.request); MockRestRequestMatchers.headerDoesNotExist("").match(this.request);
MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request); MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request);
@ -108,14 +108,14 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void headerMissing() throws Exception { void headerMissing() {
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))
.isInstanceOf(AssertionError.class) .isInstanceOf(AssertionError.class)
.hasMessageContaining("was null"); .hasMessageContaining("was null");
} }
@Test @Test
public void headerMissingValue() throws Exception { void headerMissingValue() {
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bad").match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bad").match(this.request))
@ -124,21 +124,21 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void headerContains() throws Exception { void headerContains() throws Exception {
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request); MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request);
} }
@Test @Test
public void headerContainsWithMissingHeader() throws Exception { void headerContainsWithMissingHeader() {
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request))
.isInstanceOf(AssertionError.class) .isInstanceOf(AssertionError.class)
.hasMessageContaining("but was null"); .hasMessageContaining("but was null");
} }
@Test @Test
public void headerContainsWithMissingValue() throws Exception { void headerContainsWithMissingValue() {
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request))
@ -147,21 +147,21 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void headers() throws Exception { void headers() throws Exception {
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request); MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
} }
@Test @Test
public void headersWithMissingHeader() throws Exception { void headersWithMissingHeader() {
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))
.isInstanceOf(AssertionError.class) .isInstanceOf(AssertionError.class)
.hasMessageContaining("but was null"); .hasMessageContaining("but was null");
} }
@Test @Test
public void headersWithMissingValue() throws Exception { void headersWithMissingValue() {
this.request.getHeaders().put("foo", Collections.singletonList("bar")); this.request.getHeaders().put("foo", Collections.singletonList("bar"));
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request))
@ -170,15 +170,15 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void queryParam() throws Exception { void queryParam() throws Exception {
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz")); this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request); MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request);
} }
@Test @Test
public void queryParamMissing() throws Exception { void queryParamMissing() {
this.request.setURI(new URI("http://www.foo.example/a")); this.request.setURI(URI.create("http://www.foo.example/a"));
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request))
.isInstanceOf(AssertionError.class) .isInstanceOf(AssertionError.class)
@ -186,8 +186,8 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void queryParamMissingValue() throws Exception { void queryParamMissingValue() {
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz")); this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request))
.isInstanceOf(AssertionError.class) .isInstanceOf(AssertionError.class)
@ -195,15 +195,15 @@ public class MockRestRequestMatchersTests {
} }
@Test @Test
public void queryParamContains() throws Exception { void queryParamContains() throws Exception {
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz")); this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request); MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request);
} }
@Test @Test
public void queryParamContainsWithMissingValue() throws Exception { void queryParamContainsWithMissingValue() {
this.request.setURI(new URI("http://www.foo.example/a?foo=bar&foo=baz")); this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request)) assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request))
.isInstanceOf(AssertionError.class) .isInstanceOf(AssertionError.class)

View File

@ -71,7 +71,7 @@ class ResponseCreatorsTests {
@Test @Test
void created() throws Exception { void created() throws Exception {
URI location = new URI("/foo"); URI location = URI.create("/foo");
DefaultResponseCreator responseCreator = MockRestResponseCreators.withCreatedEntity(location); DefaultResponseCreator responseCreator = MockRestResponseCreators.withCreatedEntity(location);
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,15 +17,12 @@
package org.springframework.test.web.client.samples.matchers; package org.springframework.test.web.client.samples.matchers;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.web.Person; import org.springframework.test.web.Person;
@ -42,31 +39,24 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class HeaderRequestMatchersIntegrationTests { class HeaderRequestMatchersIntegrationTests {
private static final String RESPONSE_BODY = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}"; private static final String RESPONSE_BODY = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
private final RestTemplate restTemplate = new RestTemplate();
private MockRestServiceServer mockServer; private final MockRestServiceServer mockServer = MockRestServiceServer.createServer(this.restTemplate);
private RestTemplate restTemplate;
@BeforeEach @BeforeEach
public void setup() { void setup() {
List<HttpMessageConverter<?>> converters = new ArrayList<>(); this.restTemplate.setMessageConverters(
converters.add(new StringHttpMessageConverter()); List.of(new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter()));
converters.add(new MappingJackson2HttpMessageConverter());
this.restTemplate = new RestTemplate();
this.restTemplate.setMessageConverters(converters);
this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
} }
@Test @Test
public void testString() throws Exception { void testString() {
this.mockServer.expect(requestTo("/person/1")) this.mockServer.expect(requestTo("/person/1"))
.andExpect(header("Accept", "application/json, application/*+json")) .andExpect(header("Accept", "application/json, application/*+json"))
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
@ -75,7 +65,7 @@ public class HeaderRequestMatchersIntegrationTests {
} }
@Test @Test
public void testStringContains() throws Exception { void testStringContains() {
this.mockServer.expect(requestTo("/person/1")) this.mockServer.expect(requestTo("/person/1"))
.andExpect(header("Accept", containsString("json"))) .andExpect(header("Accept", containsString("json")))
.andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON));
@ -83,8 +73,8 @@ public class HeaderRequestMatchersIntegrationTests {
executeAndVerify(); executeAndVerify();
} }
private void executeAndVerify() throws URISyntaxException { private void executeAndVerify() {
this.restTemplate.getForObject(new URI("/person/1"), Person.class); this.restTemplate.getForObject(URI.create("/person/1"), Person.class);
this.mockServer.verify(); this.mockServer.verify();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.test.web.client.samples.matchers; package org.springframework.test.web.client.samples.matchers;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -51,7 +50,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* @see org.springframework.test.web.client.match.JsonPathRequestMatchers * @see org.springframework.test.web.client.match.JsonPathRequestMatchers
* @see org.springframework.test.web.client.match.JsonPathRequestMatchersTests * @see org.springframework.test.web.client.match.JsonPathRequestMatchersTests
*/ */
public class JsonPathRequestMatchersIntegrationTests { class JsonPathRequestMatchersIntegrationTests {
private static final MultiValueMap<String, Person> people = new LinkedMultiValueMap<>(); private static final MultiValueMap<String, Person> people = new LinkedMultiValueMap<>();
@ -72,7 +71,7 @@ public class JsonPathRequestMatchersIntegrationTests {
@Test @Test
public void exists() throws Exception { void exists() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[0]").exists()) .andExpect(jsonPath("$.composers[0]").exists())
@ -85,7 +84,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void doesNotExist() throws Exception { void doesNotExist() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist()) .andExpect(jsonPath("$.composers[?(@.name == 'Edvard Grieeeeeeg')]").doesNotExist())
@ -97,7 +96,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void value() throws Exception { void value() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach")) .andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach"))
@ -108,7 +107,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void hamcrestMatchers() throws Exception { void hamcrestMatchers() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach"))) .andExpect(jsonPath("$.composers[0].name").value(equalTo("Johann Sebastian Bach")))
@ -124,7 +123,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void hamcrestMatchersWithParameterizedJsonPaths() throws Exception { void hamcrestMatchersWithParameterizedJsonPaths() {
String composerName = "$.composers[%s].name"; String composerName = "$.composers[%s].name";
String performerName = "$.performers[%s].name"; String performerName = "$.performers[%s].name";
@ -140,7 +139,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void isArray() throws Exception { void isArray() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers").isArray()) .andExpect(jsonPath("$.composers").isArray())
@ -150,7 +149,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void isString() throws Exception { void isString() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[0].name").isString()) .andExpect(jsonPath("$.composers[0].name").isString())
@ -160,7 +159,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void isNumber() throws Exception { void isNumber() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[0].someDouble").isNumber()) .andExpect(jsonPath("$.composers[0].someDouble").isNumber())
@ -170,7 +169,7 @@ public class JsonPathRequestMatchersIntegrationTests {
} }
@Test @Test
public void isBoolean() throws Exception { void isBoolean() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/json")) .andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.composers[0].someBoolean").isBoolean()) .andExpect(jsonPath("$.composers[0].someBoolean").isBoolean())
@ -179,8 +178,8 @@ public class JsonPathRequestMatchersIntegrationTests {
executeAndVerify(); executeAndVerify();
} }
private void executeAndVerify() throws URISyntaxException { private void executeAndVerify() {
this.restTemplate.put(new URI("/composers"), people); this.restTemplate.put(URI.create("/composers"), people);
this.mockServer.verify(); this.mockServer.verify();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.test.web.client.samples.matchers; package org.springframework.test.web.client.samples.matchers;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -48,7 +47,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* @see ContentRequestMatchersIntegrationTests * @see ContentRequestMatchersIntegrationTests
* @see XpathRequestMatchersIntegrationTests * @see XpathRequestMatchersIntegrationTests
*/ */
public class XmlContentRequestMatchersIntegrationTests { class XmlContentRequestMatchersIntegrationTests {
private static final String PEOPLE_XML = private static final String PEOPLE_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
@ -68,7 +67,7 @@ public class XmlContentRequestMatchersIntegrationTests {
@BeforeEach @BeforeEach
public void setup() { void setup() {
List<Person> composers = Arrays.asList( List<Person> composers = Arrays.asList(
new Person("Johann Sebastian Bach").setSomeDouble(21), new Person("Johann Sebastian Bach").setSomeDouble(21),
new Person("Johannes Brahms").setSomeDouble(.0025), new Person("Johannes Brahms").setSomeDouble(.0025),
@ -87,7 +86,7 @@ public class XmlContentRequestMatchersIntegrationTests {
} }
@Test @Test
public void testXmlEqualTo() throws Exception { void testXmlEqualTo() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/xml")) .andExpect(content().contentType("application/xml"))
.andExpect(content().xml(PEOPLE_XML)) .andExpect(content().xml(PEOPLE_XML))
@ -97,7 +96,7 @@ public class XmlContentRequestMatchersIntegrationTests {
} }
@Test @Test
public void testHamcrestNodeMatcher() throws Exception { void testHamcrestNodeMatcher() {
this.mockServer.expect(requestTo("/composers")) this.mockServer.expect(requestTo("/composers"))
.andExpect(content().contentType("application/xml")) .andExpect(content().contentType("application/xml"))
.andExpect(content().node(hasXPath("/people/composers/composer[1]"))) .andExpect(content().node(hasXPath("/people/composers/composer[1]")))
@ -106,8 +105,8 @@ public class XmlContentRequestMatchersIntegrationTests {
executeAndVerify(); executeAndVerify();
} }
private void executeAndVerify() throws URISyntaxException { private void executeAndVerify() {
this.restTemplate.put(new URI("/composers"), this.people); this.restTemplate.put(URI.create("/composers"), this.people);
this.mockServer.verify(); this.mockServer.verify();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -191,7 +191,7 @@ public class XpathRequestMatchersIntegrationTests {
} }
private void executeAndVerify() throws URISyntaxException { private void executeAndVerify() throws URISyntaxException {
this.restTemplate.put(new URI("/composers"), this.people); this.restTemplate.put(URI.create("/composers"), this.people);
this.mockServer.verify(); this.mockServer.verify();
} }

View File

@ -97,7 +97,7 @@ class MockHttpServletRequestBuilderTests {
@Test // SPR-13435 @Test // SPR-13435
void requestUriWithDoubleSlashes() throws URISyntaxException { void requestUriWithDoubleSlashes() throws URISyntaxException {
this.builder = new MockHttpServletRequestBuilder(GET, new URI("/test//currentlyValid/0")); this.builder = new MockHttpServletRequestBuilder(GET, URI.create("/test//currentlyValid/0"));
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
assertThat(request.getRequestURI()).isEqualTo("/test//currentlyValid/0"); assertThat(request.getRequestURI()).isEqualTo("/test//currentlyValid/0");

View File

@ -176,7 +176,7 @@ class PrintingResultHandlerTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set("header", "headerValue"); headers.set("header", "headerValue");
headers.setContentType(MediaType.TEXT_PLAIN); headers.setContentType(MediaType.TEXT_PLAIN);
headers.setLocation(new URI("/redirectFoo")); headers.setLocation(URI.create("/redirectFoo"));
headers.put("Set-Cookie", cookieValues); headers.put("Set-Cookie", cookieValues);
String heading = "MockHttpServletResponse"; String heading = "MockHttpServletResponse";