diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractReactiveTransactionAspectTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractReactiveTransactionAspectTests.java index 34e961b303e..c4e673a80d0 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractReactiveTransactionAspectTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractReactiveTransactionAspectTests.java @@ -48,18 +48,18 @@ import static org.mockito.Mockito.verifyZeroInteractions; */ public abstract class AbstractReactiveTransactionAspectTests { - protected Method exceptionalMethod; - protected Method getNameMethod; protected Method setNameMethod; + protected Method exceptionalMethod; + @Before public void setup() throws Exception { - exceptionalMethod = TestBean.class.getMethod("exceptional", Throwable.class); getNameMethod = TestBean.class.getMethod("getName"); setNameMethod = TestBean.class.getMethod("setName", String.class); + exceptionalMethod = TestBean.class.getMethod("exceptional", Throwable.class); } @@ -382,11 +382,11 @@ public abstract class AbstractReactiveTransactionAspectTests { public interface TestBean { - Mono exceptional(Throwable t); - Mono getName(); Publisher setName(String name); + + Mono exceptional(Throwable t); } @@ -400,7 +400,7 @@ public abstract class AbstractReactiveTransactionAspectTests { } @Override - public Mono setName(String name) { + public Publisher setName(String name) { return Mono.fromRunnable(() -> this.name = name); } diff --git a/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractTransactionAspectTests.java b/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractTransactionAspectTests.java index 686b85b9001..68584d7f3ca 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractTransactionAspectTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/interceptor/AbstractTransactionAspectTests.java @@ -48,31 +48,31 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; /** - * Mock object based tests for transaction aspects. - * True unit test in that it tests how the transaction aspect uses - * the PlatformTransactionManager helper, rather than indirectly - * testing the helper implementation. + * Mock object based tests for transaction aspects. A true unit test in that it + * tests how the transaction aspect uses the PlatformTransactionManager helper, + * rather than indirectly testing the helper implementation. * - * This is a superclass to allow testing both the AOP Alliance MethodInterceptor + *

This is a superclass to allow testing both the AOP Alliance MethodInterceptor * and the AspectJ aspect. * * @author Rod Johnson + * @author Juergen Hoeller * @since 16.03.2003 */ public abstract class AbstractTransactionAspectTests { - protected Method exceptionalMethod; - protected Method getNameMethod; protected Method setNameMethod; + protected Method exceptionalMethod; + @Before public void setup() throws Exception { - exceptionalMethod = ITestBean.class.getMethod("exceptional", Throwable.class); getNameMethod = ITestBean.class.getMethod("getName"); setNameMethod = ITestBean.class.getMethod("setName", String.class); + exceptionalMethod = ITestBean.class.getMethod("exceptional", Throwable.class); } diff --git a/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java b/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java index 86942c60375..a7841926580 100644 --- a/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java +++ b/spring-web/src/test/java/org/springframework/http/client/AbstractHttpRequestFactoryTestCase.java @@ -17,6 +17,7 @@ package org.springframework.http.client; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Locale; @@ -34,6 +35,7 @@ import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -71,13 +73,9 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod()); assertEquals("Invalid HTTP URI", uri, request.getURI()); - ClientHttpResponse response = request.execute(); - try { + try (ClientHttpResponse response = request.execute()) { assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode()); } - finally { - response.close(); - } } @Test @@ -90,7 +88,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb request.getHeaders().add(headerName, headerValue1); String headerValue2 = "value2"; request.getHeaders().add(headerName, headerValue2); - final byte[] body = "Hello World".getBytes("UTF-8"); + final byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8); request.getHeaders().setContentLength(body.length); if (request instanceof StreamingHttpOutputMessage) { @@ -101,17 +99,13 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb StreamUtils.copy(body, request.getBody()); } - ClientHttpResponse response = request.execute(); - try { + try (ClientHttpResponse response = request.execute()) { assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode()); assertTrue("Header not found", response.getHeaders().containsKey(headerName)); assertEquals("Header value not found", Arrays.asList(headerValue1, headerValue2), response.getHeaders().get(headerName)); byte[] result = FileCopyUtils.copyToByteArray(response.getBody()); - assertTrue("Invalid body", Arrays.equals(body, result)); - } - finally { - response.close(); + assertArrayEquals("Invalid body", body, result); } } @@ -119,7 +113,7 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb public void multipleWrites() throws Exception { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST); - final byte[] body = "Hello World".getBytes("UTF-8"); + final byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8); if (request instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request; streamingRequest.setBody(outputStream -> { @@ -142,10 +136,10 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST); request.getHeaders().add("MyHeader", "value"); - byte[] body = "Hello World".getBytes("UTF-8"); + byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> { FileCopyUtils.copy(body, request.getBody()); - try(ClientHttpResponse response = request.execute()) { + try (ClientHttpResponse response = request.execute()) { request.getHeaders().add("MyHeader", "value"); } }); @@ -190,13 +184,9 @@ public abstract class AbstractHttpRequestFactoryTestCase extends AbstractMockWeb URI uri = new URI(baseUrl + "/params?param1=value¶m2=value1¶m2=value2"); ClientHttpRequest request = factory.createRequest(uri, HttpMethod.GET); - ClientHttpResponse response = request.execute(); - try { + try (ClientHttpResponse response = request.execute()) { assertEquals("Invalid status code", HttpStatus.OK, response.getStatusCode()); } - finally { - response.close(); - } } }