This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 672ff53c20abdc68415ea0e5e4cb934599d576d3 Author: Otavio R. Piske <[email protected]> AuthorDate: Sat Jan 24 10:11:04 2026 +0100 CAMEL-21196: modernize exception-based assertions in camel-jetty Signed-off-by: Otavio R. Piske <[email protected]> --- .../camel/component/http/HeaderFilteringTest.java | 1 - .../camel/component/jetty/HttpBasicAuthTest.java | 18 +++++----- .../camel/component/jetty/HttpBridgeRouteTest.java | 12 +++---- .../HttpOperationsFailedExceptionUriTest.java | 17 +++++----- .../component/jetty/HttpPollingConsumerTest.java | 13 ++++---- .../jetty/HttpProducerOkStatusCodeRangeTest.java | 32 +++++++++--------- .../component/jetty/HttpProducerSOTimeoutTest.java | 15 ++++----- .../jetty/HttpRedirectNoLocationTest.java | 21 ++++++------ .../camel/component/jetty/HttpRedirectTest.java | 38 +++++++++++----------- .../component/jetty/HttpStreamCacheFileTest.java | 17 +++++----- .../camel/component/jetty/HttpsAsyncRouteTest.java | 18 ++++------ .../jetty/JettyResponseBodyWhenErrorTest.java | 27 ++++++++------- .../JettyRouteWithUnknownSocketPropertiesTest.java | 14 ++++---- ...ttyRouteWithUnknownSslSocketPropertiesTest.java | 16 ++++----- .../component/jetty/JettySuspendResumeTest.java | 13 +++----- .../camel/component/jetty/JettySuspendTest.java | 13 +++----- .../camel/component/jetty/JettyXsltTest.java | 15 ++++----- .../component/jetty/SpringHttpsRouteTest.java | 12 +++---- .../jetty/SpringJettyNoConnectionTest.java | 13 ++++---- .../async/JettyAsyncContinuationTimeoutTest.java | 23 +++++++------ .../component/jetty/javabody/HttpJavaBodyTest.java | 28 +++++++--------- .../component/jetty/proxy/HttpClientProxyTest.java | 35 +++++++++----------- .../HttpClientProxyTransferExceptionTest.java | 26 +++++++-------- .../jetty/rest/RestJettyBasicAuthTest.java | 17 +++++----- .../rest/RestJettyBindingModeJacksonXmlTest.java | 13 +++----- .../jetty/rest/RestJettyBindingModeJsonTest.java | 13 +++----- .../jetty/rest/RestJettyBindingModeXmlTest.java | 11 +++---- .../jetty/rest/RestJettyMethodNotAllowedTest.java | 26 +++++++-------- .../JettyRestProducerInvalidApiDocTest.java | 26 +++++++-------- 29 files changed, 244 insertions(+), 299 deletions(-) diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HeaderFilteringTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HeaderFilteringTest.java index a10d17ef2cd2..058c8d4496bb 100644 --- a/components/camel-http/src/test/java/org/apache/camel/component/http/HeaderFilteringTest.java +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HeaderFilteringTest.java @@ -27,7 +27,6 @@ import java.util.Collections; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; import org.apache.camel.Producer; -import org.apache.camel.http.base.HttpOperationFailedException; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.support.DefaultExchange; diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java index 7bbb3e913b59..4e260dba4853 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBasicAuthTest.java @@ -40,7 +40,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpBasicAuthTest extends BaseJettyTest { @@ -79,14 +79,14 @@ public class HttpBasicAuthTest extends BaseJettyTest { @Test public void testHttpBasicAuthInvalidPassword() { - try { - template.requestBody("http://localhost:{{port}}/test?authMethod=Basic&authUsername=donald&authPassword=sorry", - "Hello World", String.class); - fail("Should have thrown exception"); - } catch (RuntimeCamelException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(401, cause.getStatusCode()); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody( + "http://localhost:{{port}}/test?authMethod=Basic&authUsername=donald&authPassword=sorry", + "Hello World", String.class), + "Should have thrown exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(401, cause.getStatusCode()); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java index dce0d2943d30..4f9935a5fc79 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java @@ -25,8 +25,8 @@ import org.apache.camel.builder.RouteBuilder; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class HttpBridgeRouteTest extends BaseJettyTest { @@ -40,12 +40,10 @@ public class HttpBridgeRouteTest extends BaseJettyTest { response = template.requestBody("http://localhost:" + port1 + "/hello/world", "hello", String.class); assertEquals("/hello/world", response, "Get a wrong response"); - try { - template.requestBody("http://localhost:" + port2 + "/hello/world", "hello", String.class); - fail("Expect exception here!"); - } catch (Exception ex) { - assertTrue(ex instanceof RuntimeCamelException, "We should get a RuntimeCamelException"); - } + Exception ex = assertThrows(Exception.class, + () -> template.requestBody("http://localhost:" + port2 + "/hello/world", "hello", String.class), + "Expect exception here!"); + assertTrue(ex instanceof RuntimeCamelException, "We should get a RuntimeCamelException"); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java index fd36e5b0e652..48bd9c617004 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java @@ -25,20 +25,19 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpOperationsFailedExceptionUriTest extends BaseJettyTest { @Test public void testHttpOperationsFailedExceptionUri() { - try { - template.requestBodyAndHeader("http://localhost:{{port}}/foo?bar=123", null, "foo", 123); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(500, cause.getStatusCode()); - assertEquals("http://localhost:" + getPort() + "/foo?bar=123", cause.getUri()); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBodyAndHeader("http://localhost:{{port}}/foo?bar=123", null, "foo", 123), + "Should have thrown an exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + assertEquals("http://localhost:" + getPort() + "/foo?bar=123", cause.getUri()); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java index 0df65a824604..a4d950919145 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpPollingConsumerTest extends BaseJettyTest { @@ -42,12 +42,11 @@ public class HttpPollingConsumerTest extends BaseJettyTest { @Test public void testReceiveTimeoutTriggered() { - try { - consumer.receiveBody("http://localhost:{{port}}/test", 250, String.class); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - assertIsInstanceOf(SocketTimeoutException.class, e.getCause()); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> consumer.receiveBody("http://localhost:{{port}}/test", 250, String.class), + "Should have thrown an exception"); + + assertIsInstanceOf(SocketTimeoutException.class, e.getCause()); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java index 86ec0ddad2dc..a793d6cb7bce 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerOkStatusCodeRangeTest.java @@ -24,34 +24,32 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpProducerOkStatusCodeRangeTest extends BaseJettyTest { @Test public void testNoOk() { byte[] data = "Hello World".getBytes(); - try { - template.requestBody("http://localhost:{{port}}/test?okStatusCodeRange=200-200", data, String.class); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(209, cause.getStatusCode()); - assertEquals("Not allowed", cause.getResponseBody()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("http://localhost:{{port}}/test?okStatusCodeRange=200-200", data, String.class), + "Should have thrown exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(209, cause.getStatusCode()); + assertEquals("Not allowed", cause.getResponseBody()); } @Test public void testNoOkSingleValue() { byte[] data = "Hello World".getBytes(); - try { - template.requestBody("http://localhost:{{port}}/test?okStatusCodeRange=200", data, String.class); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(209, cause.getStatusCode()); - assertEquals("Not allowed", cause.getResponseBody()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("http://localhost:{{port}}/test?okStatusCodeRange=200", data, String.class), + "Should have thrown exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(209, cause.getStatusCode()); + assertEquals("Not allowed", cause.getResponseBody()); } @Test diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java index bb7e5401d555..003e8497bb3f 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java @@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test for using http client SO timeout @@ -48,13 +48,12 @@ public class HttpProducerSOTimeoutTest extends BaseJettyTest { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); - try { - // we use a timeout of 1 second - template.requestBody("http://localhost:{{port}}/myservice?responseTimeout=1000", null, String.class); - fail("Should throw an exception"); - } catch (RuntimeCamelException e) { - assertIsInstanceOf(SocketTimeoutException.class, e.getCause()); - } + // we use a timeout of 1 second + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody("http://localhost:{{port}}/myservice?responseTimeout=1000", null, String.class), + "Should throw an exception"); + + assertIsInstanceOf(SocketTimeoutException.class, e.getCause()); MockEndpoint.assertIsSatisfied(context); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java index d68e05ea017d..60d4a3822073 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java @@ -26,23 +26,22 @@ import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class HttpRedirectNoLocationTest extends BaseJettyTest { @Test public void testHttpRedirectNoLocation() { - try { - template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(302, cause.getStatusCode()); - assertTrue(cause.isRedirectError()); - assertFalse(cause.hasRedirectLocation()); - assertNull(cause.getRedirectLocation()); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class), + "Should have thrown an exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(302, cause.getStatusCode()); + assertTrue(cause.isRedirectError()); + assertFalse(cause.hasRedirectLocation()); + assertNull(cause.getRedirectLocation()); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java index a97997bcb1ea..4885c812b5c3 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java @@ -26,23 +26,22 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpRedirectTest extends BaseJettyTest { @Test public void testHttpRedirect() { - try { - template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(301, cause.getStatusCode()); - assertEquals(true, cause.isRedirectError()); - assertEquals(true, cause.hasRedirectLocation()); - assertEquals("http://localhost:" + getPort() + "/test", cause.getUri()); - assertEquals("http://localhost:" + getPort() + "/newtest", cause.getRedirectLocation()); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class), + "Should have thrown an exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(301, cause.getStatusCode()); + assertEquals(true, cause.isRedirectError()); + assertEquals(true, cause.hasRedirectLocation()); + assertEquals("http://localhost:" + getPort() + "/test", cause.getUri()); + assertEquals("http://localhost:" + getPort() + "/newtest", cause.getRedirectLocation()); } @Test @@ -51,13 +50,14 @@ public class HttpRedirectTest extends BaseJettyTest { errorEndpoint.expectedMessageCount(1); MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class); resultEndpoint.expectedMessageCount(0); - try { - template.requestBody("direct:start", "Hello World", String.class); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(302, cause.getStatusCode()); - } + + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody("direct:start", "Hello World", String.class), + "Should have thrown an exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(302, cause.getStatusCode()); + errorEndpoint.assertIsSatisfied(); resultEndpoint.assertIsSatisfied(); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java index fd372373fdff..1b56e74beca7 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.io.TempDir; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpStreamCacheFileTest extends BaseJettyTest { @@ -50,14 +50,13 @@ public class HttpStreamCacheFileTest extends BaseJettyTest { @Test public void testStreamCacheToFileShouldBeDeletedInCaseOfException() { - try { - template.requestBody("direct:start", null, String.class); - fail("Should have thrown an exception"); - } catch (CamelExecutionException e) { - HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - String s = context.getTypeConverter().convertTo(String.class, hofe.getResponseBody()); - assertEquals(responseBody, s, "Response body"); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("direct:start", null, String.class), + "Should have thrown an exception"); + + HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + String s = context.getTypeConverter().convertTo(String.class, hofe.getResponseBody()); + assertEquals(responseBody, s, "Response body"); // the temporary files should have been deleted String[] files = testDirectory.list(); diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsAsyncRouteTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsAsyncRouteTest.java index 6a751780b05f..2f08361a4168 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsAsyncRouteTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsAsyncRouteTest.java @@ -45,8 +45,8 @@ import static org.apache.camel.component.jetty.BaseJettyTest.SSL_SYSPROPS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; @ResourceLock(SSL_SYSPROPS) @DisabledOnOs(OS.WINDOWS) @@ -120,11 +120,10 @@ public class HttpsAsyncRouteTest extends HttpsRouteTest { @Test public void testEndpointWithoutHttps() { MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:a", MockEndpoint.class); - try { - template.sendBodyAndHeader("http://localhost:" + port1 + "/test", expectedBody, "Content-Type", "application/xml"); - fail("expect exception on access to https endpoint via http"); - } catch (RuntimeCamelException expected) { - } + assertThrows(RuntimeCamelException.class, + () -> template.sendBodyAndHeader("http://localhost:" + port1 + "/test", expectedBody, "Content-Type", + "application/xml"), + "expect exception on access to https endpoint via http"); assertTrue(mockEndpoint.getExchanges().isEmpty(), "mock endpoint was not called"); } @@ -147,11 +146,8 @@ public class HttpsAsyncRouteTest extends HttpsRouteTest { @Override @Test public void testHelloEndpointWithoutHttps() throws Exception { - try { - new URL("http://localhost:" + port1 + "/hello").openStream(); - fail("expected SocketException on use ot http"); - } catch (SocketException expected) { - } + assertThrows(SocketException.class, () -> new URL("http://localhost:" + port1 + "/hello").openStream(), + "expected SocketException on use ot http"); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java index 4b644564fe7d..87388ab6a85a 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java @@ -26,8 +26,8 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; /** * Unit test for HttpOperationFailedException should contain response body @@ -36,19 +36,18 @@ public class JettyResponseBodyWhenErrorTest extends BaseJettyTest { @Test public void testResponseBodyWhenError() { - try { - template.requestBody("http://localhost:{{port}}/myapp/myservice", "bookid=123"); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(500, cause.getStatusCode()); - String body = context.getTypeConverter().convertTo(String.class, cause.getResponseBody()); - assertTrue(body.indexOf("Damm") > -1); - assertTrue(body.indexOf("IllegalArgumentException") > -1); - assertNotNull(cause.getResponseHeaders()); - String type = cause.getResponseHeaders().get(Exchange.CONTENT_TYPE); - assertTrue(type.startsWith("text/plain")); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody("http://localhost:{{port}}/myapp/myservice", "bookid=123"), + "Should have thrown an exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + String body = context.getTypeConverter().convertTo(String.class, cause.getResponseBody()); + assertTrue(body.indexOf("Damm") > -1); + assertTrue(body.indexOf("IllegalArgumentException") > -1); + assertNotNull(cause.getResponseHeaders()); + String type = cause.getResponseHeaders().get(Exchange.CONTENT_TYPE); + assertTrue(type.startsWith("text/plain")); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java index 59fedfa1e66f..489b3174e06a 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java @@ -24,8 +24,8 @@ import org.eclipse.jetty.server.Server; import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; public class JettyRouteWithUnknownSocketPropertiesTest extends BaseJettyTest { @@ -54,13 +54,11 @@ public class JettyRouteWithUnknownSocketPropertiesTest extends BaseJettyTest { from("jetty:http://localhost:{{port}}/myapp/myservice").to("log:foo"); } }); - try { - context.start(); - fail("Should have thrown exception"); - } catch (Exception e) { - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); - assertTrue(iae.getMessage().endsWith("Unknown parameters=[{doesNotExist=2000}]")); - } + + Exception e = assertThrows(Exception.class, () -> context.start(), "Should have thrown exception"); + + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + assertTrue(iae.getMessage().endsWith("Unknown parameters=[{doesNotExist=2000}]")); } } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java index 2b44b1897a8e..c015c2e19e6e 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java @@ -24,8 +24,8 @@ import org.eclipse.jetty.server.Server; import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeTrue; public class JettyRouteWithUnknownSslSocketPropertiesTest extends BaseJettyTest { @@ -54,14 +54,12 @@ public class JettyRouteWithUnknownSslSocketPropertiesTest extends BaseJettyTest from("jetty:https://localhost:{{port}}/myapp/myservice").to("log:foo"); } }); - try { - context.start(); - fail("Should have thrown exception"); - } catch (Exception e) { - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); - assertTrue(iae.getMessage().endsWith("Unknown parameters=[{doesNotExist=2000}]"), - "Actual message: " + iae.getMessage()); - } + + Exception e = assertThrows(Exception.class, () -> context.start(), "Should have thrown exception"); + + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + assertTrue(iae.getMessage().endsWith("Unknown parameters=[{doesNotExist=2000}]"), + "Actual message: " + iae.getMessage()); } } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java index 8cfa99e0e8de..8da325de8758 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class JettySuspendResumeTest extends BaseJettyTest { @@ -44,13 +44,10 @@ public class JettySuspendResumeTest extends BaseJettyTest { // suspend consumer.suspend(); - try { - template.requestBody(serverUri, "Moon", String.class); - fail("Should throw exception"); - } catch (Exception e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(503, cause.getStatusCode()); - } + Exception e = assertThrows(Exception.class, () -> template.requestBody(serverUri, "Moon", String.class), + "Should throw exception"); + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(503, cause.getStatusCode()); // resume consumer.resume(); diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java index 917f2d8bfe69..52011421fadd 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class JettySuspendTest extends BaseJettyTest { @@ -44,13 +44,10 @@ public class JettySuspendTest extends BaseJettyTest { // suspend consumer.suspend(); - try { - template.requestBody(serverUri, "Moon", String.class); - fail("Should throw exception"); - } catch (Exception e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(503, cause.getStatusCode()); - } + Exception e = assertThrows(Exception.class, () -> template.requestBody(serverUri, "Moon", String.class), + "Should throw exception"); + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(503, cause.getStatusCode()); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyXsltTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyXsltTest.java index bb662210d7ac..fba48b3749d5 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyXsltTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyXsltTest.java @@ -28,8 +28,8 @@ import org.apache.camel.util.ObjectHelper; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class JettyXsltTest extends CamelTestSupport { @@ -46,13 +46,12 @@ public class JettyXsltTest extends CamelTestSupport { @Test void testClasspathInvalidParameter() { - try { - template.requestBody("xslt:org/apache/camel/component/jetty/greeting.xsl?name=greeting.xsl", "<hello>Camel</hello>", - String.class); - fail("Should have thrown exception"); - } catch (ResolveEndpointFailedException e) { - assertTrue(e.getMessage().endsWith("Unknown parameters=[{name=greeting.xsl}]")); - } + ResolveEndpointFailedException e = assertThrows(ResolveEndpointFailedException.class, + () -> template.requestBody("xslt:org/apache/camel/component/jetty/greeting.xsl?name=greeting.xsl", + "<hello>Camel</hello>", String.class), + "Should have thrown exception"); + + assertTrue(e.getMessage().endsWith("Unknown parameters=[{name=greeting.xsl}]")); } @Test diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringHttpsRouteTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringHttpsRouteTest.java index 951a677002ec..c3d444b16d61 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringHttpsRouteTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringHttpsRouteTest.java @@ -45,8 +45,8 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; @ExtendWith(SpringExtension.class) @ContextConfiguration(locations = { "/org/apache/camel/component/jetty/jetty-https.xml" }) @@ -124,11 +124,11 @@ public class SpringHttpsRouteTest { @Test public void testEndpointWithoutHttps() { mockEndpoint.reset(); - try { - template.sendBodyAndHeader("http://localhost:" + port + "/test", expectedBody, "Content-Type", "application/xml"); - fail("expect exception on access to https endpoint via http"); - } catch (RuntimeCamelException expected) { - } + assertThrows(RuntimeCamelException.class, + () -> template.sendBodyAndHeader("http://localhost:" + port + "/test", expectedBody, "Content-Type", + "application/xml"), + "expect exception on access to https endpoint via http"); + assertTrue(mockEndpoint.getExchanges().isEmpty(), "mock endpoint was not called"); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringJettyNoConnectionTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringJettyNoConnectionTest.java index 767fff581a2b..2d48ea6b3ff1 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringJettyNoConnectionTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/SpringJettyNoConnectionTest.java @@ -28,7 +28,7 @@ import org.springframework.context.support.AbstractXmlApplicationContext; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class SpringJettyNoConnectionTest extends CamelSpringTestSupport { @@ -57,11 +57,10 @@ public class SpringJettyNoConnectionTest extends CamelSpringTestSupport { // stop Jetty route so there should not be a connection context.getRouteController().stopRoute("jetty"); - try { - template.requestBody("direct:start", "Moon", String.class); - fail("Should have thrown an exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(ConnectException.class, e.getCause()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("direct:start", "Moon", String.class), + "Should have thrown an exception"); + + assertIsInstanceOf(ConnectException.class, e.getCause()); } } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java index 4b09b772b9b5..394df6b7892f 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java @@ -28,8 +28,8 @@ import org.slf4j.LoggerFactory; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class JettyAsyncContinuationTimeoutTest extends BaseJettyTest { @@ -40,20 +40,19 @@ public class JettyAsyncContinuationTimeoutTest extends BaseJettyTest { getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); StopWatch watch = new StopWatch(); - try { - template.requestBody("http://localhost:{{port}}/myservice", null, String.class); - fail("Should have thrown an exception"); - } catch (CamelExecutionException e) { - LOG.info("Timeout hit and client got reply with failure status code"); + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("http://localhost:{{port}}/myservice", null, String.class), + "Should have thrown an exception"); - long taken = watch.taken(); + LOG.info("Timeout hit and client got reply with failure status code"); - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(504, cause.getStatusCode()); + long taken = watch.taken(); - // should be approx 3-4 sec. - assertTrue(taken < 4500, "Timeout should occur faster than " + taken); - } + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(504, cause.getStatusCode()); + + // should be approx 3-4 sec. + assertTrue(taken < 4500, "Timeout should occur faster than " + taken); MockEndpoint.assertIsSatisfied(context); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/javabody/HttpJavaBodyTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/javabody/HttpJavaBodyTest.java index 52c0fe4e485f..58bf32353dfe 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/javabody/HttpJavaBodyTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/javabody/HttpJavaBodyTest.java @@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpJavaBodyTest extends BaseJettyTest { @@ -177,12 +177,9 @@ public class HttpJavaBodyTest extends BaseJettyTest { }); context.start(); - try { - template.requestBody("http://localhost:{{port}}/myapp/myservice", "Hello World", MyCoolBean.class); - fail("Should fail"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, + () -> template.requestBody("http://localhost:{{port}}/myapp/myservice", "Hello World", MyCoolBean.class), + "Should fail"); } @Test @@ -214,15 +211,14 @@ public class HttpJavaBodyTest extends BaseJettyTest { MyCoolBean cool = new MyCoolBean(123, "Camel"); - try { - template.requestBodyAndHeader("http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, - HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, - MyCoolBean.class); - fail("Should fail"); - } catch (CamelExecutionException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(415, cause.getStatusCode()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBodyAndHeader("http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, + MyCoolBean.class), + "Should fail"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(415, cause.getStatusCode()); } } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTest.java index 5aab4fa799d9..31eb4bb60b3c 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTest.java @@ -28,8 +28,8 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class HttpClientProxyTest extends BaseJettyTest { @@ -41,15 +41,13 @@ public class HttpClientProxyTest extends BaseJettyTest { @Test public void testHttpClientNoProxyException() { - try { - template.requestBody("direct:cool", "Kaboom", String.class); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(500, cause.getStatusCode()); - assertNotNull(cause.getResponseBody()); - assertTrue(cause.getResponseBody().contains("MyAppException")); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("direct:cool", "Kaboom", String.class), "Should have thrown exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + assertNotNull(cause.getResponseBody()); + assertTrue(cause.getResponseBody().contains("MyAppException")); } @Test @@ -63,15 +61,14 @@ public class HttpClientProxyTest extends BaseJettyTest { @Test public void testHttpClientProxyException() throws Exception { MyCoolService proxy = new ProxyBuilder(context).endpoint("direct:cool").build(MyCoolService.class); - try { - proxy.hello("Kaboom"); - fail("Should have thrown exception"); - } catch (UndeclaredThrowableException e) { - HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(500, cause.getStatusCode()); - assertNotNull(cause.getResponseBody()); - assertTrue(cause.getResponseBody().contains("MyAppException")); - } + + UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, + () -> proxy.hello("Kaboom"), "Should have thrown exception"); + + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + assertNotNull(cause.getResponseBody()); + assertTrue(cause.getResponseBody().contains("MyAppException")); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTransferExceptionTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTransferExceptionTest.java index 29be7f7f4213..68f46c8d9140 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTransferExceptionTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/proxy/HttpClientProxyTransferExceptionTest.java @@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class HttpClientProxyTransferExceptionTest extends BaseJettyTest { @@ -37,14 +37,13 @@ public class HttpClientProxyTransferExceptionTest extends BaseJettyTest { @Test public void testHttpClientNoProxyException() { - try { - template.requestBody("direct:cool", "Kaboom"); - fail("Should have thrown an exception"); - } catch (CamelExecutionException e) { - MyAppException cause = assertIsInstanceOf(MyAppException.class, e.getCause()); - assertNotNull(cause); - assertEquals("Kaboom", cause.getName()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("direct:cool", "Kaboom"), + "Should have thrown an exception"); + + MyAppException cause = assertIsInstanceOf(MyAppException.class, e.getCause()); + assertNotNull(cause); + assertEquals("Kaboom", cause.getName()); } @Test @@ -58,12 +57,9 @@ public class HttpClientProxyTransferExceptionTest extends BaseJettyTest { @Test public void testHttpClientProxyException() throws Exception { MyCoolService proxy = new ProxyBuilder(context).endpoint("direct:cool").build(MyCoolService.class); - try { - proxy.hello("Kaboom"); - fail("Should have thrown exception"); - } catch (MyAppException e) { - assertEquals("Kaboom", e.getName()); - } + + MyAppException e = assertThrows(MyAppException.class, () -> proxy.hello("Kaboom"), "Should have thrown exception"); + assertEquals("Kaboom", e.getName()); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java index 12f809ec5bc3..3e4d92812d7d 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBasicAuthTest.java @@ -24,7 +24,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RestJettyBasicAuthTest extends CamelSpringTestSupport { @@ -39,14 +39,13 @@ public class RestJettyBasicAuthTest extends CamelSpringTestSupport { null, String.class); assertEquals("\"pong\"", out); - try { - template.requestBody("http://localhost:9444/ping?authMethod=Basic&authUsername=mickey&authPassword=duck", null, - String.class); - fail("Should not login"); - } catch (Exception e) { - HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(401, hofe.getStatusCode()); - } + Exception e = assertThrows(Exception.class, + () -> template.requestBody("http://localhost:9444/ping?authMethod=Basic&authUsername=mickey&authPassword=duck", + null, String.class), + "Should not login"); + + HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(401, hofe.getStatusCode()); } } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJacksonXmlTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJacksonXmlTest.java index df2bbc1813e4..d5f9e0972213 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJacksonXmlTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJacksonXmlTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RestJettyBindingModeJacksonXmlTest extends BaseJettyTest { @@ -46,18 +46,15 @@ public class RestJettyBindingModeJacksonXmlTest extends BaseJettyTest { } @Test - public void testBindingModeWrong() throws Exception { + public void testBindingModeWrong() throws InterruptedException { MockEndpoint mock = getMockEndpoint("mock:input"); mock.expectedMessageCount(0); // we bind to xml, but send in json, which is not possible String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; - try { - template.sendBody("http://localhost:" + getPort() + "/users/new", body); - fail("Should have thrown exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, + () -> template.sendBody("http://localhost:" + getPort() + "/users/new", body), + "Should have thrown exception"); MockEndpoint.assertIsSatisfied(context); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java index cf942bac9d17..afa57d6be4fb 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeJsonTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RestJettyBindingModeJsonTest extends BaseJettyTest { @@ -46,18 +46,15 @@ public class RestJettyBindingModeJsonTest extends BaseJettyTest { } @Test - public void testBindingModeWrong() throws Exception { + public void testBindingModeWrong() throws InterruptedException { MockEndpoint mock = getMockEndpoint("mock:input"); mock.expectedMessageCount(0); // we bind to json, but send in xml, which is not possible String body = "<user name=\"Donald Duck\" id=\"123\"></user>"; - try { - template.sendBody("http://localhost:" + getPort() + "/users/new", body); - fail("Should have thrown exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, + () -> template.sendBody("http://localhost:" + getPort() + "/users/new", body), + "Should have thrown exception"); MockEndpoint.assertIsSatisfied(context); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java index 7e47cf952886..0b9d63de43bd 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyBindingModeXmlTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RestJettyBindingModeXmlTest extends BaseJettyTest { @@ -52,12 +52,9 @@ public class RestJettyBindingModeXmlTest extends BaseJettyTest { // we bind to xml, but send in json, which is not possible String body = "{\"id\": 123, \"name\": \"Donald Duck\"}"; - try { - template.sendBody("http://localhost:" + getPort() + "/users/new", body); - fail("Should have thrown exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, + () -> template.sendBody("http://localhost:" + getPort() + "/users/new", body), + "Should have thrown exception"); MockEndpoint.assertIsSatisfied(context); } diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java index 1513c21308a8..8144b3fa9209 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyMethodNotAllowedTest.java @@ -23,30 +23,28 @@ import org.apache.camel.http.base.HttpOperationFailedException; import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RestJettyMethodNotAllowedTest extends BaseJettyTest { @Test public void testMethodNotAllowed() { - try { - template.sendBody("http://localhost:" + getPort() + "/users/123/basic", "body"); - fail("Shall not pass!"); - } catch (Exception e) { - HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); - assertEquals(405, hofe.getStatusCode()); - } + Exception e = assertThrows(Exception.class, + () -> template.sendBody("http://localhost:" + getPort() + "/users/123/basic", "body"), + "Shall not pass!"); + + HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(405, hofe.getStatusCode()); } @Test public void testMethodAllowed() { - try { - template.sendBodyAndHeader("http://localhost:" + getPort() + "/users/123/basic", "body", Exchange.HTTP_METHOD, - "GET"); - } catch (Exception e) { - fail("Shall pass with GET http method!"); - } + assertDoesNotThrow( + () -> template.sendBodyAndHeader("http://localhost:" + getPort() + "/users/123/basic", "body", + Exchange.HTTP_METHOD, "GET"), + "Shall pass with GET http method!"); } @Override diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java index edf2dd716b8b..7d551e678603 100644 --- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java +++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/producer/JettyRestProducerInvalidApiDocTest.java @@ -22,7 +22,7 @@ import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class JettyRestProducerInvalidApiDocTest extends CamelTestSupport { @@ -43,13 +43,11 @@ public class JettyRestProducerInvalidApiDocTest extends CamelTestSupport { } }); - try { - context.start(); - fail("Should fail"); - } catch (Exception e) { - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); - assertEquals("OpenApi api-doc does not contain operation for get:/api/hello/unknown/{name}", iae.getMessage()); - } + + Exception e = assertThrows(Exception.class, () -> context.start(), "Should fail"); + + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); + assertEquals("OpenApi api-doc does not contain operation for get:/api/hello/unknown/{name}", iae.getMessage()); } @Test @@ -64,12 +62,10 @@ public class JettyRestProducerInvalidApiDocTest extends CamelTestSupport { } }); - try { - context.start(); - fail("Should fail"); - } catch (Exception e) { - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); - assertEquals("OpenApi api-doc does not contain operation for get:/api/bye", iae.getMessage()); - } + + Exception e = assertThrows(Exception.class, () -> context.start(), "Should fail"); + + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause()); + assertEquals("OpenApi api-doc does not contain operation for get:/api/bye", iae.getMessage()); } }
