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


The following commit(s) were added to refs/heads/main by this push:
     new 12150eb1008 CAMEL-21196: modernized assertions in camel-core.component
12150eb1008 is described below

commit 12150eb100834fb94a459e5bba43b6d7520f5210
Author: Nikita Konovalov <[email protected]>
AuthorDate: Wed Sep 11 16:42:26 2024 +0200

    CAMEL-21196: modernized assertions in camel-core.component
---
 .../bean/BeanExplicitMethodAmbiguousTest.java      | 15 ++++----
 .../camel/component/bean/BeanInvokeAsyncTest.java  | 22 ++++++------
 .../component/bean/BeanOverloadedMethodTest.java   | 41 ++++++++++------------
 .../camel/component/bean/MyServiceProxyTest.java   | 13 ++++---
 .../bean/ProxyReturnFutureExceptionTest.java       | 16 ++++-----
 .../component/log/LogCustomFormatterTest.java      | 11 +++---
 .../component/mock/MockEndpointTimeClauseTest.java | 12 +++----
 .../PropertiesComponentRegistryTest.java           | 16 ++++-----
 .../camel/component/xslt/XsltOutputFileTest.java   | 15 ++++----
 .../component/xslt/XsltRouteXsltWithErrorTest.java | 20 +++++------
 .../xslt/XsltTransformingExceptionTest.java        | 16 +++++----
 11 files changed, 94 insertions(+), 103 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
