You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2020/11/23 09:42:40 UTC

[camel] branch master updated: (chores) Simplified exception testing (#4657)

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 1aedba4  (chores) Simplified exception testing (#4657)
1aedba4 is described below

commit 1aedba49ac630da1a224a2e05108dc835a3614b6
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Mon Nov 23 10:41:19 2020 +0100

    (chores) Simplified exception testing (#4657)
    
    Avoids false positives when the expected exception is thrown by a
    different method other than the one under test.
    
    Components: camel-atom, camel-azure, camel-bindy, camel-braintree,
    camel-cxf-transport, camel-cxf, camel-disruptor, camel-dozer,
    camel-elytron
---
 .../camel/component/atom/AtomProducerTest.java     | 12 +++-----
 .../component/azure/blob/BlobServiceUtilTest.java  | 31 +++++++++++--------
 .../azure/queue/QueueServiceUtilTest.java          | 13 ++++----
 ...ndySimpleFixedLengthMarshallWithNoClipTest.java | 16 +++++-----
 .../braintree/CustomerGatewayIntegrationTest.java  | 33 +++++++++-----------
 .../cxf/transport/CamelDestinationTest.java        | 26 ++++++++--------
 .../cxf/AbstractCXFGreeterRouterTest.java          | 23 +++++++-------
 .../component/cxf/util/CxfEndpointUtilsTest.java   | 28 ++++++++---------
 .../DisruptorConcurrentConsumersNPEIssueTest.java  | 36 ++++++++++------------
 .../camel/component/dozer/CustomMapperTest.java    | 11 +++----
 .../component/elytron/ElytronBearerTokenTest.java  | 24 +++++++--------
 11 files changed, 119 insertions(+), 134 deletions(-)

diff --git a/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomProducerTest.java b/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomProducerTest.java
index 1f0d659..d42dbba 100644
--- a/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomProducerTest.java
+++ b/components/camel-atom/src/test/java/org/apache/camel/component/atom/AtomProducerTest.java
@@ -19,7 +19,7 @@ package org.apache.camel.component.atom;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 /**
  * Unit test for AtomProducer.
@@ -27,13 +27,9 @@ import static org.junit.jupiter.api.Assertions.fail;
 public class AtomProducerTest extends CamelTestSupport {
 
     @Test
-    void testNotYetImplemented() throws Exception {
-        try {
-            context.getEndpoint("atom:file://target/out.atom").createProducer();
-            fail("Should have thrown an UnsupportedOperationException");
-        } catch (UnsupportedOperationException e) {
-            // ok
-        }
+    void testNotYetImplemented() {
+        assertThrows(UnsupportedOperationException.class,
+                () -> context.getEndpoint("atom:file://target/out.atom").createProducer());
     }
 
 }
diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
index ca83c3e..62c39c4 100644
--- a/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
+++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/blob/BlobServiceUtilTest.java
@@ -21,13 +21,14 @@ import java.net.URI;
 import com.microsoft.azure.storage.blob.CloudAppendBlob;
 import com.microsoft.azure.storage.blob.CloudBlob;
 import com.microsoft.azure.storage.blob.CloudBlockBlob;
+import org.apache.camel.Exchange;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.component.azure.common.AzureServiceCommonTestUtil.registerCredentials;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class BlobServiceUtilTest extends CamelTestSupport {
 
@@ -61,12 +62,14 @@ public class BlobServiceUtilTest extends CamelTestSupport {
         BlobServiceEndpoint endpoint = (BlobServiceEndpoint) context
                 .getEndpoint("azure-blob://camelazure/container/blob?azureBlobClient=#azureBlobClient&publicForRead=true"
                              + "&blobType=appendBlob");
-        try {
-            BlobServiceUtil.getConfiguredClient(endpoint.createExchange(), endpoint.getConfiguration());
-            fail();
-        } catch (IllegalArgumentException ex) {
-            assertEquals("Invalid Client Type", ex.getMessage());
-        }
+
+        Exchange exchange = endpoint.createExchange();
+        BlobServiceConfiguration configuration = endpoint.getConfiguration();
+
+        Exception ex = assertThrows(IllegalArgumentException.class,
+                () -> BlobServiceUtil.getConfiguredClient(exchange, configuration));
+
+        assertEquals("Invalid Client Type", ex.getMessage());
     }
 
     @Test
@@ -78,12 +81,14 @@ public class BlobServiceUtilTest extends CamelTestSupport {
         BlobServiceEndpoint endpoint = (BlobServiceEndpoint) context
                 .getEndpoint("azure-blob://camelazure/container/blob2?azureBlobClient=#azureBlobClient&publicForRead=true"
                              + "&blobType=appendBlob");
-        try {
-            BlobServiceUtil.getConfiguredClient(endpoint.createExchange(), endpoint.getConfiguration());
-            fail();
-        } catch (IllegalArgumentException ex) {
-            assertEquals("Invalid Client URI", ex.getMessage());
-        }
+
+        Exchange exchange = endpoint.createExchange();
+        BlobServiceConfiguration configuration = endpoint.getConfiguration();
+
+        Exception ex = assertThrows(IllegalArgumentException.class,
+                () -> BlobServiceUtil.getConfiguredClient(exchange, configuration));
+
+        assertEquals("Invalid Client URI", ex.getMessage());
     }
 
     @Test
diff --git a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java
index 45c9d15..28595a7 100644
--- a/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java
+++ b/components/camel-azure/src/test/java/org/apache/camel/component/azure/queue/QueueServiceUtilTest.java
@@ -26,7 +26,7 @@ import static org.apache.camel.component.azure.common.AzureServiceCommonTestUtil
 import static org.apache.camel.component.azure.common.AzureServiceCommonTestUtil.registerCredentials;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class QueueServiceUtilTest extends CamelTestSupport {
 
@@ -61,12 +61,11 @@ public class QueueServiceUtilTest extends CamelTestSupport {
         QueueServiceEndpoint endpoint = (QueueServiceEndpoint) context
                 .getEndpoint("azure-queue://camelazure/testqueue2?azureQueueClient=#azureQueueClient");
 
-        try {
-            QueueServiceUtil.getConfiguredClient(endpoint.getConfiguration());
-            fail();
-        } catch (IllegalArgumentException ex) {
-            assertEquals("Invalid Client URI", ex.getMessage());
-        }
+        QueueServiceConfiguration configuration = endpoint.getConfiguration();
+        Exception ex = assertThrows(IllegalArgumentException.class,
+                () -> QueueServiceUtil.getConfiguredClient(configuration));
+
+        assertEquals("Invalid Client URI", ex.getMessage());
     }
 
     @Test
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/marshall/simple/BindySimpleFixedLengthMarshallWithNoClipTest.java b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/marshall/simple/BindySimpleFixedLengthMarshallWithNoClipTest.java
index 16fa9ef..ce4472c 100644
--- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/marshall/simple/BindySimpleFixedLengthMarshallWithNoClipTest.java
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/marshall/simple/BindySimpleFixedLengthMarshallWithNoClipTest.java
@@ -35,7 +35,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 BindySimpleFixedLengthMarshallWithNoClipTest extends CamelTestSupport {
 
@@ -57,13 +57,13 @@ public class BindySimpleFixedLengthMarshallWithNoClipTest extends CamelTestSuppo
 
     @Test
     public void testMarshallMessage() throws Exception {
-        try {
-            template.sendBody("direct:start", generateModel());
-            fail("Should have thrown an exception");
-        } catch (CamelExecutionException e) {
-            IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("Length for the firstName must not be larger than allowed, was: 13, allowed: 9", cause.getMessage());
-        }
+        List<Map<String, Object>> model = generateModel();
+        Exception ex = assertThrows(CamelExecutionException.class,
+                () -> template.sendBody("direct:start", model));
+
+        IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, ex.getCause());
+        assertEquals("Length for the firstName must not be larger than allowed, was: 13, allowed: 9",
+                cause.getMessage());
     }
 
     public List<Map<String, Object>> generateModel() {
diff --git a/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/CustomerGatewayIntegrationTest.java b/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/CustomerGatewayIntegrationTest.java
index 2c853fa..eba5917 100644
--- a/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/CustomerGatewayIntegrationTest.java
+++ b/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/CustomerGatewayIntegrationTest.java
@@ -40,8 +40,8 @@ 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.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 CustomerGatewayIntegrationTest extends AbstractBraintreeTestSupport {
 
@@ -118,30 +118,27 @@ public class CustomerGatewayIntegrationTest extends AbstractBraintreeTestSupport
 
     @Test
     public void testUpdateUnknownCustomer() throws Exception {
-        try {
-            String id = "unknown-" + UUID.randomUUID().toString();
+        String id = "unknown-" + UUID.randomUUID().toString();
 
-            HashMap<String, Object> headers = new HashMap<>();
-            headers.put("CamelBraintree.id", id);
+        HashMap<String, Object> headers = new HashMap<>();
+        headers.put("CamelBraintree.id", id);
 
-            requestBodyAndHeaders("direct://UPDATE_IN_BODY",
-                    new CustomerRequest().firstName(id),
-                    headers);
+        CustomerRequest customerRequest = new CustomerRequest().firstName(id);
 
-            fail("Should have thrown NotFoundException");
-        } catch (CamelExecutionException e) {
-            assertIsInstanceOf(NotFoundException.class, e.getCause().getCause());
-        }
+        Exception ex = assertThrows(CamelExecutionException.class,
+                () -> requestBodyAndHeaders("direct://UPDATE_IN_BODY", customerRequest, headers));
+
+        assertIsInstanceOf(NotFoundException.class, ex.getCause().getCause());
     }
 
     @Test
     public void testSearchUnknownCustomer() throws Exception {
-        try {
-            requestBody("direct://FIND_IN_BODY", "unknown-" + UUID.randomUUID().toString());
-            fail("Should have thrown NotFoundException");
-        } catch (CamelExecutionException e) {
-            assertIsInstanceOf(NotFoundException.class, e.getCause().getCause());
-        }
+        String uuid = "unknown-" + UUID.randomUUID().toString();
+
+        Exception ex = assertThrows(CamelExecutionException.class,
+                () -> requestBody("direct://FIND_IN_BODY", uuid));
+
+        assertIsInstanceOf(NotFoundException.class, ex.getCause().getCause());
     }
 
     @Test
diff --git a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/CamelDestinationTest.java b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/CamelDestinationTest.java
index 8b60ac2..a5428e0 100644
--- a/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/CamelDestinationTest.java
+++ b/components/camel-cxf-transport/src/test/java/org/apache/camel/component/cxf/transport/CamelDestinationTest.java
@@ -53,6 +53,7 @@ import org.slf4j.LoggerFactory;
 
 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;
 import static org.mockito.Mockito.doThrow;
@@ -281,19 +282,18 @@ public class CamelDestinationTest extends CamelTransportTestSupport {
     }
 
     @Test
-    public void testCAMEL4073() throws Exception {
-        try {
-            Endpoint.publish("camel://foo", new Person() {
-                public void getPerson(Holder<String> personId, Holder<String> ssn, Holder<String> name)
-                        throws UnknownPersonFault {
-                }
-            });
-            fail("Should throw and Exception");
-        } catch (WebServiceException ex) {
-            Throwable c = ex.getCause();
-            assertNotNull(c);
-            assertTrue(c instanceof NoSuchEndpointException);
-        }
+    public void testCAMEL4073() {
+        Person person = new Person() {
+            public void getPerson(Holder<String> personId, Holder<String> ssn, Holder<String> name)
+                    throws UnknownPersonFault {
+            }
+        };
+
+        Exception ex = assertThrows(WebServiceException.class, () -> Endpoint.publish("camel://foo", person));
+
+        Throwable c = ex.getCause();
+        assertNotNull(c);
+        assertTrue(c instanceof NoSuchEndpointException);
     }
 
 }
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java
index 9899dfa..ff3f507 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java
@@ -35,6 +35,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 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;
 
@@ -110,18 +111,16 @@ public abstract class AbstractCXFGreeterRouterTest extends CamelTestSupport {
     }
 
     @Test
-    public void testRoutingSOAPFault() throws Exception {
-        try {
-            template.sendBody("http://localhost:" + getPort2() + "/"
-                              + getClass().getSimpleName()
-                              + "/CamelContext/RouterPort/",
-                    testDocLitFaultBody);
-            fail("Should get an exception here.");
-        } catch (RuntimeCamelException exception) {
-            assertTrue(exception.getCause() instanceof HttpOperationFailedException, "It should get the response error");
-            assertEquals(500, ((HttpOperationFailedException) exception.getCause()).getStatusCode(),
-                    "Get a wrong response code");
-        }
+    public void testRoutingSOAPFault() {
+        String endpointUri = "http://localhost:" + getPort2() + "/" + getClass().getSimpleName()
+                             + "/CamelContext/RouterPort/";
+
+        Exception ex = assertThrows(RuntimeCamelException.class,
+                () -> template.sendBody(endpointUri, testDocLitFaultBody));
+
+        assertTrue(ex.getCause() instanceof HttpOperationFailedException, "It should get the response error");
+        assertEquals(500, ((HttpOperationFailedException) ex.getCause()).getStatusCode(),
+                "Get a wrong response code");
     }
 
     @Test
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfEndpointUtilsTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfEndpointUtilsTest.java
index fdc4d5d..c34d444 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfEndpointUtilsTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/CxfEndpointUtilsTest.java
@@ -31,8 +31,8 @@ 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.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 CxfEndpointUtilsTest {
     // set up the port name and service name
@@ -104,21 +104,17 @@ public class CxfEndpointUtilsTest {
     @Test
     public void testCheckServiceClassConsumer() throws Exception {
         CxfEndpoint endpoint = createEndpoint(getNoServiceClassURI());
-        try {
-            Consumer cxfConsumer = endpoint.createConsumer(new Processor() {
-
-                @Override
-                public void process(Exchange exchange) throws Exception {
-                    // noop
-                }
-
-            });
-            cxfConsumer.start();
-            fail("Should have thrown exception");
-        } catch (IllegalArgumentException exception) {
-            assertNotNull(exception, "Should get a CamelException here");
-            assertTrue(exception.getMessage().startsWith("serviceClass must be specified"));
-        }
+
+        Consumer cxfConsumer = endpoint.createConsumer(new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                // noop
+            }
+        });
+
+        Exception ex = assertThrows(IllegalArgumentException.class, () -> cxfConsumer.start());
+        assertNotNull(ex, "Should get a CamelException here");
+        assertTrue(ex.getMessage().startsWith("serviceClass must be specified"));
     }
 
 }
diff --git a/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DisruptorConcurrentConsumersNPEIssueTest.java b/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DisruptorConcurrentConsumersNPEIssueTest.java
index 9dcf8bd..3aaa4ee 100644
--- a/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DisruptorConcurrentConsumersNPEIssueTest.java
+++ b/components/camel-disruptor/src/test/java/org/apache/camel/component/disruptor/DisruptorConcurrentConsumersNPEIssueTest.java
@@ -19,11 +19,12 @@ package org.apache.camel.component.disruptor;
 import org.apache.camel.FailedToStartRouteException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.RouteController;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.Test;
 
 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 DisruptorConcurrentConsumersNPEIssueTest extends CamelTestSupport {
     @Test
@@ -34,16 +35,14 @@ public class DisruptorConcurrentConsumersNPEIssueTest extends CamelTestSupport {
         template.sendBody("disruptor:foo", "Hello World");
 
         assertMockEndpointsSatisfied();
+        RouteController routeController = context.getRouteController();
 
-        try {
-            context.getRouteController().startRoute("first");
-            fail("Should have thrown exception");
-        } catch (FailedToStartRouteException e) {
-            assertEquals(
-                    "Failed to start route first because of Multiple consumers for the same endpoint is not allowed:"
-                         + " disruptor://foo?concurrentConsumers=5",
-                    e.getMessage());
-        }
+        Exception ex = assertThrows(FailedToStartRouteException.class,
+                () -> routeController.startRoute("first"));
+
+        assertEquals("Failed to start route first because of Multiple consumers for the same endpoint is not "
+                     + "allowed: disruptor://foo?concurrentConsumers=5",
+                ex.getMessage());
     }
 
     @Test
@@ -58,15 +57,14 @@ public class DisruptorConcurrentConsumersNPEIssueTest extends CamelTestSupport {
         // this should be okay
         context.getRouteController().startRoute("third");
 
-        try {
-            context.getRouteController().startRoute("first");
-            fail("Should have thrown exception");
-        } catch (FailedToStartRouteException e) {
-            assertEquals(
-                    "Failed to start route first because of Multiple consumers for the same endpoint is not allowed:"
-                         + " disruptor://foo?concurrentConsumers=5",
-                    e.getMessage());
-        }
+        RouteController routeController = context.getRouteController();
+
+        Exception ex = assertThrows(FailedToStartRouteException.class,
+                () -> routeController.startRoute("first"));
+
+        assertEquals("Failed to start route first because of Multiple consumers for the same endpoint is not allowed:"
+                     + " disruptor://foo?concurrentConsumers=5",
+                ex.getMessage());
     }
 
     @Override
diff --git a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
index f770c9a..1b5caad 100644
--- a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
+++ b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
@@ -24,8 +24,8 @@ 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.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 public class CustomMapperTest {
 
@@ -64,12 +64,9 @@ public class CustomMapperTest {
     @Test
     void mapCustomInvalidOperation() {
         customMapper.setParameter(MapperWithTwoMethods.class.getName() + ",convertToB");
-        try {
-            customMapper.mapCustom(new B(), B.class);
-            fail("Invalid operation should result in exception");
-        } catch (RuntimeException ex) {
-            assertTrue(ex.getCause() instanceof NoSuchMethodException);
-        }
+        B b = new B();
+        Exception ex = assertThrows(RuntimeException.class, () -> customMapper.mapCustom(b, B.class));
+        assertTrue(ex.getCause() instanceof NoSuchMethodException);
     }
 
     @Test
diff --git a/components/camel-elytron/src/test/java/org/apache/camel/component/elytron/ElytronBearerTokenTest.java b/components/camel-elytron/src/test/java/org/apache/camel/component/elytron/ElytronBearerTokenTest.java
index 00aa36d..a4241d9 100644
--- a/components/camel-elytron/src/test/java/org/apache/camel/component/elytron/ElytronBearerTokenTest.java
+++ b/components/camel-elytron/src/test/java/org/apache/camel/component/elytron/ElytronBearerTokenTest.java
@@ -43,6 +43,7 @@ import org.wildfly.security.http.bearer.WildFlyElytronHttpBearerProvider;
 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.fail;
 
 public class ElytronBearerTokenTest extends BaseElytronTest {
@@ -82,19 +83,16 @@ public class ElytronBearerTokenTest extends BaseElytronTest {
 
     @Test
     public void testBearerTokenBadRole() throws Exception {
-        try {
-            String response = template.requestBodyAndHeader("undertow:http://localhost:{{port}}/myapp",
-                    "empty body",
-                    Headers.AUTHORIZATION.toString(),
-                    "Bearer " + createToken("alice", "guest", new Date(new Date().getTime() + 10000),
-                            getKeyPair().getPrivate()),
-                    String.class);
-            fail("Should throw exception");
-
-        } catch (CamelExecutionException e) {
-            HttpOperationFailedException he = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
-            assertEquals(403, he.getStatusCode());
-        }
+        Date date = new Date(new Date().getTime() + 10000);
+        String authHeader = Headers.AUTHORIZATION.toString();
+        String authHeaderValue = "Bearer " + createToken("alice", "guest", date, getKeyPair().getPrivate());
+
+        Exception ex = assertThrows(CamelExecutionException.class,
+                () -> template.requestBodyAndHeader("undertow:http://localhost:{{port}}/myapp",
+                        "empty body", authHeader, authHeaderValue, String.class));
+
+        HttpOperationFailedException he = assertIsInstanceOf(HttpOperationFailedException.class, ex.getCause());
+        assertEquals(403, he.getStatusCode());
     }
 
     @Override