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 720c14a77212f1b0de0d7eff6ffd7724624352cc Author: Otavio R. Piske <[email protected]> AuthorDate: Sat Jan 24 10:42:22 2026 +0100 CAMEL-21196: modernize exception-based assertions in camel-mail --- .../component/mail/InvalidConfigurationTest.java | 20 +++++++------------- .../camel/component/mail/MailNoRecipientsTest.java | 15 +++++++-------- .../mail/MailProducerUnsupportedCharsetTest.java | 16 ++++++++-------- .../security/SslContextParametersMailRouteTest.java | 16 +++++++--------- 4 files changed, 29 insertions(+), 38 deletions(-) diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/InvalidConfigurationTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/InvalidConfigurationTest.java index 88b290179178..bc9eacff66df 100644 --- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/InvalidConfigurationTest.java +++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/InvalidConfigurationTest.java @@ -21,7 +21,7 @@ import org.apache.camel.PollingConsumer; 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 various invalid configurations etc. @@ -32,24 +32,18 @@ public class InvalidConfigurationTest extends CamelTestSupport { public void testSMTPCanNotBeUsedForConsumingMails() throws Exception { Endpoint endpoint = context.getEndpoint("smtp://localhost?username=james"); PollingConsumer consumer = endpoint.createPollingConsumer(); - try { - consumer.start(); - fail("Should have thrown NoSuchProviderException as smtp protocol cannot be used for consuming mails"); - } catch (IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, + () -> consumer.start(), + "Should have thrown NoSuchProviderException as smtp protocol cannot be used for consuming mails"); } @Test public void testSMTPSCanNotBeUsedForConsumingMails() throws Exception { Endpoint endpoint = context.getEndpoint("smtps://localhost?username=james"); PollingConsumer consumer = endpoint.createPollingConsumer(); - try { - consumer.start(); - fail("Should have thrown NoSuchProviderException as smtp protocol cannot be used for consuming mails"); - } catch (IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, + () -> consumer.start(), + "Should have thrown NoSuchProviderException as smtp protocol cannot be used for consuming mails"); } } diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailNoRecipientsTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailNoRecipientsTest.java index 8187b39a1085..cbcb0acfe4cd 100644 --- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailNoRecipientsTest.java +++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailNoRecipientsTest.java @@ -23,7 +23,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 no recipients @@ -32,13 +32,12 @@ public class MailNoRecipientsTest extends CamelTestSupport { @Test public void testMailNoRecipients() { - try { - template.sendBody("direct:a", "Hello World"); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); - assertEquals("The mail message does not have any recipients set.", iae.getMessage()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBody("direct:a", "Hello World"), + "Should have thrown exception"); + + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + assertEquals("The mail message does not have any recipients set.", iae.getMessage()); } @Override diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java index d08e2ae10a4e..868a9bccb3bb 100644 --- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java +++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java @@ -29,7 +29,7 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class MailProducerUnsupportedCharsetTest extends CamelTestSupport { private static final MailboxUser jones = Mailbox.getOrCreateUser("jones", "secret"); @@ -94,13 +94,13 @@ public class MailProducerUnsupportedCharsetTest extends CamelTestSupport { headers.clear(); headers.put("To", "jones@localhost"); headers.put("Content-Type", "text/plain; charset=XXX"); - try { - template.sendBodyAndHeaders(jones.uriPrefix(Protocol.smtp) + "&ignoreUnsupportedCharset=false", "Bye World", - headers); - fail("Should have thrown an exception"); - } catch (RuntimeCamelException e) { - assertIsInstanceOf(UnsupportedEncodingException.class, e.getCause()); - } + + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.sendBodyAndHeaders(jones.uriPrefix(Protocol.smtp) + "&ignoreUnsupportedCharset=false", + "Bye World", + headers), + "Should have thrown an exception"); + assertIsInstanceOf(UnsupportedEncodingException.class, e.getCause()); mock.assertIsSatisfied(); } diff --git a/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java b/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java index 6c50da6eced3..b012743777cd 100644 --- a/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java +++ b/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java @@ -32,8 +32,8 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Disabled; 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; /** * Test of integration between the mail component and JSSE Configuration Utility. This test does not easily automate. @@ -99,14 +99,12 @@ public class SslContextParametersMailRouteTest extends CamelTestSupport { headers.put(MailConstants.MAIL_REPLY_TO, email); headers.put("Subject", "SSL/TLS Test"); - try { - template.sendBodyAndHeaders("direct:in", "Test Email Body", headers); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - assertTrue(e.getCause().getCause() instanceof SSLHandshakeException); - assertTrue(e.getCause().getCause().getMessage() - .contains("unable to find valid certification path to requested target")); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBodyAndHeaders("direct:in", "Test Email Body", headers), + "Should have thrown exception"); + assertTrue(e.getCause().getCause() instanceof SSLHandshakeException); + assertTrue(e.getCause().getCause().getMessage() + .contains("unable to find valid certification path to requested target")); } /**
