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 d259f6390e69089da42d01dfe8970fc47b743747 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Sat Jan 24 18:13:34 2026 +0000 CAMEL-21196: modernize exception-based assertions in camel-avro Convert try-catch-fail pattern to JUnit 5 assertThrows() in AvroMarshalAndUnmarshalTest. --- .../avro/AvroMarshalAndUnmarshalTest.java | 9 +- .../bean/validator/BeanValidatorRouteTest.java | 170 ++++++++++----------- 2 files changed, 82 insertions(+), 97 deletions(-) diff --git a/components/camel-avro/src/test/java/org/apache/camel/dataformat/avro/AvroMarshalAndUnmarshalTest.java b/components/camel-avro/src/test/java/org/apache/camel/dataformat/avro/AvroMarshalAndUnmarshalTest.java index ea8b621d569c..8bf1ae160d14 100644 --- a/components/camel-avro/src/test/java/org/apache/camel/dataformat/avro/AvroMarshalAndUnmarshalTest.java +++ b/components/camel-avro/src/test/java/org/apache/camel/dataformat/avro/AvroMarshalAndUnmarshalTest.java @@ -25,7 +25,7 @@ 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 AvroMarshalAndUnmarshalTest extends CamelTestSupport { @@ -46,7 +46,7 @@ public class AvroMarshalAndUnmarshalTest extends CamelTestSupport { @Test public void testMarshalAndUnmarshalWithDSL3() { - try { + assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { @Override public void configure() { @@ -54,10 +54,7 @@ public class AvroMarshalAndUnmarshalTest extends CamelTestSupport { .to("mock:reverse"); } }); - fail("Expect the exception here"); - } catch (Exception ex) { - // expected - } + }, "Expect the exception here"); } private void marshalAndUnmarshal(String inURI, String outURI) throws Exception { diff --git a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java index aacc4b9ef798..e9068fe16a1b 100644 --- a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java +++ b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorRouteTest.java @@ -39,7 +39,7 @@ 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.assertNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.condition.OS.AIX; @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -90,22 +90,20 @@ class BeanValidatorRouteTest extends CamelTestSupport { final String url = "bean-validator://x"; - try { + CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> { template.requestBody(url, cars); - fail("should throw exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(BeanValidationException.class, e.getCause()); - - BeanValidationException exception = (BeanValidationException) e.getCause(); - Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); - - assertEquals(numberOfViolations, constraintViolations.size()); - constraintViolations.forEach(cv -> { - assertEquals("licensePlate", cv.getPropertyPath().toString()); - assertNull(cv.getInvalidValue()); - assertEquals("must not be null", cv.getMessage()); - }); - } + }); + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertNull(cv.getInvalidValue()); + assertEquals("must not be null", cv.getMessage()); + }); setLicensePlates(cars, "D-A"); @@ -124,22 +122,20 @@ class BeanValidatorRouteTest extends CamelTestSupport { final String url = "bean-validator://x?group=jakarta.validation.groups.Default"; - try { + CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> { template.requestBody(url, cars); - fail("should throw exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(BeanValidationException.class, e.getCause()); - - BeanValidationException exception = (BeanValidationException) e.getCause(); - Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); - - assertEquals(numberOfViolations, constraintViolations.size()); - constraintViolations.forEach(cv -> { - assertEquals("licensePlate", cv.getPropertyPath().toString()); - assertNull(cv.getInvalidValue()); - assertEquals("must not be null", cv.getMessage()); - }); - } + }); + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertNull(cv.getInvalidValue()); + assertEquals("must not be null", cv.getMessage()); + }); setLicensePlates(cars, "D-A"); @@ -158,22 +154,20 @@ class BeanValidatorRouteTest extends CamelTestSupport { final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.OptionalChecks"; - try { + CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> { template.requestBody(url, cars); - fail("should throw exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(BeanValidationException.class, e.getCause()); - - BeanValidationException exception = (BeanValidationException) e.getCause(); - Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); - - assertEquals(numberOfViolations, constraintViolations.size()); - constraintViolations.forEach(cv -> { - assertEquals("licensePlate", cv.getPropertyPath().toString()); - assertEquals("D-A", cv.getInvalidValue()); - assertEquals("size must be between 5 and 14", cv.getMessage()); - }); - } + }); + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertEquals("D-A", cv.getInvalidValue()); + assertEquals("size must be between 5 and 14", cv.getMessage()); + }); setLicensePlates(cars, "DD-AB-123"); @@ -192,41 +186,37 @@ class BeanValidatorRouteTest extends CamelTestSupport { final String url = "bean-validator://x?group=org.apache.camel.component.bean.validator.OrderedChecks"; - try { + CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> { template.requestBody(url, cars); - fail("should throw exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(BeanValidationException.class, e.getCause()); - - BeanValidationException exception = (BeanValidationException) e.getCause(); - Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); - - assertEquals(numberOfViolations, constraintViolations.size()); - constraintViolations.forEach(cv -> { - assertEquals("manufacturer", cv.getPropertyPath().toString()); - assertNull(cv.getInvalidValue()); - assertEquals("must not be null", cv.getMessage()); - }); - } + }); + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("manufacturer", cv.getPropertyPath().toString()); + assertNull(cv.getInvalidValue()); + assertEquals("must not be null", cv.getMessage()); + }); setManufacturer(cars, "BMW"); - try { + CamelExecutionException e2 = assertThrows(CamelExecutionException.class, () -> { template.requestBody(url, cars); - fail("should throw exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(BeanValidationException.class, e.getCause()); - - BeanValidationException exception = (BeanValidationException) e.getCause(); - Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); - - assertEquals(numberOfViolations, constraintViolations.size()); - constraintViolations.forEach(cv -> { - assertEquals("licensePlate", cv.getPropertyPath().toString()); - assertEquals("D-A", cv.getInvalidValue()); - assertEquals("size must be between 5 and 14", cv.getMessage()); - }); - } + }); + assertIsInstanceOf(BeanValidationException.class, e2.getCause()); + + BeanValidationException exception2 = (BeanValidationException) e2.getCause(); + Set<ConstraintViolation<Object>> constraintViolations2 = exception2.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations2.size()); + constraintViolations2.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertEquals("D-A", cv.getInvalidValue()); + assertEquals("size must be between 5 and 14", cv.getMessage()); + }); setLicensePlates(cars, "DD-AB-123"); @@ -260,22 +250,20 @@ class BeanValidatorRouteTest extends CamelTestSupport { final String url = "bean-validator://x"; - try { + CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> { template.requestBody(url, cars); - fail("should throw exception"); - } catch (CamelExecutionException e) { - assertIsInstanceOf(BeanValidationException.class, e.getCause()); - - BeanValidationException exception = (BeanValidationException) e.getCause(); - Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); - - assertEquals(numberOfViolations, constraintViolations.size()); - constraintViolations.forEach(cv -> { - assertEquals("licensePlate", cv.getPropertyPath().toString()); - assertEquals("D-A", cv.getInvalidValue()); - assertEquals("size must be between 5 and 14", cv.getMessage()); - }); - } + }); + assertIsInstanceOf(BeanValidationException.class, e.getCause()); + + BeanValidationException exception = (BeanValidationException) e.getCause(); + Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations(); + + assertEquals(numberOfViolations, constraintViolations.size()); + constraintViolations.forEach(cv -> { + assertEquals("licensePlate", cv.getPropertyPath().toString()); + assertEquals("D-A", cv.getInvalidValue()); + assertEquals("size must be between 5 and 14", cv.getMessage()); + }); } Car createCar(String manufacturer, String licencePlate) {
