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 812660e0bb009d19322860a77d4893df63065c66 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Sat Jan 24 19:21:32 2026 +0000 CAMEL-21196: modernize exception-based assertions in camel-jmx Replace try-catch-fail patterns with assertThrows in: - JMXEndpointTest (9 occurrences) - JMXRobustRemoteConnectionTest (1 occurrence) - JMXComponentTest (3 occurrences) --- .../camel/component/jmx/JMXComponentTest.java | 32 ++++---- .../camel/component/jmx/JMXEndpointTest.java | 90 +++++++++------------- .../jmx/JMXRobustRemoteConnectionTest.java | 14 ++-- 3 files changed, 54 insertions(+), 82 deletions(-) diff --git a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXComponentTest.java b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXComponentTest.java index d158f2a81bf2..55b5d16a207e 100644 --- a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXComponentTest.java +++ b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXComponentTest.java @@ -25,8 +25,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; /** * Test behavior in the component for initializing an endpoint. Not much here beyond checking that the code for the @@ -63,31 +63,25 @@ public class JMXComponentTest { @Test public void withObjectNameAndObjectProperties() { - try { - context.getEndpoint("jmx:platform?objectDomain=FooDomain&objectName=theObjectName&key.propOne=prop1"); - fail("expected exception"); - } catch (ResolveEndpointFailedException e) { - assertTrue(e.getCause() instanceof IllegalArgumentException); - } + ResolveEndpointFailedException e = assertThrows(ResolveEndpointFailedException.class, + () -> context.getEndpoint("jmx:platform?objectDomain=FooDomain&objectName=theObjectName&key.propOne=prop1"), + "expected exception"); + assertTrue(e.getCause() instanceof IllegalArgumentException); } @Test public void withoutDomain() { - try { - context.getEndpoint("jmx:platform?objectName=theObjectName"); - fail("missing domain should have caused failure"); - } catch (ResolveEndpointFailedException e) { - assertTrue(e.getCause() instanceof IllegalArgumentException); - } + ResolveEndpointFailedException e = assertThrows(ResolveEndpointFailedException.class, + () -> context.getEndpoint("jmx:platform?objectName=theObjectName"), + "missing domain should have caused failure"); + assertTrue(e.getCause() instanceof IllegalArgumentException); } @Test public void withoutObjectNameAndObjectProperties() { - try { - context.getEndpoint("jmx:platform?objectDomain=theObjectDomain"); - fail("missing name should have caused failure"); - } catch (ResolveEndpointFailedException e) { - assertTrue(e.getCause() instanceof IllegalArgumentException); - } + ResolveEndpointFailedException e = assertThrows(ResolveEndpointFailedException.class, + () -> context.getEndpoint("jmx:platform?objectDomain=theObjectDomain"), + "missing name should have caused failure"); + assertTrue(e.getCause() instanceof IllegalArgumentException); } } diff --git a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXEndpointTest.java b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXEndpointTest.java index a03e873b9be8..d00a2cd7bb51 100644 --- a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXEndpointTest.java +++ b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXEndpointTest.java @@ -28,8 +28,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.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; /** * Tests for the endpoint. Most of the params in the endpoint are set via the endpoint helper so there's no much beyond @@ -48,13 +48,9 @@ public class JMXEndpointTest { public void setObjectNameThrowsWhenObjectPropertiesIsSet() { JMXEndpoint ep = new JMXEndpoint("urn:ignored", new JMXComponent()); ep.setObjectProperties(new Hashtable<String, String>()); - try { - // should fault since objectName is mutex with objectProperties - ep.setObjectName("foo"); - fail("expected exception"); - } catch (IllegalArgumentException e) { - // expected - } + assertThrows(IllegalArgumentException.class, + () -> ep.setObjectName("foo"), + "expected exception"); } @Test @@ -120,11 +116,9 @@ public class JMXEndpointTest { @Test public void noProducer() throws Exception { JMXEndpoint ep = context.getEndpoint("jmx:platform?objectDomain=FooDomain&key.name=theObjectName", JMXEndpoint.class); - try { - ep.createProducer(); - fail("producer pattern is not supported"); - } catch (UnsupportedOperationException e) { - } + assertThrows(UnsupportedOperationException.class, + () -> ep.createProducer(), + "producer pattern is not supported"); } @Test @@ -139,12 +133,10 @@ public class JMXEndpointTest { public void noObservedAttribute() throws Exception { JMXEndpoint ep = context.getEndpoint("jmx:platform?objectDomain=FooDomain&objectName=theObjectName&monitorType=string", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_OBSERVED_ATTRIBUTE, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_OBSERVED_ATTRIBUTE, e.getMessage()); } @Test @@ -152,12 +144,10 @@ public class JMXEndpointTest { JMXEndpoint ep = context.getEndpoint( "jmx:platform?objectDomain=FooDomain&objectName=theObjectName&monitorType=string&observedAttribute=foo", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_STRING_TO_COMPARE, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_STRING_TO_COMPARE, e.getMessage()); } @Test @@ -165,12 +155,10 @@ public class JMXEndpointTest { JMXEndpoint ep = context.getEndpoint( "jmx:platform?objectDomain=FooDomain&objectName=theObjectName&monitorType=string&observedAttribute=foo&stringToCompare=foo", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_STRING_NOTIFY, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_STRING_NOTIFY, e.getMessage()); } @Test @@ -178,12 +166,10 @@ public class JMXEndpointTest { JMXEndpoint ep = context.getEndpoint( "jmx:platform?objectDomain=FooDomain&objectName=theObjectName&monitorType=gauge&observedAttribute=foo", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_GAUGE_NOTIFY, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_GAUGE_NOTIFY, e.getMessage()); } @Test @@ -191,12 +177,10 @@ public class JMXEndpointTest { JMXEndpoint ep = context.getEndpoint( "jmx:platform?objectDomain=FooDomain&objectName=theObjectName&monitorType=gauge&observedAttribute=foo&thresholdLow=100¬ifyHigh=true", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_THRESHOLD_HIGH, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_THRESHOLD_HIGH, e.getMessage()); } @Test @@ -204,12 +188,10 @@ public class JMXEndpointTest { JMXEndpoint ep = context.getEndpoint( "jmx:platform?objectDomain=FooDomain&objectName=theObjectName&monitorType=gauge&observedAttribute=foo&thresholdHigh=100¬ifyHigh=true", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_THRESHOLD_LOW, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_THRESHOLD_LOW, e.getMessage()); } @Test @@ -217,11 +199,9 @@ public class JMXEndpointTest { JMXEndpoint ep = context.getEndpoint( "jmx:service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi?objectDomain=FooDomain&key.name=theObjectName&monitorType=gauge", JMXEndpoint.class); - try { - ep.createConsumer(null); - fail("expected exception"); - } catch (IllegalArgumentException e) { - assertEquals(JMXEndpoint.ERR_PLATFORM_SERVER, e.getMessage()); - } + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> ep.createConsumer(null), + "expected exception"); + assertEquals(JMXEndpoint.ERR_PLATFORM_SERVER, e.getMessage()); } } diff --git a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXRobustRemoteConnectionTest.java b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXRobustRemoteConnectionTest.java index 4ed29590f01d..860121a530ee 100644 --- a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXRobustRemoteConnectionTest.java +++ b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXRobustRemoteConnectionTest.java @@ -32,8 +32,8 @@ import org.junit.jupiter.api.BeforeEach; 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; /** * Test to verify: @@ -91,13 +91,11 @@ public class JMXRobustRemoteConnectionTest extends SimpleBeanFixture { public void testRobustConnection() throws Exception { // the JMX service should not be started - try { - getSimpleMXBean().touch(); - fail("The mxbean should not be available."); - } catch (Exception e) { - assertTrue(e instanceof java.lang.IllegalArgumentException); - assertEquals("Null connection", e.getMessage()); - } + Exception e = assertThrows(Exception.class, + () -> getSimpleMXBean().touch(), + "The mxbean should not be available."); + assertTrue(e instanceof java.lang.IllegalArgumentException); + assertEquals("Null connection", e.getMessage()); // start the server; the JMX consumer should connect and start; the mock should receive a notification initServer();