index c861d26f29d..19d2abd8fee 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
@@ -25,7 +25,7 @@ import org.apache.camel.spi.Registry;
 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 BeanExplicitMethodAmbiguousTest extends ContextTestSupport {
 
@@ -38,13 +38,12 @@ public class BeanExplicitMethodAmbiguousTest extends 
ContextTestSupport {
 
     @Test
     public void testBeanExplicitMethodAmbiguous() {
-        try {
-            template.requestBody("direct:hello", "Camel");
-            fail("Should thrown an exception");
-        } catch (Exception e) {
-            AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
-            assertEquals(2, cause.getMethods().size());
-        }
+        Exception e = assertThrows(Exception.class,
+                () -> template.requestBody("direct:hello", "Camel"),
+                "Should thrown an exception");
+
+        AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
+        assertEquals(2, cause.getMethods().size());
     }
 
     @Test
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvokeAsyncTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvokeAsyncTest.java
index d3a6b67eb12..162c72ac53a 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvokeAsyncTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvokeAsyncTest.java
@@ -65,17 +65,17 @@ public class BeanInvokeAsyncTest extends ContextTestSupport 
{
     }
 
     @Test
-    public void testThrowSomething() throws Exception {
-        try {
-            runTestSendBody(m -> m.expectedMessageCount(0), "SomeProblem", 
this::throwSomething);
-            fail("Exception expected");
-        } catch (ExecutionException e) {
-            boolean b1 = e.getCause() instanceof CamelExecutionException;
-            assertTrue(b1);
-            boolean b = e.getCause().getCause() instanceof 
IllegalStateException;
-            assertTrue(b);
-            assertEquals("SomeProblem", e.getCause().getCause().getMessage());
-        }
+    public void testThrowSomething() {
+
+        ExecutionException e = assertThrows(ExecutionException.class,
+                () -> runTestSendBody(m -> m.expectedMessageCount(0), 
"SomeProblem", this::throwSomething),
+                "Exception expected");
+
+        boolean b1 = e.getCause() instanceof CamelExecutionException;
+        assertTrue(b1);
+        boolean b = e.getCause().getCause() instanceof IllegalStateException;
+        assertTrue(b);
+        assertEquals("SomeProblem", e.getCause().getCause().getMessage());
     }
 
     private void runTestSendBody(String expectedBody, String sentBody, 
Function<String, String> processor) throws Exception {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
index ce8bc4f28bd..a248f5e19a5 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanOverloadedMethodTest.java
@@ -23,7 +23,7 @@ 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.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 /**
  *
@@ -156,13 +156,12 @@ public class BeanOverloadedMethodTest extends 
ContextTestSupport {
         });
         context.start();
 
-        try {
-            template.sendBodyAndHeader("direct:start", "Claus", "country", 
"Denmark");
-            fail("Should have thrown an exception");
-        } catch (CamelExecutionException e) {
-            AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
-            assertEquals(2, cause.getMethods().size());
-        }
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.sendBodyAndHeader("direct:start", "Claus", 
"country", "Denmark"),
+                "Should have thrown an exception");
+
+        AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
+        assertEquals(2, cause.getMethods().size());
     }
 
     @Test
@@ -176,13 +175,12 @@ public class BeanOverloadedMethodTest extends 
ContextTestSupport {
         });
         context.start();
 
-        try {
-            template.sendBodyAndHeader("direct:start", "Claus", "country", 
"Denmark");
-            fail("Should have thrown an exception");
-        } catch (CamelExecutionException e) {
-            AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
-            assertEquals(2, cause.getMethods().size());
-        }
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.sendBodyAndHeader("direct:start", "Claus", 
"country", "Denmark"),
+                "Should have thrown an exception");
+
+        AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
+        assertEquals(2, cause.getMethods().size());
     }
 
     @Test
@@ -196,13 +194,12 @@ public class BeanOverloadedMethodTest extends 
ContextTestSupport {
         });
         context.start();
 
-        try {
-            template.sendBodyAndHeader("direct:start", "Claus", "country", 
"Denmark");
-            fail("Should have thrown an exception");
-        } catch (CamelExecutionException e) {
-            AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
-            assertEquals(2, cause.getMethods().size());
-        }
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.sendBodyAndHeader("direct:start", "Claus", 
"country", "Denmark"),
+                "Should have thrown an exception");
+
+        AmbiguousMethodCallException cause = 
assertIsInstanceOf(AmbiguousMethodCallException.class, e.getCause());
+        assertEquals(2, cause.getMethods().size());
     }
 
     @Test
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/MyServiceProxyTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/MyServiceProxyTest.java
index da163335a9c..9a71770a852 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/MyServiceProxyTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/MyServiceProxyTest.java
@@ -25,7 +25,6 @@ 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.fail;
 
 public class MyServiceProxyTest extends ContextTestSupport {
 
@@ -39,12 +38,12 @@ public class MyServiceProxyTest extends ContextTestSupport {
     @Test
     public void testKaboom() throws Exception {
         MyService myService = 
ProxyHelper.createProxy(context.getEndpoint("direct:start"), MyService.class);
-        try {
-            myService.method("Kaboom");
-            fail("Should have thrown exception");
-        } catch (IllegalArgumentException e) {
-            assertEquals("Damn", e.getMessage());
-        }
+
+        IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class,
+                () -> myService.method("Kaboom"),
+                "Should have thrown exception");
+
+        assertEquals("Damn", e.getMessage());
     }
 
     @Test
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/ProxyReturnFutureExceptionTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/ProxyReturnFutureExceptionTest.java
index b0632a7289c..08a87893559 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/bean/ProxyReturnFutureExceptionTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/bean/ProxyReturnFutureExceptionTest.java
@@ -25,7 +25,7 @@ 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.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class ProxyReturnFutureExceptionTest extends ContextTestSupport {
 
@@ -37,13 +37,13 @@ public class ProxyReturnFutureExceptionTest extends 
ContextTestSupport {
         log.info("Got future");
 
         log.info("Waiting for future to be done ...");
-        try {
-            assertEquals("Four", future.get(5, TimeUnit.SECONDS));
-            fail("Should have thrown exception");
-        } catch (ExecutionException e) {
-            IllegalArgumentException cause = 
assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("Forced", cause.getMessage());
-        }
+
+        ExecutionException e = assertThrows(ExecutionException.class,
+                () -> assertEquals("Four", future.get(5, TimeUnit.SECONDS)),
+                "Should have thrown exception");
+
+        IllegalArgumentException cause = 
assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertEquals("Forced", cause.getMessage());
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
index 14f4d95c3b8..4eb621cd3e8 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
@@ -99,14 +99,13 @@ public class LogCustomFormatterTest extends 
ContextTestSupport {
         context.start();
 
         // unknown parameter
-        try {
+        Exception e = assertThrows(Exception.class, () -> {
             String endpointUri2 = "log:" + 
LogCustomFormatterTest.class.getCanonicalName() + "?prefix=foo&bar=no";
             template.requestBody(endpointUri2, "Hello World");
-            fail("Should have thrown exception");
-        } catch (Exception e) {
-            ResolveEndpointFailedException cause = 
assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
-            assertTrue(cause.getMessage().endsWith("Unknown 
parameters=[{bar=no}]"));
-        }
+        }, "Should have thrown exception");
+
+        ResolveEndpointFailedException cause = 
assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+        assertTrue(cause.getMessage().endsWith("Unknown 
parameters=[{bar=no}]"));
     }
 
     @Test
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTimeClauseTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTimeClauseTest.java
index bc4b60bd322..9c508111d37 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTimeClauseTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTimeClauseTest.java
@@ -29,7 +29,7 @@ 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.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class MockEndpointTimeClauseTest extends ContextTestSupport {
 
@@ -92,12 +92,10 @@ public class MockEndpointTimeClauseTest extends 
ContextTestSupport {
             }
         });
 
-        try {
-            mock.assertIsSatisfied();
-            fail("Should have thrown an exception");
-        } catch (AssertionError e) {
-            assertEquals("mock://result Received message count. Expected: <1> 
but was: <2>", e.getMessage());
-        }
+        AssertionError e = assertThrows(AssertionError.class, 
mock::assertIsSatisfied,
+                "Should have thrown an exception");
+
+        assertEquals("mock://result Received message count. Expected: <1> but 
was: <2>", e.getMessage());
 
         executor.shutdownNow();
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
index d6ac4f48cae..dadd7709e63 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
@@ -29,7 +29,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.fail;
 
 public class PropertiesComponentRegistryTest extends ContextTestSupport {
 
@@ -75,14 +74,13 @@ public class PropertiesComponentRegistryTest extends 
ContextTestSupport {
         assertSame(foo, context.getRegistry().lookupByName("{{bean.foo}}"));
         assertSame(bar, context.getRegistry().lookupByName("{{bean.bar}}"));
 
-        try {
-            context.getRegistry().lookupByName("{{bean.unknown}}");
-            fail("Should have thrown exception");
-        } catch (RuntimeCamelException e) {
-            IllegalArgumentException cause = 
assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("Property with key [bean.unknown] not found in 
properties from text: {{bean.unknown}}",
-                    cause.getMessage());
-        }
+        RuntimeCamelException e = assertThrows(RuntimeCamelException.class,
+                () -> context.getRegistry().lookupByName("{{bean.unknown}}"),
+                "Should have thrown exception");
+
+        IllegalArgumentException cause = 
assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertEquals("Property with key [bean.unknown] not found in properties 
from text: {{bean.unknown}}",
+                cause.getMessage());
     }
 
     @Test
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java
index 95522cc7e83..2eb45c5d8f1 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltOutputFileTest.java
@@ -27,7 +27,7 @@ import org.apache.camel.component.mock.MockEndpoint;
 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;
 
 /**
  *
@@ -49,13 +49,12 @@ public class XsltOutputFileTest extends ContextTestSupport {
 
     @Test
     public void testXsltOutputFileMissingHeader() {
-        try {
-            template.sendBody("direct:start", "<hello>world!</hello>");
-            fail("Should thrown exception");
-        } catch (CamelExecutionException e) {
-            NoSuchHeaderException nshe = 
assertIsInstanceOf(NoSuchHeaderException.class, e.getCause());
-            assertEquals(Exchange.XSLT_FILE_NAME, nshe.getHeaderName());
-        }
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.sendBody("direct:start", 
"<hello>world!</hello>"),
+                "Should thrown exception");
+
+        NoSuchHeaderException nshe = 
assertIsInstanceOf(NoSuchHeaderException.class, e.getCause());
+        assertEquals(Exchange.XSLT_FILE_NAME, nshe.getHeaderName());
     }
 
     @Override
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
index a62bfaa5514..00636c282d4 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltRouteXsltWithErrorTest.java
@@ -40,16 +40,16 @@ public class XsltRouteXsltWithErrorTest extends 
ContextTestSupport {
                 
from("direct:start").to("xslt:org/apache/camel/component/xslt/transform-with-error.xsl");
             }
         });
-        try {
-            context.start();
-            fail("Should have thrown exception");
-        } catch (Exception e) {
-            TransformerConfigurationException cause = 
ObjectHelper.getException(TransformerConfigurationException.class, e);
-            assertNotNull(cause);
-            // not sure if XSLT errors may be i18n and not english always so
-            // just check for the spelling mistake of select -> slect
-            assertTrue(cause.getMessage().contains("slect"));
-        }
+
+        Exception e = assertThrows(Exception.class,
+                () -> context.start(),
+                "Should have thrown exception");
+
+        TransformerConfigurationException cause = 
ObjectHelper.getException(TransformerConfigurationException.class, e);
+        assertNotNull(cause);
+        // not sure if XSLT errors may be i18n and not english always so
+        // just check for the spelling mistake of select -> slect
+        assertTrue(cause.getMessage().contains("slect"));
     }
 
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java
index 145c99c6ecf..74b9919f74c 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltTransformingExceptionTest.java
@@ -22,6 +22,7 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
@@ -33,13 +34,14 @@ public class XsltTransformingExceptionTest extends 
ContextTestSupport {
     public void testXsltException() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(0);
-        try {
-            template.sendBody("direct:start", BAD_XML_STRING);
-            fail("Except a camel Execution exception here");
-        } catch (CamelExecutionException ex) {
-            boolean b = ex.getCause() instanceof 
javax.xml.transform.TransformerException;
-            assertTrue(b);
-        }
+
+        CamelExecutionException ex = 
assertThrows(CamelExecutionException.class,
+                () -> template.sendBody("direct:start", BAD_XML_STRING),
+                "Except a camel Execution exception here");
+
+        boolean b = ex.getCause() instanceof 
javax.xml.transform.TransformerException;
+        assertTrue(b);
+
         // we should not get any message from the result endpoint
         assertMockEndpointsSatisfied();
     }

Reply via email to