From 07857067039dd5f3dbd9999d3e35a86e0432600d Mon Sep 17 00:00:00 2001 From: Nikita Konovalov Date: Thu, 26 Sep 2024 17:51:27 +0200 Subject: [PATCH] CAMEL-21196: modernized assertions in camel-core.model --- .../model/ProcessorTypeConfigurationTest.java | 14 ++--- .../RouteConfigurationOnCompletionTest.java | 18 ++---- ...esConfigurationBuilderIdOrPatternTest.java | 23 ++++---- .../RoutesConfigurationErrorHandlerTest.java | 11 ++-- .../StartingRoutesErrorReportedTest.java | 55 +++++++++---------- 5 files changed, 50 insertions(+), 71 deletions(-) diff --git a/core/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java b/core/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java index d6dc474f150a5..10ed72d5fbdf0 100644 --- a/core/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/model/ProcessorTypeConfigurationTest.java @@ -21,7 +21,7 @@ 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; /** * Unit test to verify end-user exceptions for miss configuration @@ -30,17 +30,15 @@ public class ProcessorTypeConfigurationTest extends ContextTestSupport { @Test public void testProcessorRefMissConfigured() { - try { + Exception e = assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { public void configure() { from("direct:in").process("hello"); } }); - fail("Should have thrown IllegalArgumentException"); - } catch (Exception e) { - assertEquals("No bean could be found in the registry for: hello of type: org.apache.camel.Processor", - e.getCause().getMessage()); - } - } + }, "Should have thrown IllegalArgumentException"); + assertEquals("No bean could be found in the registry for: hello of type: org.apache.camel.Processor", + e.getCause().getMessage()); + } } diff --git a/core/camel-core/src/test/java/org/apache/camel/model/RouteConfigurationOnCompletionTest.java b/core/camel-core/src/test/java/org/apache/camel/model/RouteConfigurationOnCompletionTest.java index 152fd49a7aca2..128e6f42f1e0f 100644 --- a/core/camel-core/src/test/java/org/apache/camel/model/RouteConfigurationOnCompletionTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/model/RouteConfigurationOnCompletionTest.java @@ -23,7 +23,7 @@ import org.apache.camel.processor.OnCompletionTest; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RouteConfigurationOnCompletionTest extends ContextTestSupport { @@ -58,12 +58,8 @@ public void testFail() throws Exception { getMockEndpoint("mock:fail").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(0); - try { - template.sendBody("direct:start", "Kaboom"); - fail("Should have thrown exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, () -> template.sendBody("direct:start", "Kaboom"), + "Should have thrown exception"); assertMockEndpointsSatisfied(); } @@ -75,12 +71,8 @@ public void testOkAndFail() throws Exception { getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); template.sendBody("direct:start", "Hello World"); - try { - template.sendBody("direct:start", "Kaboom"); - fail("Should throw exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, () -> template.sendBody("direct:start", "Kaboom"), + "Should throw exception"); assertMockEndpointsSatisfied(); } diff --git a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java index f7c8b3bcbe646..6ba8c11b723c9 100644 --- a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java @@ -31,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSupport { @@ -84,12 +84,9 @@ public void configuration() { getMockEndpoint("mock:error").expectedBodiesReceived("Bye World"); - try { - template.sendBody("direct:start", "Hello World"); - fail("Should throw exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, () -> template.sendBody("direct:start", "Hello World"), + "Should throw exception"); + template.sendBody("direct:start2", "Bye World"); assertMockEndpointsSatisfied(); @@ -242,12 +239,12 @@ public void configuration() { routeConfiguration("foo").onException(IllegalArgumentException.class).handled(true).to("mock:foo"); } }; - try { - context.addRoutesConfigurations(rcb); - fail("Should throw exception"); - } catch (IllegalArgumentException e) { - assertEquals("Route configuration already exists with id: foo", e.getMessage()); - } + + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> context.addRoutesConfigurations(rcb), + "Should throw exception"); + + assertEquals("Route configuration already exists with id: foo", e.getMessage()); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationErrorHandlerTest.java b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationErrorHandlerTest.java index 192a0a5e5d0d9..85178e4b5a522 100644 --- a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationErrorHandlerTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationErrorHandlerTest.java @@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteConfigurationBuilder; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Fail.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class RoutesConfigurationErrorHandlerTest extends ContextTestSupport { @@ -115,12 +115,9 @@ public void configure() { getMockEndpoint("mock:error").expectedBodiesReceived("Bye World"); - try { - template.sendBody("direct:start", "Hello World"); - fail("Should throw exception"); - } catch (Exception e) { - // expected - } + assertThrows(Exception.class, () -> template.sendBody("direct:start", "Hello World"), + "Should throw exception"); + template.sendBody("direct:start2", "Bye World"); assertMockEndpointsSatisfied(); diff --git a/core/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java b/core/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java index e1c024fd77bd7..a08563b33b806 100644 --- a/core/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/model/StartingRoutesErrorReportedTest.java @@ -20,14 +20,14 @@ import org.apache.camel.builder.RouteBuilder; 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; public class StartingRoutesErrorReportedTest extends ContextTestSupport { @Test public void testInvalidFrom() { - try { + Exception e = assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { @Override public void configure() { @@ -35,16 +35,15 @@ public void configure() { } }); context.start(); - fail(); - } catch (Exception e) { - assertTrue(e.getMessage().startsWith( - "Failed to create route route1: Route(route1)[From[direct:start?foo=bar] -> [To[mock:result]... because of")); - } + }); + + assertTrue(e.getMessage().startsWith( + "Failed to create route route1: Route(route1)[From[direct:start?foo=bar] -> [To[mock:result]... because of")); } @Test public void testInvalidTo() { - try { + Exception e = assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { @Override public void configure() { @@ -52,16 +51,15 @@ public void configure() { } }); context.start(); - fail(); - } catch (Exception e) { - assertTrue( - e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:")); - } + }); + + assertTrue( + e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:")); } @Test public void testMaskPassword() { - try { + Exception e = assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { @Override public void configure() { @@ -69,16 +67,15 @@ public void configure() { } }); context.start(); - fail(); - } catch (Exception e) { - assertTrue( - e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:")); - } + }); + + assertTrue( + e.getMessage().startsWith("Failed to create route route2 at: >>> To[direct:result?foo=bar] <<< in route:")); } @Test public void testInvalidBean() { - try { + Exception e = assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { @Override public void configure() { @@ -86,15 +83,14 @@ public void configure() { } }); context.start(); - fail("Should have thrown exception"); - } catch (Exception e) { - assertTrue(e.getMessage().startsWith("Failed to create route route3 at: >>> Bean[ref:] <<< in route:")); - } + }, "Should have thrown exception"); + + assertTrue(e.getMessage().startsWith("Failed to create route route3 at: >>> Bean[ref:] <<< in route:")); } @Test public void testUnavailableDataFormatOnClasspath() { - try { + Exception e = assertThrows(Exception.class, () -> { context.addRoutes(new RouteBuilder() { @Override public void configure() { @@ -102,11 +98,10 @@ public void configure() { } }); context.start(); - fail("Should have thrown exception"); - } catch (Exception e) { - assertTrue(e.getMessage().contains( - "Ensure that the data format is valid and the associated Camel component is present on the classpath")); - } + }, "Should have thrown exception"); + + assertTrue(e.getMessage().contains( + "Ensure that the data format is valid and the associated Camel component is present on the classpath")); } @Override