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 a082febfc9a1395f9c127814efd7d6774613cdf2 Author: Otavio R. Piske <[email protected]> AuthorDate: Sat Jan 24 10:40:53 2026 +0100 CAMEL-21196: modernize exception-based assertions in camel-jms --- .../camel/component/jms/JmsAllowNullBodyTest.java | 14 ++++++------ .../jms/JmsRouteTimeoutCheckerIntervalTest.java | 14 ++++++------ .../JmsRouteWithDefaultKeyFormatStrategyTest.java | 11 ++++------ .../AsyncJmsProducerExceptionInTXManualIT.java | 25 +++++++++++----------- .../spring/tx/JMSTXInOutPersistentQueueIT.java | 20 ++++++----------- .../ncd/NoClassDefFoundErrorWrapExceptionTest.java | 20 ++++++++--------- 6 files changed, 45 insertions(+), 59 deletions(-) diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java index a0eafc6dd317..d5a9424446ca 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsAllowNullBodyTest.java @@ -36,7 +36,7 @@ import org.junit.jupiter.params.provider.ValueSource; 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; /** * @@ -66,13 +66,11 @@ public class JmsAllowNullBodyTest extends AbstractJMSTest { @Test public void testNoAllowNullBody() { - try { - template.sendBodyAndHeader("activemq:queue:JmsAllowNullBodyTest?allowNullBody=false", null, "bar", 123); - fail("Should have thrown exception"); - } catch (CamelExecutionException e) { - JMSException cause = assertIsInstanceOf(JMSException.class, e.getCause().getCause()); - assertEquals("Cannot send message as message body is null, and option allowNullBody is false.", cause.getMessage()); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBodyAndHeader("activemq:queue:JmsAllowNullBodyTest?allowNullBody=false", null, "bar", 123), + "Should have thrown exception"); + JMSException cause = assertIsInstanceOf(JMSException.class, e.getCause().getCause()); + assertEquals("Cannot send message as message body is null, and option allowNullBody is false.", cause.getMessage()); } @Override diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTimeoutCheckerIntervalTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTimeoutCheckerIntervalTest.java index d5cd7ba61010..902da5e561c8 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTimeoutCheckerIntervalTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTimeoutCheckerIntervalTest.java @@ -32,8 +32,8 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.RegisterExtension; 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; /** * Unit test for testing request timeout with a InOut exchange. @@ -50,13 +50,11 @@ public class JmsRouteTimeoutCheckerIntervalTest extends AbstractJMSTest { @Test public void testTimeout() { - try { - // send a in-out with a timeout for 1 sec - template.requestBody("activemq:queue:JmsRouteTimeoutCheckerIntervalTest.slow?requestTimeout=1000", "Hello World"); - fail("Should have timed out with an exception"); - } catch (RuntimeCamelException e) { - assertTrue(e.getCause() instanceof ExchangeTimedOutException, "Should have timed out with an exception"); - } + RuntimeCamelException e = assertThrows(RuntimeCamelException.class, + () -> template.requestBody("activemq:queue:JmsRouteTimeoutCheckerIntervalTest.slow?requestTimeout=1000", + "Hello World"), + "Should have timed out with an exception"); + assertTrue(e.getCause() instanceof ExchangeTimedOutException, "Should have timed out with an exception"); } @Test diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteWithDefaultKeyFormatStrategyTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteWithDefaultKeyFormatStrategyTest.java index 5b26c93bf06e..7f3dce64e4f1 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteWithDefaultKeyFormatStrategyTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteWithDefaultKeyFormatStrategyTest.java @@ -32,7 +32,7 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class JmsRouteWithDefaultKeyFormatStrategyTest extends AbstractJMSTest { @@ -49,12 +49,9 @@ public class JmsRouteWithDefaultKeyFormatStrategyTest extends AbstractJMSTest { @Test public void testIllegalOption() { - try { - context.getEndpoint("activemq:queue:bar?jmsHeaderStrategy=xxx"); - fail("Should have thrown a ResolveEndpointFailedException"); - } catch (ResolveEndpointFailedException e) { - // expected - } + assertThrows(ResolveEndpointFailedException.class, + () -> context.getEndpoint("activemq:queue:bar?jmsHeaderStrategy=xxx"), + "Should have thrown a ResolveEndpointFailedException"); } @Test diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/issues/AsyncJmsProducerExceptionInTXManualIT.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/issues/AsyncJmsProducerExceptionInTXManualIT.java index 835b8aa15e3f..e5d22b0ef150 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/issues/AsyncJmsProducerExceptionInTXManualIT.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/issues/AsyncJmsProducerExceptionInTXManualIT.java @@ -27,7 +27,7 @@ import org.springframework.transaction.TransactionException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * @see <a href="https://issues.apache.org/jira/browse/CAMEL-4616">CAMEL-4616</a> @@ -72,19 +72,18 @@ class AsyncJmsProducerExceptionInTXManualIT extends SpringJMSBasic { @Test void testAsyncEndpointException() { - try { - template.sendBody("direct:transacted_start", null); - fail("transaction should fail, otherwise looks like CAMEL-4616 has been emerged!"); - } catch (CamelExecutionException e) { - Throwable cause = e.getCause(); - assertInstanceOf(TransactionException.class, cause); - while (cause.getCause() != null) { - cause = cause.getCause(); - } - assertEquals( - "Usage Manager Memory Limit reached on queue://AsyncJmsProducerExceptionInTXTest. See http://activemq.apache.org/producer-flow-control.html for more info", - cause.getMessage()); + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBody("direct:transacted_start", null), + "transaction should fail, otherwise looks like CAMEL-4616 has been emerged!"); + + Throwable cause = e.getCause(); + assertInstanceOf(TransactionException.class, cause); + while (cause.getCause() != null) { + cause = cause.getCause(); } + assertEquals( + "Usage Manager Memory Limit reached on queue://AsyncJmsProducerExceptionInTXTest. See http://activemq.apache.org/producer-flow-control.html for more info", + cause.getMessage()); // non-transacted not fail assertion template.sendBody("direct:non_transacted_start", null); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/tx/JMSTXInOutPersistentQueueIT.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/tx/JMSTXInOutPersistentQueueIT.java index b631e1a76fdc..bf69c08bda6c 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/tx/JMSTXInOutPersistentQueueIT.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/integration/spring/tx/JMSTXInOutPersistentQueueIT.java @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test; import org.springframework.context.support.AbstractXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; @Tags({ @Tag("not-parallel"), @Tag("spring"), @Tag("tx") }) public class JMSTXInOutPersistentQueueIT extends AbstractSpringJMSITSupport { @@ -44,20 +44,14 @@ public class JMSTXInOutPersistentQueueIT extends AbstractSpringJMSITSupport { getMockEndpoint("mock:foo").expectedBodiesReceived("World", "World", "World"); getMockEndpoint("mock:reply").expectedBodiesReceived("Bye World", "Bye World", "Bye World"); - try { - template.sendBody("direct:start", "World"); - fail("Should thrown an exception"); - } catch (Exception e) { - // ignore - } + assertThrows(Exception.class, + () -> template.sendBody("direct:start", "World"), + "Should thrown an exception"); // let client re-try - try { - template.sendBody("direct:start", "World"); - fail("Should thrown an exception"); - } catch (Exception e) { - // ignore - } + assertThrows(Exception.class, + () -> template.sendBody("direct:start", "World"), + "Should thrown an exception"); // now we succeed template.sendBody("direct:start", "World"); diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/error/ncd/NoClassDefFoundErrorWrapExceptionTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/error/ncd/NoClassDefFoundErrorWrapExceptionTest.java index 2c8e8d8c122d..fcd44967c530 100644 --- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/error/ncd/NoClassDefFoundErrorWrapExceptionTest.java +++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/error/ncd/NoClassDefFoundErrorWrapExceptionTest.java @@ -29,8 +29,8 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +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 NoClassDefFoundErrorWrapExceptionTest extends AbstractJMSTest { @@ -43,15 +43,15 @@ public class NoClassDefFoundErrorWrapExceptionTest extends AbstractJMSTest { @Test public void testNoClassDef() { - try { - template.requestBody("activemq:NoClassDefFoundErrorWrapExceptionTest?transferException=true", "Hello World"); - fail("Should throw exception"); - } catch (Exception e) { - final String s = ExceptionHelper.stackTraceToString(e); - assertTrue(s.contains("java.lang.LinkageError")); - assertTrue(s.contains("Cannot do this")); - assertTrue(s.contains("org.apache.camel.component.jms.issues.error.ncd.ProcessorFail.process")); - } + Exception e = assertThrows(Exception.class, + () -> template.requestBody("activemq:NoClassDefFoundErrorWrapExceptionTest?transferException=true", + "Hello World"), + "Should throw exception"); + + final String s = ExceptionHelper.stackTraceToString(e); + assertTrue(s.contains("java.lang.LinkageError")); + assertTrue(s.contains("Cannot do this")); + assertTrue(s.contains("org.apache.camel.component.jms.issues.error.ncd.ProcessorFail.process")); } @Override
