diff --git a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java index cc6b1b335f7..d7f05ac097d 100644 --- a/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java +++ b/integration/spring-boot/autoconfigure/src/test/java/org/apache/cxf/spring/boot/autoconfigure/CxfAutoConfigurationTest.java @@ -36,9 +36,7 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.junit.After; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; @@ -50,6 +48,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; /** @@ -59,9 +58,6 @@ */ public class CxfAutoConfigurationTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - private AnnotationConfigWebApplicationContext context; @After @@ -80,13 +76,13 @@ public void defaultConfiguration() { @Test public void customPathMustBeginWithASlash() { - this.thrown.expect(UnsatisfiedDependencyException.class); - this.thrown.expectCause( + UnsatisfiedDependencyException exception = assertThrows(UnsatisfiedDependencyException.class, + () -> load(CxfAutoConfiguration.class, "cxf.path=invalid")); + assertThat(exception.getCause(), allOf(instanceOf(ConfigurationPropertiesBindException.class), hasProperty("cause", allOf(instanceOf(BindException.class), hasProperty("cause", allOf(instanceOf(BindValidationException.class), hasProperty("message", containsString("Path must start with /")))))))); - load(CxfAutoConfiguration.class, "cxf.path=invalid"); } @Test diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java index 63dfe41ff68..a5778ee9293 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java @@ -38,9 +38,7 @@ import org.apache.cxf.jaxrs.resources.UriBuilderWrongAnnotations; import org.apache.cxf.jaxrs.utils.JAXRSUtils; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; @@ -48,9 +46,6 @@ public class UriBuilderImplTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testFromUriRelativePath() throws Exception { UriBuilder builder = UriBuilder.fromUri("path"); @@ -863,12 +858,11 @@ public void testAddPathMethodNull() throws Exception { @Test public void testAddPathMethodNoAnnotation() throws Exception { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage( - String.format("Method '%s.getBook' is not annotated with Path", - BookStore.class.getCanonicalName())); Method noAnnot = BookStore.class.getMethod("getBook", String.class); - new UriBuilderImpl().path(noAnnot).build(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> new UriBuilderImpl().path(noAnnot).build()); + assertEquals(String.format("Method '%s.getBook' is not annotated with Path", + BookStore.class.getCanonicalName()), ex.getMessage()); } @Test diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalInvocationHandlerTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalInvocationHandlerTest.java index cc534b9c8b2..812572edd7f 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalInvocationHandlerTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/tl/ThreadLocalInvocationHandlerTest.java @@ -24,9 +24,10 @@ import java.net.SocketException; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; public class ThreadLocalInvocationHandlerTest { @@ -37,13 +38,6 @@ public class ThreadLocalInvocationHandlerTest { private TestIface testIface; - private ExpectedException expectedException = ExpectedException.none(); - - @Rule - public ExpectedException getExpectedExceptionRule() { - return expectedException; - } - @Before public void setUp() throws Exception { ThreadLocalInvocationHandler subject = new ThreadLocalInvocationHandler<>(); @@ -55,34 +49,30 @@ public void setUp() throws Exception { @Test public void testCheckedExceptionPropagation() throws Exception { - expectedException.expect(SocketException.class); - expectedException.expectMessage(CHECKED_EXCEPTION_MSG); - - testIface.throwCheckedException(); + SocketException ex = assertThrows(SocketException.class, + () -> testIface.throwCheckedException()); + assertEquals(CHECKED_EXCEPTION_MSG, ex.getMessage()); } @Test public void testUncheckedExceptionPropagation() { - expectedException.expect(IndexOutOfBoundsException.class); - expectedException.expectMessage(UNCHECKED_EXCEPTION_MSG); - - testIface.throwUncheckedException(); + IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, + () -> testIface.throwUncheckedException()); + assertEquals(UNCHECKED_EXCEPTION_MSG, ex.getMessage()); } @Test public void testErrorPropagation() { - expectedException.expect(AnnotationFormatError.class); - expectedException.expectMessage(ERROR_MSG); - - testIface.throwError(); + AnnotationFormatError ex = assertThrows(AnnotationFormatError.class, + () -> testIface.throwError()); + assertEquals(ERROR_MSG, ex.getMessage()); } @Test public void testThrowablePropagation() throws Throwable { - expectedException.expect(Throwable.class); - expectedException.expectMessage(THROWABLE_MSG); - - testIface.throwThrowable(); + Throwable ex = assertThrows(Throwable.class, + () -> testIface.throwThrowable()); + assertEquals(THROWABLE_MSG, ex.getMessage()); } private interface TestIface { diff --git a/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/SseEventSinkContextProviderTest.java b/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/SseEventSinkContextProviderTest.java index 8f59c4104e3..b91e0a8214a 100644 --- a/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/SseEventSinkContextProviderTest.java +++ b/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/SseEventSinkContextProviderTest.java @@ -37,20 +37,17 @@ import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class SseEventSinkContextProviderTest { private static final OutboundSseEvent EVENT = new OutboundSseEventImpl.BuilderImpl().build(); - - @Rule - public ExpectedException exception = ExpectedException.none(); private SseEventSinkContextProvider provider; private Message message; @@ -96,11 +93,10 @@ public void testCreateSseEventSinkWithDefaultBufferSize() { // The buffer overflow should trigger message rejection and exceptional completion final CompletableFuture overflow = sink.send(EVENT).toCompletableFuture(); assertThat(overflow.isCompletedExceptionally(), equalTo(true)); - - exception.expect(CompletionException.class); - exception.expectMessage("The buffer is full (10000), unable to queue SSE event for send."); - overflow.join(); + final CompletionException ex = assertThrows(CompletionException.class, overflow::join); + assertThat(ex.getCause().getMessage(), + startsWith("The buffer is full (10000)")); } @Test @@ -118,10 +114,9 @@ public void testCreateSseEventSinkWithCustomBufferSize() { // The buffer overflow should trigger message rejection and exceptional completion final CompletableFuture overflow = sink.send(EVENT).toCompletableFuture(); assertThat(overflow.isCompletedExceptionally(), equalTo(true)); - - exception.expect(CompletionException.class); - exception.expectMessage("The buffer is full (20000), unable to queue SSE event for send."); - overflow.join(); + final CompletionException ex = assertThrows(CompletionException.class, overflow::join); + assertThat(ex.getCause().getMessage(), + startsWith("The buffer is full (20000)")); } } diff --git a/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java b/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java index 501840bef88..5b65870007a 100644 --- a/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java +++ b/rt/rs/sse/src/test/java/org/apache/cxf/jaxrs/sse/client/SseEventSourceImplTest.java @@ -51,15 +51,14 @@ import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.awaitility.Awaitility.await; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; public class SseEventSourceImplTest { @@ -109,9 +108,6 @@ enum Type { private static final Map SERVERS = new EnumMap<>(Type.class); - @Rule - public ExpectedException exception = ExpectedException.none(); - private final List events = new ArrayList<>(); private final List errors = new ArrayList<>(); @@ -207,8 +203,8 @@ public void testWhenTryToConnectTwiceSecondAttemtShouldFail() throws Interrupted // The attempt to open the SSE connection in another thread at the same // time should fail final Future future = executor.submit(() -> eventSource.open()); - exception.expectCause(instanceOf(IllegalStateException.class)); - assertThat(future.get(), equalTo(null)); + final ExecutionException ex = assertThrows(ExecutionException.class, future::get); + assertThat(ex.getCause(), instanceOf(IllegalStateException.class)); assertThat(eventSource.isOpen(), equalTo(true)); assertThat(events.size(), equalTo(1)); diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSClientMetricsTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSClientMetricsTest.java index 903589ec550..d51acb45a11 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSClientMetricsTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSClientMetricsTest.java @@ -42,7 +42,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; @@ -53,6 +52,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.times; @@ -60,7 +60,6 @@ @RunWith(MockitoJUnitRunner.class) public class JAXRSClientMetricsTest { @Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); - @Rule public ExpectedException expectedException = ExpectedException.none(); private MetricsProvider provider; private MetricsContext operationContext; @@ -102,8 +101,7 @@ public void usingClientProxyStopIsCalledWhenServerReturnsNotFound() throws Excep try { final Library client = factory.create(Library.class); - expectedException.expect(NotFoundException.class); - client.getBook(10); + assertThrows(NotFoundException.class, () -> client.getBook(10)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -123,11 +121,11 @@ public void usingClientStopIsCalledWhenServerReturnsNotFound() throws Exception .withStatus(404))); try { - expectedException.expect(ProcessingException.class); - client - .target("http://localhost:" + wireMockRule.port() + "/books/10") - .request(MediaType.APPLICATION_JSON).get() - .readEntity(Book.class); + assertThrows(ProcessingException.class, + () -> client + .target("http://localhost:" + wireMockRule.port() + "/books/10") + .request(MediaType.APPLICATION_JSON).get() + .readEntity(Book.class)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -145,12 +143,12 @@ public void usingClientStopIsCalledWhenConnectionIsRefused() throws Exception { .register(JacksonJsonProvider.class); try { - expectedException.expect(ProcessingException.class); - client - .target("http://localhost:" + port + "/books/10") - .request(MediaType.APPLICATION_JSON) - .get() - .readEntity(Book.class); + assertThrows(ProcessingException.class, + () -> client + .target("http://localhost:" + port + "/books/10") + .request(MediaType.APPLICATION_JSON) + .get() + .readEntity(Book.class)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -195,8 +193,7 @@ public void usingWebClientStopIsCalledWhenServerReturnsNotFound() throws Excepti .withStatus(404))); try { - expectedException.expect(ProcessingException.class); - client.get().readEntity(Book.class); + assertThrows(ProcessingException.class, () -> client.get().readEntity(Book.class)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSServerMetricsTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSServerMetricsTest.java index b0db62c138a..cbf0eb922e7 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSServerMetricsTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/metrics/JAXRSServerMetricsTest.java @@ -44,13 +44,12 @@ import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; @@ -63,8 +62,6 @@ public class JAXRSServerMetricsTest extends AbstractClientServerTestBase { private static MetricsProvider provider; private static MetricsContext operationContext; private static MetricsContext resourceContext; - - @Rule public ExpectedException expectedException = ExpectedException.none(); public static class BookLibrary implements Library { @Override @@ -135,8 +132,7 @@ public void usingClientProxyStopIsCalledWhenServerReturnsNotFound() throws Excep try { final Library client = factory.create(Library.class); - expectedException.expect(NotFoundException.class); - client.getBook(10); + assertThrows(NotFoundException.class, () -> client.getBook(10)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -151,11 +147,11 @@ public void usingClientStopIsCalledWhenServerReturnsNotFound() throws Exception .register(JacksonJsonProvider.class); try { - expectedException.expect(ProcessingException.class); - client - .target("http://localhost:" + PORT + "/books/10") - .request(MediaType.APPLICATION_JSON).get() - .readEntity(Book.class); + assertThrows(ProcessingException.class, + () -> client + .target("http://localhost:" + PORT + "/books/10") + .request(MediaType.APPLICATION_JSON).get() + .readEntity(Book.class)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -188,8 +184,7 @@ public void usingWebClientStopIsCalledWhenServerReturnsNotFound() throws Excepti Arrays.asList(JacksonJsonProvider.class)); try { - expectedException.expect(ProcessingException.class); - client.get().readEntity(Book.class); + assertThrows(ProcessingException.class, () -> client.get().readEntity(Book.class)); } finally { Mockito.verify(resourceContext, times(1)).start(any(Exchange.class)); Mockito.verify(resourceContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -203,8 +198,7 @@ public void usingWebClientStopIsCalledWhenUrlIsNotServed() throws Exception { Arrays.asList(JacksonJsonProvider.class)); try { - expectedException.expect(ProcessingException.class); - client.get().readEntity(Book.class); + assertThrows(ProcessingException.class, () -> client.get().readEntity(Book.class)); } finally { Mockito.verifyNoInteractions(resourceContext); Mockito.verifyNoInteractions(operationContext); diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/metrics/JAXWSClientMetricsTest.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/metrics/JAXWSClientMetricsTest.java index 8ff02602d61..5e4fa611b7c 100644 --- a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/metrics/JAXWSClientMetricsTest.java +++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/metrics/JAXWSClientMetricsTest.java @@ -34,22 +34,19 @@ import org.apache.cxf.test.AbstractCXFSpringTest; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.times; @RunWith(MockitoJUnitRunner.class) public class JAXWSClientMetricsTest extends AbstractCXFSpringTest { - @Rule public ExpectedException expectedException = ExpectedException.none(); - private MetricsProvider provider; private MetricsContext operationContext; private MetricsContext resourceContext; @@ -107,8 +104,7 @@ public void usingClientProxyStopIsCalledWhenServerReturnsFault() throws Exceptio try { final Client client = factory.create(); - expectedException.expect(SoapFault.class); - client.invoke("getBook", 11); + assertThrows(SoapFault.class, () -> client.invoke("getBook", 11)); } finally { Mockito.verify(operationContext, times(1)).start(any(Exchange.class)); Mockito.verify(operationContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class)); @@ -127,8 +123,7 @@ public void usingClientProxyStopIsCalledForUnsupportedOperation() throws Excepti try { final Client client = factory.create(); - expectedException.expect(UncheckedException.class); - client.invoke("getBooks"); + assertThrows(UncheckedException.class, () -> client.invoke("getBooks")); } finally { Mockito.verifyNoInteractions(endpointContext); Mockito.verifyNoInteractions(operationContext); diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/brave/jaxrs/AbstractBraveTracingTest.java b/systests/tracing/src/test/java/org/apache/cxf/systest/brave/jaxrs/AbstractBraveTracingTest.java index c7839595cf3..43afd0cc56b 100644 --- a/systests/tracing/src/test/java/org/apache/cxf/systest/brave/jaxrs/AbstractBraveTracingTest.java +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/brave/jaxrs/AbstractBraveTracingTest.java @@ -45,9 +45,7 @@ import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.junit.After; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.cxf.systest.HasSize.hasSize; import static org.apache.cxf.systest.brave.BraveTestSupport.PARENT_SPAN_ID_NAME; @@ -65,14 +63,12 @@ import static org.hamcrest.collection.IsMapContaining.hasKey; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; public abstract class AbstractBraveTracingTest extends AbstractClientServerTestBase { private static final AtomicLong RANDOM = new AtomicLong(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private final Tracing brave = Tracing.newBuilder() .addSpanHandler(new TestSpanHandler()) .build(); @@ -337,16 +333,13 @@ public void testThatNewSpanIsCreatedOnClientTimeout() { httpClientPolicy.setReceiveTimeout(100); WebClient.getConfig(client).getHttpConduit().setClient(httpClientPolicy); - expectedException.expect(ProcessingException.class); - try { - client.get(); - } finally { - await().atMost(Duration.ofSeconds(5L)).untilAsserted(() -> - assertThat(TestSpanHandler.getAllSpans(), hasSize(2))); - assertThat(TestSpanHandler.getAllSpans().get(0).name(), equalTo("GET " + client.getCurrentURI())); - assertThat(TestSpanHandler.getAllSpans().get(0).tags(), hasKey("error")); - assertThat(TestSpanHandler.getAllSpans().get(1).name(), equalTo("GET /bookstore/books/long")); - } + assertThrows(ProcessingException.class, () -> client.get()); + + await().atMost(Duration.ofSeconds(5L)).untilAsserted(() -> + assertThat(TestSpanHandler.getAllSpans(), hasSize(2))); + assertThat(TestSpanHandler.getAllSpans().get(0).name(), equalTo("GET " + client.getCurrentURI())); + assertThat(TestSpanHandler.getAllSpans().get(0).tags(), hasKey("error")); + assertThat(TestSpanHandler.getAllSpans().get(1).name(), equalTo("GET /bookstore/books/long")); } @Test diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentelemetry/JaxrsOpenTelemetryTracingTest.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentelemetry/JaxrsOpenTelemetryTracingTest.java index 5da7c704b9d..6c825e09a90 100644 --- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentelemetry/JaxrsOpenTelemetryTracingTest.java +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentelemetry/JaxrsOpenTelemetryTracingTest.java @@ -75,9 +75,7 @@ import org.junit.After; import org.junit.BeforeClass; import org.junit.ClassRule; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.cxf.systest.HasSize.hasSize; import static org.apache.cxf.systest.jaxrs.tracing.opentelemetry.HasAttribute.hasAttribute; @@ -90,6 +88,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class JaxrsOpenTelemetryTracingTest extends AbstractClientServerTestBase { @@ -100,9 +99,6 @@ public class JaxrsOpenTelemetryTracingTest extends AbstractClientServerTestBase private static final AtomicLong RANDOM = new AtomicLong(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - @BeforeClass public static void startServers() { AbstractResourceInfo.clearAllMaps(); @@ -434,15 +430,12 @@ public void testThatNewSpanIsCreatedOnClientTimeout() { httpClientPolicy.setReceiveTimeout(100); WebClient.getConfig(client).getHttpConduit().setClient(httpClientPolicy); - expectedException.expect(ProcessingException.class); - try { - client.get(); - } finally { - await().atMost(Duration.ofSeconds(5L)).untilAsserted(() -> assertThat(otelRule.getSpans(), hasSize(2))); - assertThat(otelRule.getSpans().get(0).getName(), equalTo("GET " + client.getCurrentURI())); - assertThat(otelRule.getSpans().get(0).getStatus().getStatusCode(), equalTo(StatusCode.ERROR)); - assertThat(otelRule.getSpans().get(1).getName(), equalTo("GET /bookstore/books/long")); - } + assertThrows(ProcessingException.class, () -> client.get()); + + await().atMost(Duration.ofSeconds(5L)).untilAsserted(() -> assertThat(otelRule.getSpans(), hasSize(2))); + assertThat(otelRule.getSpans().get(0).getName(), equalTo("GET " + client.getCurrentURI())); + assertThat(otelRule.getSpans().get(0).getStatus().getStatusCode(), equalTo(StatusCode.ERROR)); + assertThat(otelRule.getSpans().get(1).getName(), equalTo("GET /bookstore/books/long")); } @Test diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/JaxrsOpenTracingTracingTest.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/JaxrsOpenTracingTracingTest.java index 5207ff5ed48..c807d79ebb8 100644 --- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/JaxrsOpenTracingTracingTest.java +++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/JaxrsOpenTracingTracingTest.java @@ -66,9 +66,7 @@ import org.junit.After; import org.junit.BeforeClass; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.apache.cxf.systest.HasSize.hasSize; import static org.apache.cxf.systest.jaxrs.tracing.opentracing.HasSpan.hasSpan; @@ -80,6 +78,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class JaxrsOpenTracingTracingTest extends AbstractClientServerTestBase { @@ -89,9 +88,6 @@ public class JaxrsOpenTracingTracingTest extends AbstractClientServerTestBase { private static final InMemoryReporter REPORTER = new InMemoryReporter(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private final Tracer tracer = new JaegerTracer.Builder("tracer-jaxrs") .withSampler(new ConstSampler(true)) .withReporter(REPORTER) @@ -382,15 +378,12 @@ public void testThatNewSpanIsCreatedOnClientTimeout() { httpClientPolicy.setReceiveTimeout(100); WebClient.getConfig(client).getHttpConduit().setClient(httpClientPolicy); - expectedException.expect(ProcessingException.class); - try { - client.get(); - } finally { - await().atMost(Duration.ofSeconds(5L)).untilAsserted(() -> assertThat(REPORTER.getSpans(), hasSize(2))); - assertThat(REPORTER.getSpans().get(0).getOperationName(), equalTo("GET " + client.getCurrentURI())); - assertThat(REPORTER.getSpans().get(0).getTags(), hasItem(Tags.ERROR.getKey(), Boolean.TRUE)); - assertThat(REPORTER.getSpans().get(1).getOperationName(), equalTo("GET /bookstore/books/long")); - } + assertThrows(ProcessingException.class, () -> client.get()); + + await().atMost(Duration.ofSeconds(5L)).untilAsserted(() -> assertThat(REPORTER.getSpans(), hasSize(2))); + assertThat(REPORTER.getSpans().get(0).getOperationName(), equalTo("GET " + client.getCurrentURI())); + assertThat(REPORTER.getSpans().get(0).getTags(), hasItem(Tags.ERROR.getKey(), Boolean.TRUE)); + assertThat(REPORTER.getSpans().get(1).getOperationName(), equalTo("GET /bookstore/books/long")); } @Test diff --git a/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java b/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java index 9a7b7993b9e..8817031d95d 100644 --- a/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java +++ b/systests/transports/src/test/java/org/apache/cxf/systest/http/HTTPConduitIoExceptionsTest.java @@ -33,20 +33,16 @@ import org.apache.cxf.transport.http.HTTPException; import org.apache.hello_world.Greeter; import org.apache.hello_world.services.SOAPService; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; import org.junit.BeforeClass; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class HTTPConduitIoExceptionsTest extends AbstractBusClientServerTestBase { - @Rule public final ExpectedException exception = ExpectedException.none(); - private final QName serviceName = new QName("http://apache.org/hello_world", "SOAPService"); @@ -68,11 +64,9 @@ public void testNoIoExceptions() throws Exception { try (Client client = (Client)greeter) { client.getRequestContext().put(HTTPConduit.NO_IO_EXCEPTIONS, true); - - exception.expect(SOAPFaultException.class); - exception.expectMessage("Go away"); - - greeter.sayHi(); + + SOAPFaultException ex = assertThrows(SOAPFaultException.class, () -> greeter.sayHi()); + assertTrue(ex.getMessage().contains("Go away")); } } @@ -80,54 +74,24 @@ public void testNoIoExceptions() throws Exception { public void testServiceUnavailable() throws Exception { final Greeter greeter = getGreeter(); - exception.expect(WebServiceException.class); - exception.expectCause(new TypeSafeMatcher() { - private static final String message = "HTTP response '503: Service Unavailable' when " - + "communicating with http://localhost:" + BadServer.PORT + "/Mortimer"; - - @Override - public void describeTo(Description description) { - description - .appendValue(HTTPException.class) - .appendText(" and message ") - .appendValue(message); - } - - @Override - protected boolean matchesSafely(Throwable item) { - return item instanceof HTTPException && item.getMessage().equals(message); - } - }); - - - greeter.sayHi(); + WebServiceException ex = assertThrows(WebServiceException.class, () -> greeter.sayHi()); + final String message = "HTTP response '503: Service Unavailable' when " + + "communicating with http://localhost:" + BadServer.PORT + "/Mortimer"; + Throwable cause = ex.getCause(); + assertTrue(cause instanceof HTTPException); + assertEquals(message, cause.getMessage()); } @Test public void testNotFound() throws Exception { final Greeter greeter = getGreeter(); - exception.expect(WebServiceException.class); - exception.expectCause(new TypeSafeMatcher() { - private static final String message = "HTTP response '404: Not Found' when " - + "communicating with http://localhost:" + BadServer.PORT + "/Mortimer"; - - @Override - public void describeTo(Description description) { - description - .appendValue(HTTPException.class) - .appendText(" and message ") - .appendValue(message); - } - - @Override - protected boolean matchesSafely(Throwable item) { - return item instanceof HTTPException && item.getMessage().equals(message); - } - }); - - - greeter.greetMe("Test"); + WebServiceException ex = assertThrows(WebServiceException.class, () -> greeter.greetMe("Test")); + final String message = "HTTP response '404: Not Found' when " + + "communicating with http://localhost:" + BadServer.PORT + "/Mortimer"; + Throwable cause = ex.getCause(); + assertTrue(cause instanceof HTTPException); + assertEquals(message, cause.getMessage()); } private Greeter getGreeter() throws MalformedURLException {