You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/08/23 10:53:07 UTC
[camel] branch main updated: Camel-19557: added assertions to camel-core tests (#11145)
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
The following commit(s) were added to refs/heads/main by this push:
new fc1d8c3494a Camel-19557: added assertions to camel-core tests (#11145)
fc1d8c3494a is described below
commit fc1d8c3494a9a825a4c904fa1e7e980a9f3801fa
Author: LostArtist <93...@users.noreply.github.com>
AuthorDate: Wed Aug 23 12:52:59 2023 +0200
Camel-19557: added assertions to camel-core tests (#11145)
* CAMEL-19557: added assertions to camel-core tests
Co-authored-by: Nikita Konovalov <nk...@redhat.com>
---
.../component/bean/BeanInfoInheritanceTest.java | 8 +-
.../direct/DirectEndpointRouteInlinedTest.java | 29 +--
.../component/direct/DirectNoConsumerTest.java | 23 +-
.../direct/DirectProducerBlockingTest.java | 44 ++--
.../SendToNonExistingDirectEndpointTest.java | 25 +-
.../extension/verifier/ComponentVerifierTest.java | 34 ++-
.../camel/component/file/FileConfigureTest.java | 30 ++-
.../file/FileConsumerAutoCreateDirectoryTest.java | 21 +-
.../file/FileConsumerBeginRenameStrategyTest.java | 9 +-
.../file/FileConsumerCommitRenameStrategyTest.java | 2 +
.../file/FileConsumerSpringSchedulerTest.java | 16 +-
...onsumerStartingDirectoryMustHaveAccessTest.java | 25 +-
.../file/FileInvalidStartingPathTest.java | 13 +-
.../file/FileProducerAllowNullBodyTest.java | 20 +-
.../file/FileProducerChmodOptionTest.java | 23 +-
.../FileProducerJailStartingDirectoryTest.java | 15 +-
.../file/FileProducerMoveExistingTest.java | 19 +-
.../camel/component/file/NewFileConsumeTest.java | 19 +-
.../camel/component/file/NewFileProduceTest.java | 11 +-
.../camel/component/mock/MockEndpointTest.java | 261 +++++++++------------
.../OptionalPropertiesDslInvalidSyntaxTest.java | 16 +-
.../properties/OptionalPropertiesDslTest.java | 11 +-
.../PropertiesComponentOnExceptionTest.java | 11 +-
...ropertiesComponentOnlyUseDefaultValuesTest.java | 9 +-
.../PropertiesComponentRegistryTest.java | 24 +-
.../component/rest/FromRestDuplicateTest.java | 29 +--
...meSedaQueueMultipleConsumersDifferenceTest.java | 23 +-
.../camel/component/xslt/InvalidXsltFileTest.java | 2 +-
.../camel/component/xslt/XsltFeatureRouteTest.java | 18 +-
.../camel/component/xslt/XsltFileNotFoundTest.java | 2 +-
.../org/apache/camel/converter/ConverterTest.java | 2 +-
.../camel/converter/DurationConverterTest.java | 16 +-
.../camel/converter/FutureConverterTest.java | 15 +-
.../converter/InstanceFallbackConverterTest.java | 15 +-
.../camel/converter/ObjectConverterTest.java | 33 +--
.../converter/stream/CachedOutputStreamTest.java | 9 +-
.../converter/stream/StreamCacheConverterTest.java | 14 +-
...lContextSuspendResumeRouteStartupOrderTest.java | 8 +-
.../DefaultCamelContextSuspendResumeRouteTest.java | 9 +-
.../camel/impl/DefaultClassResolverTest.java | 12 +-
.../DefaultComponentReferencePropertiesTest.java | 29 ++-
.../apache/camel/impl/DefaultComponentTest.java | 59 ++---
.../impl/DefaultComponentValidateURITest.java | 32 +--
...ConsumerTemplateWithCustomCacheMaxSizeTest.java | 24 +-
.../camel/impl/DefaultEndpointRegistryTest.java | 3 +-
.../camel/impl/DefaultExchangeHolderTest.java | 15 +-
.../org/apache/camel/impl/DefaultExchangeTest.java | 22 +-
.../impl/DefaultExecutorServiceManagerTest.java | 22 +-
.../impl/DefaultProducerTemplateAsyncTest.java | 24 +-
.../camel/impl/DefaultProducerTemplateTest.java | 44 ++--
...ProducerTemplateWithCustomCacheMaxSizeTest.java | 24 +-
.../EventDrivenPollingConsumerQueueSizeTest.java | 16 +-
.../camel/impl/FromHasNoOutputRouteTest.java | 20 +-
.../GracefulShutdownNoAutoStartOrderClashTest.java | 18 +-
.../impl/LifecycleStrategyFailOnStartupTest.java | 10 +-
.../camel/impl/MultipleConsumersSupportTest.java | 11 +-
.../camel/impl/RouteDirectSuspendResumeTest.java | 10 +-
.../impl/RouteMustHaveOutputOnExceptionTest.java | 11 +-
.../org/apache/camel/impl/RouteNoOutputTest.java | 21 +-
.../camel/impl/RouteSetRouteIdTwoTimesTest.java | 12 +-
.../impl/RouteWithMistypedComponentNameTest.java | 21 +-
...gistryStatisticsEnabledNoStreamCachingTest.java | 10 +-
...TypeConverterRegistryStatisticsEnabledTest.java | 12 +-
.../camel/impl/engine/DefaultCamelContextTest.java | 62 +++--
.../impl/engine/IntrospectionSupportTest.java | 30 ++-
.../impl/event/SimpleEventNotifierEventsTest.java | 16 +-
.../issues/AdviceWithErrorHandlerRemoveTest.java | 12 +-
.../issues/AdviceWithInvalidConfiguredTest.java | 32 ++-
.../apache/camel/issues/AdviceWithIssueTest.java | 26 +-
.../issues/AdviceWithOnExceptionRemoveTest.java | 12 +-
.../CharlesSplitAndTryCatchRollbackIssueTest.java | 36 ++-
.../issues/ChoiceEndOrEndChoiceIssueTest.java | 26 +-
...xceptionNotHandledErrorHandlerRefIssueTest.java | 15 +-
...otHandledErrorHandlerRefIssueTwoRoutesTest.java | 15 +-
...HandledRouteScopedErrorHandlerRefIssueTest.java | 15 +-
...uteScopedErrorHandlerRefIssueTwoRoutesTest.java | 15 +-
.../issues/ErrorHandlerOnRedeliveryStopTest.java | 15 +-
.../camel/issues/ExceptionCamel4022Test.java | 14 +-
.../org/apache/camel/issues/ExceptionTest.java | 38 +--
...tionThrownFromOnExceptionNoEndlessLoopTest.java | 15 +-
.../issues/ExceptionThrownFromOnExceptionTest.java | 69 +++---
.../camel/issues/MockValueBuilderIssueTest.java | 16 +-
...UnitOfWorkOnExceptionHandledFalseIssueTest.java | 15 +-
.../apache/camel/issues/OnCompletionIssueTest.java | 24 +-
84 files changed, 862 insertions(+), 1051 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoInheritanceTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoInheritanceTest.java
index 883525ab7c5..440930fc361 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoInheritanceTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoInheritanceTest.java
@@ -20,10 +20,7 @@ import org.apache.camel.ContextTestSupport;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Unit test for overridden methods in an inheritance.
@@ -67,8 +64,7 @@ public class BeanInfoInheritanceTest extends ContextTestSupport {
DefaultExchange exchange = new DefaultExchange(context);
exchange.getIn().setBody(new Request());
- assertThrows(AmbiguousMethodCallException.class,
- () -> beanInfo.createInvocation(null, exchange),
+ assertThrows(AmbiguousMethodCallException.class, () -> beanInfo.createInvocation(null, exchange),
"This should be ambiguous!");
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointRouteInlinedTest.java b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointRouteInlinedTest.java
index 65412212c14..9cba545267b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointRouteInlinedTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectEndpointRouteInlinedTest.java
@@ -21,8 +21,8 @@ import org.apache.camel.FailedToStartRouteException;
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 DirectEndpointRouteInlinedTest extends ContextTestSupport {
@@ -48,20 +48,21 @@ public class DirectEndpointRouteInlinedTest extends ContextTestSupport {
@Test
public void testDirectExistingExists() throws Exception {
- try {
- context.addRoutes(new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- from("direct:start").to("mock:result");
- from("direct:start").to("mock:bar");
- }
- });
- fail("Should have thrown exception");
- } catch (FailedToStartRouteException e) {
- assertTrue(e.getMessage().matches(
- "Failed to start route route[0-9]+ because of Multiple consumers for the same endpoint is not allowed: direct://start"));
- }
+ FailedToStartRouteException e = assertThrows(FailedToStartRouteException.class,
+ () -> {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").to("mock:result");
+
+ from("direct:start").to("mock:bar");
+ }
+ });
+ }, "Should have thrown exception");
+
+ assertTrue(e.getMessage().matches(
+ "Failed to start route route[0-9]+ because of Multiple consumers for the same endpoint is not allowed: direct://start"));
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectNoConsumerTest.java b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectNoConsumerTest.java
index f2d1fdd7e4e..5f34cd7093d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectNoConsumerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectNoConsumerTest.java
@@ -24,7 +24,6 @@ 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.fail;
public class DirectNoConsumerTest extends ContextTestSupport {
@@ -45,12 +44,11 @@ public class DirectNoConsumerTest extends ContextTestSupport {
context.start();
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should throw an exception");
- } catch (CamelExecutionException e) {
- assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should throw an exception");
+
+ assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
}
public void testInOut() throws Exception {
@@ -65,12 +63,11 @@ public class DirectNoConsumerTest extends ContextTestSupport {
context.start();
- try {
- template.requestBody("direct:start", "Hello World");
- fail("Should throw an exception");
- } catch (CamelExecutionException e) {
- assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.requestBody("direct:start", "Hello World"),
+ "Should throw an exception");
+
+ assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
index 1724734fffb..b6e4dfe9e8d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/direct/DirectProducerBlockingTest.java
@@ -27,8 +27,8 @@ import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
@Timeout(20)
public class DirectProducerBlockingTest extends ContextTestSupport {
@@ -39,16 +39,16 @@ public class DirectProducerBlockingTest extends ContextTestSupport {
endpoint.getConsumer().suspend();
StopWatch watch = new StopWatch();
- try {
- template.sendBody("direct:suspended?block=true&timeout=500", "hello world");
- fail("Expected CamelExecutionException");
- } catch (CamelExecutionException e) {
- DirectConsumerNotAvailableException cause
- = assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
- assertIsInstanceOf(CamelExchangeException.class, cause);
- assertTrue(watch.taken() > 490);
- assertTrue(watch.taken() < 5000);
- }
+
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:suspended?block=true&timeout=500", "hello world"),
+ "Expected CamelExecutionException");
+
+ DirectConsumerNotAvailableException cause
+ = assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
+ assertIsInstanceOf(CamelExchangeException.class, cause);
+ assertTrue(watch.taken() > 490);
+ assertTrue(watch.taken() < 5000);
}
@Test
@@ -57,17 +57,17 @@ public class DirectProducerBlockingTest extends ContextTestSupport {
endpoint.getConsumer().suspend();
StopWatch watch = new StopWatch();
- try {
- template.sendBody("direct:start?block=true&timeout=500", "hello world");
- fail("Expected CamelExecutionException");
- } catch (CamelExecutionException e) {
- DirectConsumerNotAvailableException cause
- = assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
- assertIsInstanceOf(CamelExchangeException.class, cause);
-
- assertTrue(watch.taken() > 490);
- assertTrue(watch.taken() < 5000);
- }
+
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start?block=true&timeout=500", "hello world"),
+ "Expected CamelExecutionException");
+
+ DirectConsumerNotAvailableException cause
+ = assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
+ assertIsInstanceOf(CamelExchangeException.class, cause);
+
+ assertTrue(watch.taken() > 490);
+ assertTrue(watch.taken() < 5000);
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/direct/SendToNonExistingDirectEndpointTest.java b/core/camel-core/src/test/java/org/apache/camel/component/direct/SendToNonExistingDirectEndpointTest.java
index bf0dd01e666..80f087a25c4 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/direct/SendToNonExistingDirectEndpointTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/direct/SendToNonExistingDirectEndpointTest.java
@@ -22,7 +22,7 @@ import org.apache.camel.ContextTestSupport;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class SendToNonExistingDirectEndpointTest extends ContextTestSupport {
@@ -32,21 +32,20 @@ public class SendToNonExistingDirectEndpointTest extends ContextTestSupport {
}
@Test
- public void testDirect() throws Exception {
+ public void testDirect() {
context.start();
context.getComponent("direct", DirectComponent.class).setBlock(false);
- try {
- template.sendBody("direct:foo", "Hello World");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- DirectConsumerNotAvailableException cause
- = assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
- assertIsInstanceOf(CamelExchangeException.class, cause); // ensure
- // backwards
- // compatibility
- assertNotNull(cause.getExchange());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:foo", "Hello World"),
+ "Should have thrown exception");
+
+ DirectConsumerNotAvailableException cause
+ = assertIsInstanceOf(DirectConsumerNotAvailableException.class, e.getCause());
+ assertIsInstanceOf(CamelExchangeException.class, cause); // ensure
+ // backwards
+ // compatibility
+ assertNotNull(cause.getExchange());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/extension/verifier/ComponentVerifierTest.java b/core/camel-core/src/test/java/org/apache/camel/component/extension/verifier/ComponentVerifierTest.java
index 58df81727eb..3950bb51805 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/extension/verifier/ComponentVerifierTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/extension/verifier/ComponentVerifierTest.java
@@ -20,7 +20,10 @@ import org.apache.camel.component.extension.ComponentVerifierExtension;
import org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class ComponentVerifierTest {
@@ -41,32 +44,27 @@ public class ComponentVerifierTest {
@Test
public void testNullCode() {
- try {
- VerificationError.asCode(null);
- fail("Code must not be null");
- } catch (IllegalArgumentException exp) {
- assertTrue(exp.getMessage().contains("null"));
- }
+
+ IllegalArgumentException exp = assertThrows(IllegalArgumentException.class,
+ () -> VerificationError.asCode(null), "Code must not be null");
+
+ assertTrue(exp.getMessage().contains("null"));
}
@Test
public void testNullAttribute() {
- try {
- VerificationError.asAttribute(null);
- fail("Attribute must not be null");
- } catch (IllegalArgumentException exp) {
- assertTrue(exp.getMessage().contains("null"));
- }
+
+ IllegalArgumentException exp = assertThrows(IllegalArgumentException.class,
+ () -> VerificationError.asAttribute(null), "Attribute must not be null");
+
+ assertTrue(exp.getMessage().contains("null"));
}
@Test
public void testScopeFromString() {
assertEquals(ComponentVerifierExtension.Scope.PARAMETERS, ComponentVerifierExtension.Scope.fromString("PaRaMeTeRS"));
- try {
- ComponentVerifierExtension.Scope.fromString("unknown");
- fail();
- } catch (IllegalArgumentException exp) {
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> ComponentVerifierExtension.Scope.fromString("unknown"));
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
index d044bd6ccad..fbe7e5ecf12 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConfigureTest.java
@@ -27,8 +27,8 @@ import org.junit.jupiter.api.Test;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
public class FileConfigureTest extends ContextTestSupport {
@@ -98,27 +98,25 @@ public class FileConfigureTest extends ContextTestSupport {
assertNotNull(endpoint, "Could not find endpoint: file://target/data/FileConfigureTest/bar?charset=UTF-8");
assertEquals("UTF-8", endpoint.getCharset(), "Get a wrong charset");
- try {
- resolveMandatoryEndpoint("file://target/data/FileConfigureTest/bar?charset=ASSI", FileEndpoint.class);
- // The charset is wrong
- fail("Expect a configure exception here");
- } catch (Exception ex) {
- boolean b = ex instanceof ResolveEndpointFailedException;
- assertTrue(b, "Get the wrong exception type here");
- }
+ Exception ex = assertThrows(Exception.class,
+ () -> resolveMandatoryEndpoint("file://target/data/FileConfigureTest/bar?charset=ASSI", FileEndpoint.class),
+ "Expect a configure exception here");
+
+ boolean b = ex instanceof ResolveEndpointFailedException;
+ assertTrue(b, "Get the wrong exception type here");
}
@Test
public void testConsumerConfigurations() throws Exception {
FileConsumer consumer = createFileConsumer("file://target/data/FileConfigureTest/bar?recursive=true");
assertNotNull(consumer);
- try {
- createFileConsumer("file://target/data/FileConfigureTest/bar?recursiv=true");
- fail("Expect a configure exception here");
- } catch (Exception ex) {
- boolean b = ex instanceof ResolveEndpointFailedException;
- assertTrue(b, "Get the wrong exception type here");
- }
+
+ Exception ex = assertThrows(Exception.class,
+ () -> createFileConsumer("file://target/data/FileConfigureTest/bar?recursiv=true"),
+ "Expect a configure exception here");
+
+ boolean b = ex instanceof ResolveEndpointFailedException;
+ assertTrue(b, "Get the wrong exception type here");
}
private FileConsumer createFileConsumer(String endpointUri) throws Exception {
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java
index 1fdc82be72d..9884dad975b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java
@@ -27,8 +27,8 @@ import org.apache.camel.Processor;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
+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 FileConsumerAutoCreateDirectoryTest extends ContextTestSupport {
@@ -105,16 +105,15 @@ public class FileConsumerAutoCreateDirectoryTest extends ContextTestSupport {
@Test
public void testStartingDirectoryMustExistDirectory() throws Exception {
Endpoint endpoint = context.getEndpoint(fileUri("foo?autoCreate=false&startingDirectoryMustExist=true"));
- try {
- endpoint.createConsumer(new Processor() {
- public void process(Exchange exchange) throws Exception {
- // noop
- }
- });
- fail("Should have thrown an exception");
- } catch (FileNotFoundException e) {
- assertTrue(e.getMessage().startsWith("Starting directory does not exist"));
- }
+
+ FileNotFoundException e = assertThrows(FileNotFoundException.class,
+ () -> {
+ endpoint.createConsumer(exchange -> {
+ // noop
+ });
+ }, "Should have thrown an exception");
+
+ assertTrue(e.getMessage().startsWith("Starting directory does not exist"));
// the directory should NOT exists
assertFalse(Files.exists(testDirectory("foo")), "Directory should NOT be created");
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBeginRenameStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBeginRenameStrategyTest.java
index 3d7fb8feb96..059bc460c71 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBeginRenameStrategyTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerBeginRenameStrategyTest.java
@@ -26,6 +26,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -49,13 +50,11 @@ public class FileConsumerBeginRenameStrategyTest extends ContextTestSupport {
public void testRenameFileExists() throws Exception {
// create a file in inprogress to let there be a duplicate file
testDirectory("inprogress", true);
- FileWriter fw = new FileWriter(testFile("inprogress/london.txt").toFile());
- try {
+
+ try(FileWriter fw = new FileWriter(testFile("inprogress/london.txt").toFile())) {
fw.write("I was there once in London");
fw.flush();
- } finally {
- fw.close();
- }
+ };
MockEndpoint mock = getMockEndpoint("mock:report");
mock.expectedBodiesReceived("Hello London");
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameStrategyTest.java
index 53294aaa1fd..203bfcac38c 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameStrategyTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameStrategyTest.java
@@ -25,6 +25,7 @@ import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.converter.IOConverter;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
@@ -47,6 +48,7 @@ public class FileConsumerCommitRenameStrategyTest extends ContextTestSupport {
public void testRenameFileExists() throws Exception {
// create a file in done to let there be a duplicate file
testDirectory("done", true);
+
try (FileWriter fw = new FileWriter(testFile("done/london.txt").toFile())) {
fw.write("I was there once in London");
fw.flush();
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerSpringSchedulerTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerSpringSchedulerTest.java
index b6451a02c24..b95ad0e0cdb 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerSpringSchedulerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerSpringSchedulerTest.java
@@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteBuilder;
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 FileConsumerSpringSchedulerTest extends ContextTestSupport {
@@ -38,13 +38,13 @@ public class FileConsumerSpringSchedulerTest extends ContextTestSupport {
from(fileUri("?scheduler=spring")).routeId("foo").noAutoStartup().to("mock:result");
}
});
- try {
- context.start();
- fail("Should throw exception");
- } catch (Exception e) {
- ClassNotFoundException cnfe = assertIsInstanceOf(ClassNotFoundException.class, e.getCause().getCause().getCause());
- assertEquals("org.apache.camel.spring.pollingconsumer.SpringScheduledPollConsumerScheduler", cnfe.getMessage());
- }
+
+ Exception e = assertThrows(Exception.class,
+ () -> context.start(),
+ "Should throw exception");
+
+ ClassNotFoundException cnfe = assertIsInstanceOf(ClassNotFoundException.class, e.getCause().getCause().getCause());
+ assertEquals("org.apache.camel.spring.pollingconsumer.SpringScheduledPollConsumerScheduler", cnfe.getMessage());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
index 659c259e985..420b44963e7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
@@ -21,15 +21,13 @@ import java.io.IOException;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
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 FileConsumerStartingDirectoryMustHaveAccessTest extends ContextTestSupport {
@@ -50,18 +48,17 @@ public class FileConsumerStartingDirectoryMustHaveAccessTest extends ContextTest
}
@Test
- public void testStartingDirectoryMustHaveAccess() throws Exception {
+ public void testStartingDirectoryMustHaveAccess() {
Endpoint endpoint = context.getEndpoint(
fileUri("noAccess?autoCreate=false&startingDirectoryMustExist=true&startingDirectoryMustHaveAccess=true"));
- try {
- endpoint.createConsumer(new Processor() {
- public void process(Exchange exchange) throws Exception {
- // noop
- }
- });
- fail("Should have thrown an exception");
- } catch (IOException e) {
- assertTrue(e.getMessage().startsWith("Starting directory permission denied"), e.getMessage());
- }
+
+ IOException e = assertThrows(IOException.class,
+ () -> {
+ endpoint.createConsumer(exchange -> {
+ // noop
+ });
+ }, "Should have thrown an exception");
+
+ assertTrue(e.getMessage().startsWith("Starting directory permission denied"), e.getMessage());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileInvalidStartingPathTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileInvalidStartingPathTest.java
index a5168dc777f..506ecf0c4ef 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileInvalidStartingPathTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileInvalidStartingPathTest.java
@@ -20,19 +20,18 @@ import org.apache.camel.ContextTestSupport;
import org.apache.camel.ResolveEndpointFailedException;
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 FileInvalidStartingPathTest extends ContextTestSupport {
@Test
public void testInvalidStartingPath() {
- try {
- context.getEndpoint(fileUri("${date:now:yyyyMMdd}/${in.header.messageType}-${date:now:hhmmss}.txt"));
- fail("Should have thrown an exception");
- } catch (ResolveEndpointFailedException e) {
- assertTrue(e.getCause().getMessage().startsWith("Invalid directory"));
- }
+ ResolveEndpointFailedException e = assertThrows(ResolveEndpointFailedException.class,
+ () -> context.getEndpoint(fileUri("${date:now:yyyyMMdd}/${in.header.messageType}-${date:now:hhmmss}.txt")),
+ "Should have thrown an exception");
+
+ assertTrue(e.getCause().getMessage().startsWith("Invalid directory"));
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
index 275e682b96f..62723a6ca33 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
@@ -23,8 +23,8 @@ import org.apache.camel.ContextTestSupport;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
+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 tests to ensure that When the allowNullBody option is set to true it will create an empty file and not throw an
@@ -40,15 +40,15 @@ public class FileProducerAllowNullBodyTest extends ContextTestSupport {
}
@Test
- public void testAllowNullBodyFalse() throws Exception {
- try {
- template.sendBody(fileUri("?fileName=allowNullBody.txt"), null);
- fail("Should have thrown a GenericFileOperationFailedException");
- } catch (CamelExecutionException e) {
- GenericFileOperationFailedException cause
- = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
- assertTrue(cause.getMessage().endsWith("allowNullBody.txt"));
- }
+ public void testAllowNullBodyFalse() {
+
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody(fileUri("?fileName=allowNullBody.txt"), null),
+ "Should have thrown a GenericFileOperationFailedException");
+
+ GenericFileOperationFailedException cause
+ = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
+ assertTrue(cause.getMessage().endsWith("allowNullBody.txt"));
assertFalse(Files.exists(testFile("allowNullBody.txt")),
"allowNullBody set to false with null body should not create a new file");
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java
index 99cd3ecbc1e..8694e760e39 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java
@@ -35,8 +35,8 @@ import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
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;
@DisabledOnOs(OS.WINDOWS)
public class FileProducerChmodOptionTest extends ContextTestSupport {
@@ -68,23 +68,22 @@ public class FileProducerChmodOptionTest extends ContextTestSupport {
}
@Test
- public void testInvalidChmod() throws Exception {
- try {
+ public void testInvalidChmod() {
+ FailedToCreateRouteException e = assertThrows(FailedToCreateRouteException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
from("direct:writeBadChmod1").to(fileUri("?chmod=abc")).to("mock:badChmod1");
}
});
- fail("Expected FailedToCreateRouteException");
- } catch (FailedToCreateRouteException e) {
- assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
- PropertyBindingException pbe = assertIsInstanceOf(PropertyBindingException.class, e.getCause().getCause());
- assertEquals("chmod", pbe.getPropertyName());
- IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, pbe.getCause());
- assertTrue(iae.getMessage().contains("chmod option [abc] is not valid"));
- }
+ }, "Expected FailedToCreateRouteException");
+
+ assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+ PropertyBindingException pbe = assertIsInstanceOf(PropertyBindingException.class, e.getCause().getCause());
+ assertEquals("chmod", pbe.getPropertyName());
+ IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, pbe.getCause());
+ assertTrue(iae.getMessage().contains("chmod option [abc] is not valid"));
}
/**
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerJailStartingDirectoryTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerJailStartingDirectoryTest.java
index 4197e1bb133..41790842f72 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerJailStartingDirectoryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerJailStartingDirectoryTest.java
@@ -22,8 +22,8 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
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 FileProducerJailStartingDirectoryTest extends ContextTestSupport {
@@ -32,13 +32,12 @@ public class FileProducerJailStartingDirectoryTest extends ContextTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
- try {
- template.sendBodyAndHeader("direct:start", "Hello World", Exchange.FILE_NAME, "hello.txt");
- fail("Should have thrown exception");
- } catch (Exception e) {
- IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertTrue(iae.getMessage().contains("as the filename is jailed to the starting directory"));
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBodyAndHeader("direct:start", "Hello World", Exchange.FILE_NAME, "hello.txt"),
+ "Should have thrown exception");
+
+ IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertTrue(iae.getMessage().contains("as the filename is jailed to the starting directory"));
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
index 73fd9a5c323..2feb6cdf0b3 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
@@ -28,9 +28,10 @@ import org.apache.camel.Exchange;
import org.junit.jupiter.api.Test;
import static java.io.File.separator;
+
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;
/**
*
@@ -215,7 +216,7 @@ public class FileProducerMoveExistingTest extends ContextTestSupport {
assertFileExists(testFile("hello.txt"), "Bye World");
- // would move into sub directory and keep existing name as is
+ // would move into subdirectory and keep existing name as is
assertFileExists(testFile("backup/hello.txt"), "Hello World");
}
@@ -248,17 +249,17 @@ public class FileProducerMoveExistingTest extends ContextTestSupport {
fileUri("?fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}&eagerDeleteTargetFile=false"),
"Hello World",
Exchange.FILE_NAME, "hello.txt");
- try {
+
+ CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> {
template.sendBodyAndHeader(
fileUri("?fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}&eagerDeleteTargetFile=false"),
"Bye World",
Exchange.FILE_NAME, "hello.txt");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- GenericFileOperationFailedException cause
- = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
- assertTrue(cause.getMessage().startsWith("Cannot move existing file"));
- }
+ }, "Should have thrown an exception");
+
+ GenericFileOperationFailedException cause
+ = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
+ assertTrue(cause.getMessage().startsWith("Cannot move existing file"));
// we could not write the new file so the previous context should be
// there
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
index dda2845d06d..7c3bbfe03fb 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileConsumeTest.java
@@ -24,8 +24,6 @@ import java.util.concurrent.TimeUnit;
import org.apache.camel.Consumer;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -53,15 +51,16 @@ public class NewFileConsumeTest extends ContextTestSupport {
Files.write(testFile("hello.txt"), "Hello World".getBytes());
Endpoint endpoint = comp.createEndpoint(fileUri(), testDirectory().toString(),
- new HashMap<String, Object>());
- Consumer consumer = endpoint.createConsumer(new Processor() {
- public void process(Exchange exchange) throws Exception {
- assertNotNull(exchange);
- String body = exchange.getIn().getBody(String.class);
- assertEquals("Hello World", body);
- latch.countDown();
- }
+ new HashMap<>());
+ Consumer consumer = endpoint.createConsumer(exchange -> {
+ assertNotNull(exchange);
+ String body = exchange.getIn().getBody(String.class);
+ assertEquals("Hello World", body);
+ latch.countDown();
});
+
+ assertFileExists(testFile("hello.txt"));
+
consumer.start();
latch.await(5, TimeUnit.SECONDS);
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileProduceTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileProduceTest.java
index 7a7b3474453..8fa092a662d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileProduceTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/NewFileProduceTest.java
@@ -21,7 +21,6 @@ import java.util.HashMap;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
import org.junit.jupiter.api.Test;
/**
@@ -35,12 +34,10 @@ public class NewFileProduceTest extends ContextTestSupport {
comp.setCamelContext(context);
Endpoint endpoint = comp.createEndpoint(fileUri(), testDirectory().toString(),
- new HashMap<String, Object>());
- template.send(endpoint, new Processor() {
- public void process(Exchange exchange) throws Exception {
- exchange.getIn().setHeader(Exchange.FILE_NAME, "bye.txt");
- exchange.getIn().setBody("Bye World");
- }
+ new HashMap<>());
+ template.send(endpoint, exchange -> {
+ exchange.getIn().setHeader(Exchange.FILE_NAME, "bye.txt");
+ exchange.getIn().setBody("Bye World");
});
assertFileExists(testFile("bye.txt"));
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java b/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
index df9f60b5230..a1bdb9ff052 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
@@ -34,7 +34,10 @@ import org.apache.camel.spi.Language;
import org.apache.camel.spi.Registry;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class MockEndpointTest extends ContextTestSupport {
@@ -119,19 +122,18 @@ public class MockEndpointTest extends ContextTestSupport {
}
@Test
- public void testExpectsHeadersInAnyOrderFail() throws Exception {
+ public void testExpectsHeadersInAnyOrderFail() {
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedHeaderValuesReceivedInAnyOrder("counter", 11, 12, 7, 14, 15);
sendMessages(15, 12, 14, 13, 11);
- try {
- resultEndpoint.assertIsSatisfied();
- fail("Should fail");
- } catch (AssertionError e) {
- assertEquals("mock://result Expected 5 headers with key[counter], received 4 headers. Expected header values: [7]",
- e.getMessage());
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ resultEndpoint::assertIsSatisfied,
+ "Should fail");
+
+ assertEquals("mock://result Expected 5 headers with key[counter], received 4 headers. Expected header values: [7]",
+ e.getMessage());
}
@Test
@@ -155,30 +157,20 @@ public class MockEndpointTest extends ContextTestSupport {
}
@Test
- public void testExpectsPropertiesInAnyOrderFail() throws Exception {
+ public void testExpectsPropertiesInAnyOrderFail() {
MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
resultEndpoint.expectedPropertyValuesReceivedInAnyOrder("foo", 123, 456);
- template.send("direct:a", new Processor() {
- public void process(Exchange exchange) throws Exception {
- exchange.setProperty("foo", 123);
- }
- });
+ template.send("direct:a", exchange -> exchange.setProperty("foo", 123));
+ template.send("direct:a", exchange -> exchange.setProperty("foo", 789));
- template.send("direct:a", new Processor() {
- public void process(Exchange exchange) throws Exception {
- exchange.setProperty("foo", 789);
- }
- });
+ AssertionError e = assertThrows(AssertionError.class,
+ resultEndpoint::assertIsSatisfied,
+ "Should fail");
- try {
- resultEndpoint.assertIsSatisfied();
- fail("Should fail");
- } catch (AssertionError e) {
- assertEquals(
- "mock://result Expected 2 properties with key[foo], received 1 properties. Expected property values: [456]",
- e.getMessage());
- }
+ assertEquals(
+ "mock://result Expected 2 properties with key[foo], received 1 properties. Expected property values: [456]",
+ e.getMessage());
}
@Test
@@ -369,79 +361,64 @@ public class MockEndpointTest extends ContextTestSupport {
}
@Test
- public void testSimulateError() throws Exception {
+ public void testSimulateError() {
MockEndpoint mock = getMockEndpoint("mock:result");
- mock.whenAnyExchangeReceived(new Processor() {
- public void process(Exchange exchange) throws Exception {
- exchange.setException(new IllegalArgumentException("Forced"));
- }
- });
+ mock.whenAnyExchangeReceived(exchange -> exchange.setException(new IllegalArgumentException("Forced")));
- try {
- template.sendBody("direct:a", "Hello World");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Forced", e.getCause().getMessage());
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:a", "Hello World"),
+ "Should have thrown an exception");
+
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced", e.getCause().getMessage());
}
@Test
- public void testSimulateErrorByThrowingException() throws Exception {
+ public void testSimulateErrorByThrowingException() {
MockEndpoint mock = getMockEndpoint("mock:result");
- mock.whenAnyExchangeReceived(new Processor() {
- public void process(Exchange exchange) throws Exception {
- throw new IllegalArgumentException("Forced");
- }
+ mock.whenAnyExchangeReceived(exchange -> {
+ throw new IllegalArgumentException("Forced");
});
- try {
- template.sendBody("direct:a", "Hello World");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Forced", e.getCause().getMessage());
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:a", "Hello World"),
+ "Should have thrown an exception");
+
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced", e.getCause().getMessage());
}
@Test
- public void testSimulateErrorWithIndex() throws Exception {
+ public void testSimulateErrorWithIndex() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(2);
- mock.whenExchangeReceived(2, new Processor() {
- public void process(Exchange exchange) throws Exception {
- exchange.setException(new IllegalArgumentException("Forced"));
- }
- });
+ mock.whenExchangeReceived(2, exchange -> exchange.setException(new IllegalArgumentException("Forced")));
template.sendBody("direct:a", "Hello World");
- try {
- template.sendBody("direct:a", "Hello World");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Forced", e.getCause().getMessage());
- }
+
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:a", "Hello World"),
+ "Should have thrown an exception");
+
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced", e.getCause().getMessage());
}
@Test
- public void testSimulateErrorWithIndexByThrowingException() throws Exception {
+ public void testSimulateErrorWithIndexByThrowingException() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(2);
- mock.whenExchangeReceived(2, new Processor() {
- public void process(Exchange exchange) throws Exception {
- throw new IllegalArgumentException("Forced");
- }
+ mock.whenExchangeReceived(2, exchange -> {
+ throw new IllegalArgumentException("Forced");
});
template.sendBody("direct:a", "Hello World");
- try {
- template.sendBody("direct:a", "Bye World");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Forced", e.getCause().getMessage());
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:a", "Bye World"),
+ "Should have thrown an exception");
+
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced", e.getCause().getMessage());
}
@Test
@@ -511,11 +488,7 @@ public class MockEndpointTest extends ContextTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.setExpectedMessageCount(1);
- mock.setReporter(new Processor() {
- public void process(Exchange exchange) throws Exception {
- reported.set(true);
- }
- });
+ mock.setReporter(exchange -> reported.set(true));
template.sendBody("direct:a", "Hello World");
@@ -528,18 +501,15 @@ public class MockEndpointTest extends ContextTestSupport {
@Test
public void testNoArgCtr() {
MockEndpoint mock = new MockEndpoint("mock:bar", new MockComponent(context));
- try {
- mock.createConsumer(null);
- fail("Should have thrown an exception");
- } catch (Exception e) {
- // not possible
- }
+ assertThrows(Exception.class,
+ () -> mock.createConsumer(null),
+ "Should have thrown an exception");
assertEquals(0, mock.getFailures().size());
}
@Test
- public void testHeaderMissing() throws Exception {
+ public void testHeaderMissing() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived("foo", 123);
@@ -547,50 +517,46 @@ public class MockEndpointTest extends ContextTestSupport {
template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- assertEquals("mock://result No header with name bar found for message: 0", e.getMessage());
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+
+ assertEquals("mock://result No header with name bar found for message: 0", e.getMessage());
}
@Test
- public void testHeaderNoMessageSent() throws Exception {
+ public void testHeaderNoMessageSent() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedHeaderReceived("foo", 123);
// just wait a little bit as we dont want to wait 10 seconds (default)
mock.setResultWaitTime(5);
// do not send any message
+ AssertionError e = assertThrows(AssertionError.class,
+ mock::assertIsSatisfied,
+ "Should fail");
- try {
- mock.assertIsSatisfied();
- fail("Should fail");
- } catch (AssertionError e) {
- assertEquals("mock://result Received message count 0, expected at least 1", e.getMessage());
- }
+ assertEquals("mock://result Received message count 0, expected at least 1", e.getMessage());
}
@Test
- public void testHeaderInvalidValue() throws Exception {
+ public void testHeaderInvalidValue() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived("bar", "cheese");
template.sendBodyAndHeader("direct:a", "Hello World", "bar", "beer");
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- assertEquals("mock://result Header with name bar for message: 0. Expected: <cheese> but was: <beer>",
- e.getMessage());
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+
+ assertEquals("mock://result Header with name bar for message: 0. Expected: <cheese> but was: <beer>",
+ e.getMessage());
}
@Test
- public void testPropertyMissing() throws Exception {
+ public void testPropertyMissing() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedPropertyReceived("foo", 123);
@@ -598,12 +564,11 @@ public class MockEndpointTest extends ContextTestSupport {
template.sendBodyAndProperty("direct:a", "Hello World", "foo", 123);
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- assertEquals("mock://result No property with name bar found for message: 0", e.getMessage());
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+
+ assertEquals("mock://result No property with name bar found for message: 0", e.getMessage());
}
@Test
@@ -642,24 +607,22 @@ public class MockEndpointTest extends ContextTestSupport {
}
@Test
- public void testPropertyInvalidValue() throws Exception {
+ public void testPropertyInvalidValue() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedPropertyReceived("bar", "cheese");
template.sendBodyAndProperty("direct:a", "Hello World", "bar", "beer");
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- assertEquals("mock://result Property with name bar for message: 0. Expected: <cheese> but was: <beer>",
- e.getMessage());
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+ assertEquals("mock://result Property with name bar for message: 0. Expected: <cheese> but was: <beer>",
+ e.getMessage());
}
@Test
- public void testMessageIndexIsEqualTo() throws Exception {
+ public void testMessageIndexIsEqualTo() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(2);
mock.message(0).header("foo").isEqualTo(123);
@@ -668,17 +631,16 @@ public class MockEndpointTest extends ContextTestSupport {
template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
template.sendBodyAndHeader("direct:a", "Hello World", "bar", 234);
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- String s = "Assertion error at index 1 on mock mock://result with predicate: header(bar) == 444 evaluated as: 234 == 444";
- assertTrue(e.getMessage().startsWith(s));
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+
+ String s = "Assertion error at index 1 on mock mock://result with predicate: header(bar) == 444 evaluated as: 234 == 444";
+ assertTrue(e.getMessage().startsWith(s));
}
@Test
- public void testPredicateEvaluationIsNull() throws Exception {
+ public void testPredicateEvaluationIsNull() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(2);
mock.message(0).header("foo").isNotNull();
@@ -687,17 +649,16 @@ public class MockEndpointTest extends ContextTestSupport {
template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
template.sendBodyAndHeader("direct:a", "Hello World", "bar", 234);
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- String s = "Assertion error at index 1 on mock mock://result with predicate: header(bar) is null evaluated as: 234 is null";
- assertTrue(e.getMessage().startsWith(s));
- }
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+
+ String s = "Assertion error at index 1 on mock mock://result with predicate: header(bar) is null evaluated as: 234 is null";
+ assertTrue(e.getMessage().startsWith(s));
}
@Test
- public void testPredicateEvaluationIsInstanceOf() throws Exception {
+ public void testPredicateEvaluationIsInstanceOf() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(2);
mock.message(0).header("foo").isNotNull();
@@ -706,13 +667,13 @@ public class MockEndpointTest extends ContextTestSupport {
template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
template.sendBodyAndHeader("direct:a", "Hello World", "bar", 234);
- try {
- assertMockEndpointsSatisfied();
- fail("Should have thrown exception");
- } catch (AssertionError e) {
- String s = "Assertion error at index 1 on mock mock://result with predicate: header(bar) instanceof java.lang.String";
- assertTrue(e.getMessage().startsWith(s));
- }
+
+
+ AssertionError e = assertThrows(AssertionError.class,
+ this::assertMockEndpointsSatisfied,
+ "Should have thrown exception");
+ String s = "Assertion error at index 1 on mock mock://result with predicate: header(bar) instanceof java.lang.String";
+ assertTrue(e.getMessage().startsWith(s));
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslInvalidSyntaxTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslInvalidSyntaxTest.java
index 4814c8644ae..be98bdd9af1 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslInvalidSyntaxTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslInvalidSyntaxTest.java
@@ -22,7 +22,7 @@ import org.apache.camel.builder.RouteBuilder;
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;
/**
* Test that placeholder DSL is working as expected.
@@ -38,13 +38,13 @@ public class OptionalPropertiesDslInvalidSyntaxTest extends ContextTestSupport {
.throwException(new IllegalAccessException("Damn")).to("mock:b");
}
});
- try {
- context.start();
- fail("Should have thrown exception");
- } catch (Exception e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Property with key [xxx] not found in properties from text: {{xxx}}", cause.getMessage());
- }
+
+ Exception e = assertThrows(Exception.class,
+ () -> context.start(),
+ "Should have thrown exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Property with key [xxx] not found in properties from text: {{xxx}}", cause.getMessage());
}
@Override
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslTest.java
index 802bcd46028..163965b0fee 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslTest.java
@@ -21,7 +21,7 @@ import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Test that placeholder DSL is working as expected.
@@ -33,12 +33,9 @@ public class OptionalPropertiesDslTest extends ContextTestSupport {
getMockEndpoint("mock:a").expectedMessageCount(1);
getMockEndpoint("mock:b").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnExceptionTest.java
index e48613c3f6a..b95bd9ba6aa 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnExceptionTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnExceptionTest.java
@@ -26,7 +26,7 @@ import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.spi.Registry;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class PropertiesComponentOnExceptionTest extends ContextTestSupport {
@@ -37,12 +37,9 @@ public class PropertiesComponentOnExceptionTest extends ContextTestSupport {
mock.message(0).header(Exchange.REDELIVERED).isEqualTo(true);
mock.message(0).header(Exchange.REDELIVERY_COUNTER).isEqualTo(3);
- 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");
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java
index 8f0af338370..4fe361170bd 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java
@@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class PropertiesComponentOnlyUseDefaultValuesTest extends ContextTestSupport {
@@ -57,12 +57,7 @@ public class PropertiesComponentOnlyUseDefaultValuesTest extends ContextTestSupp
}
});
- try {
- context.start();
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class, () -> context.start(), "Should have thrown exception");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
index 2f6712dd331..b350b433a7c 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRegistryTest.java
@@ -25,7 +25,11 @@ import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.DefaultRegistry;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.fail;
public class PropertiesComponentRegistryTest extends ContextTestSupport {
@@ -82,20 +86,18 @@ public class PropertiesComponentRegistryTest extends ContextTestSupport {
}
@Test
- public void testPropertiesComponentRegistryLookupNameAndType() throws Exception {
+ public void testPropertiesComponentRegistryLookupNameAndType() {
context.start();
assertSame(foo, context.getRegistry().lookupByNameAndType("{{bean.foo}}", MyFooBean.class));
assertSame(bar, context.getRegistry().lookupByNameAndType("{{bean.bar}}", MyDummyBean.class));
- try {
- context.getRegistry().lookupByNameAndType("{{bean.unknown}}", MyDummyBean.class);
- fail("Should have thrown exception");
- } catch (RuntimeCamelException e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Property with key [bean.unknown] not found in properties from text: {{bean.unknown}}",
- cause.getMessage());
- }
- }
+ RuntimeCamelException e = assertThrows(RuntimeCamelException.class,
+ () -> context.getRegistry().lookupByNameAndType("{{bean.unknown}}", MyDummyBean.class),
+ "Should have thrown exception");
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Property with key [bean.unknown] not found in properties from text: {{bean.unknown}}",
+ cause.getMessage());
+ }
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java
index 116ac0848c6..22ad030c8e2 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/rest/FromRestDuplicateTest.java
@@ -22,7 +22,7 @@ import org.apache.camel.spi.Registry;
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 FromRestDuplicateTest extends ContextTestSupport {
@@ -39,30 +39,28 @@ public class FromRestDuplicateTest extends ContextTestSupport {
}
@Test
- public void testDuplicateGet() throws Exception {
- try {
+ public void testDuplicateGet() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
restConfiguration().host("localhost");
rest("/users").get("{id}").to("log:foo").post().to("log:foo").get("").to("log:foo").get("{id}")
.to("log:foo");
-
}
});
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- assertEquals("Duplicate verb detected in rest-dsl: get:{id}", e.getMessage());
- }
+ }, "Should throw exception");
+
+ assertEquals("Duplicate verb detected in rest-dsl: get:{id}", e.getMessage());
}
@Test
- public void testDuplicatePost() throws Exception {
- try {
+ public void testDuplicatePost() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
restConfiguration().host("localhost");
rest("/users").get("{id}").to("log:foo").post().to("log:foo").get("").to("log:foo").put().to("log:foo")
@@ -70,10 +68,9 @@ public class FromRestDuplicateTest extends ContextTestSupport {
}
});
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- assertEquals("Duplicate verb detected in rest-dsl: post", e.getMessage());
- }
+ }, "Should throw exception");
+
+ assertEquals("Duplicate verb detected in rest-dsl: post", e.getMessage());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/seda/SameSedaQueueMultipleConsumersDifferenceTest.java b/core/camel-core/src/test/java/org/apache/camel/component/seda/SameSedaQueueMultipleConsumersDifferenceTest.java
index 2219e8a0f52..a26fa0c9520 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/seda/SameSedaQueueMultipleConsumersDifferenceTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/seda/SameSedaQueueMultipleConsumersDifferenceTest.java
@@ -22,7 +22,7 @@ import org.apache.camel.builder.RouteBuilder;
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 SameSedaQueueMultipleConsumersDifferenceTest extends ContextTestSupport {
@@ -47,22 +47,21 @@ public class SameSedaQueueMultipleConsumersDifferenceTest extends ContextTestSup
}
@Test
- public void testAddConsumer() throws Exception {
- try {
+ public void testAddConsumer() {
+ Exception e = assertThrows(Exception.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
from("seda:foo").routeId("fail").to("mock:fail");
}
});
- fail("Should have thrown exception");
- } catch (Exception e) {
- FailedToStartRouteException failed = assertIsInstanceOf(FailedToStartRouteException.class, e);
- assertEquals("fail", failed.getRouteId());
- assertEquals(
- "Cannot use existing queue seda://foo as the existing queue multiple consumers true does not match given multiple consumers false",
- e.getCause().getMessage());
- }
+ }, "Should have thrown exception");
+
+ FailedToStartRouteException failed = assertIsInstanceOf(FailedToStartRouteException.class, e);
+ assertEquals("fail", failed.getRouteId());
+ assertEquals(
+ "Cannot use existing queue seda://foo as the existing queue multiple consumers true does not match given multiple consumers false",
+ e.getCause().getMessage());
}
@Override
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java b/core/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
index f4cb210937d..afa6fde531a 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/xslt/InvalidXsltFileTest.java
@@ -37,7 +37,7 @@ public class InvalidXsltFileTest extends TestSupport {
RouteBuilder builder = createRouteBuilder();
CamelContext context = new DefaultCamelContext();
context.addRoutes(builder);
- RuntimeCamelException exception = assertThrows(RuntimeCamelException.class, () -> context.start());
+ RuntimeCamelException exception = assertThrows(RuntimeCamelException.class, context::start);
assertIsInstanceOf(TransformerConfigurationException.class, exception.getCause().getCause().getCause());
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFeatureRouteTest.java b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFeatureRouteTest.java
index d5efa4e245a..3c98007a41d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFeatureRouteTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFeatureRouteTest.java
@@ -23,8 +23,8 @@ import org.apache.camel.ContextTestSupport;
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 XsltFeatureRouteTest extends ContextTestSupport {
@@ -36,17 +36,13 @@ public class XsltFeatureRouteTest extends ContextTestSupport {
}
public void sendXmlMessage(String uri, String message) {
- try {
- template.sendBody("direct:start1", message);
- fail("expect an exception here");
- } catch (Exception ex) {
- // expect an exception here
- boolean b1 = ex instanceof CamelExecutionException;
- assertTrue(b1, "Get a wrong exception");
- boolean b = ex.getCause() instanceof TransformerException;
- assertTrue(b, "Get a wrong exception cause");
- }
+ Exception ex = assertThrows(Exception.class, () -> template.sendBody("direct:start1", message), "Expected an exception here");
+ // expect an exception here
+ boolean b1 = ex instanceof CamelExecutionException;
+ assertTrue(b1, "Get a wrong exception");
+ boolean b = ex.getCause() instanceof TransformerException;
+ assertTrue(b, "Get a wrong exception cause");
}
@Override
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
index 863f14bbb39..c78e2ce80e9 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
@@ -39,7 +39,7 @@ public class XsltFileNotFoundTest extends TestSupport {
RouteBuilder builder = createRouteBuilder();
CamelContext context = new DefaultCamelContext();
context.addRoutes(builder);
- RuntimeCamelException exception = assertThrows(RuntimeCamelException.class, () -> context.start());
+ RuntimeCamelException exception = assertThrows(RuntimeCamelException.class, context::start);
assertIsInstanceOf(TransformerException.class, exception.getCause().getCause().getCause());
assertIsInstanceOf(FileNotFoundException.class, exception.getCause().getCause().getCause().getCause());
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
index 73e912bc595..531015ff78c 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/ConverterTest.java
@@ -234,7 +234,7 @@ public class ConverterTest extends TestSupport {
CamelContext camel = new DefaultCamelContext();
Exchange exchange = new DefaultExchange(camel);
- Exception ex = assertThrows(NoTypeConversionAvailableException.class,
+ assertThrows(NoTypeConversionAvailableException.class,
() -> converter.mandatoryConvertTo(InputStream.class, exchange),
"Expected to get a NoTypeConversionAvailableException here");
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/DurationConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/DurationConverterTest.java
index 1f79d1a4699..1799bc315f6 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/DurationConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/DurationConverterTest.java
@@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class DurationConverterTest extends ContextTestSupport {
@@ -39,14 +39,14 @@ public class DurationConverterTest extends ContextTestSupport {
}
@Test
- public void testToMillisOverflow() throws Exception {
+ public void testToMillisOverflow() {
Duration duration = Duration.parse("P60000000000000D");
- try {
- context.getTypeConverter().convertTo(long.class, duration);
- fail("Should throw exception");
- } catch (TypeConversionException e) {
- assertIsInstanceOf(ArithmeticException.class, e.getCause());
- }
+
+ TypeConversionException e = assertThrows(TypeConversionException.class,
+ () -> context.getTypeConverter().convertTo(long.class, duration),
+ "Should throw exception");
+
+ assertIsInstanceOf(ArithmeticException.class, e.getCause());
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java
index 070eb8b66b9..e33933308b7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java
@@ -27,7 +27,9 @@ import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
@Isolated
public class FutureConverterTest extends ContextTestSupport {
@@ -58,16 +60,13 @@ public class FutureConverterTest extends ContextTestSupport {
}
@Test
- public void testConvertMandatoryFutureWithExchangeFailed() throws Exception {
+ public void testConvertMandatoryFutureWithExchangeFailed() {
Exchange exchange = new DefaultExchange(context);
Future<?> future = template.asyncRequestBody("direct:foo", "Hello World");
- try {
- context.getTypeConverter().mandatoryConvertTo(Timestamp.class, exchange, future);
- fail("Should have thrown an exception");
- } catch (NoTypeConversionAvailableException e) {
- // expected
- }
+ assertThrows(NoTypeConversionAvailableException.class,
+ () -> context.getTypeConverter().mandatoryConvertTo(Timestamp.class, exchange, future),
+ "Should have thrown an exception");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java
index 2fd28666fc8..81e11e21dd9 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java
@@ -27,7 +27,9 @@ import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class InstanceFallbackConverterTest extends ContextTestSupport {
@@ -55,15 +57,12 @@ public class InstanceFallbackConverterTest extends ContextTestSupport {
}
@Test
- public void testInstanceFallbackMandatoryFailed() throws Exception {
+ public void testInstanceFallbackMandatoryFailed() {
Exchange exchange = new DefaultExchange(context);
- try {
- context.getTypeConverter().mandatoryConvertTo(Date.class, exchange, new Timestamp(0));
- fail("Should have thrown an exception");
- } catch (NoTypeConversionAvailableException e) {
- // expected
- }
+ assertThrows(NoTypeConversionAvailableException.class,
+ () -> context.getTypeConverter().mandatoryConvertTo(Date.class, exchange, new Timestamp(0)),
+ "Should have thrown an exception");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
index 8ccc76fa771..641b4ee0bf6 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/ObjectConverterTest.java
@@ -24,7 +24,12 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+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;
public class ObjectConverterTest {
@@ -168,7 +173,7 @@ public class ObjectConverterTest {
}
@Test
- public void testToBoolean() throws Exception {
+ public void testToBoolean() {
assertTrue(ObjectConverter.toBoolean("true"));
assertTrue(ObjectConverter.toBoolean("true".getBytes(StandardCharsets.UTF_8)));
assertTrue(ObjectConverter.toBoolean("TRUE"));
@@ -183,25 +188,11 @@ public class ObjectConverterTest {
assertTrue(ObjectConverter.toBool("TRUE"));
assertFalse(ObjectConverter.toBool("false"));
assertFalse(ObjectConverter.toBool("FALSE"));
- // primitive boolean is more strict
- try {
- ObjectConverter.toBool("1");
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- ObjectConverter.toBool("");
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
- ObjectConverter.toBool("yes");
- fail("Should throw exception");
- } catch (IllegalArgumentException e) {
- // expected
- }
+
+ // primitive boolean is stricter
+ assertThrows(IllegalArgumentException.class, () -> ObjectConverter.toBool("1"), "Should throw exception");
+ assertThrows(IllegalArgumentException.class, () -> ObjectConverter.toBool(""), "Should throw exception");
+ assertThrows(IllegalArgumentException.class, () -> ObjectConverter.toBool("yes"), "Should throw exception");
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
index 0b13f65e153..77dc1639d72 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
@@ -39,6 +39,7 @@ 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -175,13 +176,7 @@ public class CachedOutputStreamTest extends ContextTestSupport {
assertEquals(TEST_STRING, temp, "Cached a wrong file");
exchange.getUnitOfWork().done(exchange);
- try {
- cache.reset();
- // The stream is closed, so the temp file is gone.
- fail("we expect the exception here");
- } catch (Exception exception) {
- // do nothing
- }
+ assertThrows(Exception.class, cache::reset, "We expect the exception here");
files = file.list();
assertEquals(0, files.length, "we should have no temp file");
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
index 5d38ed01274..ff2ca038263 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java
@@ -36,7 +36,9 @@ import org.apache.camel.util.xml.StreamSourceConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Test cases for {@link StreamCacheConverter}
@@ -101,15 +103,11 @@ public class StreamCacheConverterTest extends ContextTestSupport {
InputStream is = getTestFileStream();
InputStream cache = (InputStream) StreamCacheConverter.convertToStreamCache(is, exchange);
assertNotNull(IOConverter.toString(cache, null));
- try {
- // since the stream is closed you delete the temp file
- // reset will not work any more
+
+ assertThrows(Exception.class, () -> {
cache.reset();
exchange.getUnitOfWork().done(exchange);
- fail("except the exception here");
- } catch (Exception exception) {
- // do nothing
- }
+ }, "We except the exception here");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteStartupOrderTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteStartupOrderTest.java
index f25f831c4db..263e45e5461 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteStartupOrderTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteStartupOrderTest.java
@@ -16,9 +16,12 @@
*/
package org.apache.camel.impl;
+import java.util.concurrent.TimeUnit;
+
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
+import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -45,9 +48,10 @@ public class DefaultCamelContextSuspendResumeRouteStartupOrderTest extends Conte
context.suspend();
// need to give seda consumer thread time to idle
- Thread.sleep(100);
+ Awaitility.await().atMost(100, TimeUnit.MILLISECONDS)
+ .pollDelay(10, TimeUnit.MILLISECONDS)
+ .untilAsserted(() -> template.sendBody("seda:foo", "B"));
- template.sendBody("seda:foo", "B");
mock.assertIsSatisfied(1000);
assertTrue(context.isSuspended());
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteTest.java
index b2fc4ee8a1e..6daf41f2ac1 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextSuspendResumeRouteTest.java
@@ -16,9 +16,12 @@
*/
package org.apache.camel.impl;
+import java.util.concurrent.TimeUnit;
+
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
+import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -46,9 +49,9 @@ public class DefaultCamelContextSuspendResumeRouteTest extends ContextTestSuppor
context.suspend();
// need to give seda consumer thread time to idle
- Thread.sleep(100);
-
- template.sendBody("seda:foo", "B");
+ Awaitility.await().atMost(100, TimeUnit.MILLISECONDS)
+ .pollDelay(10, TimeUnit.MILLISECONDS)
+ .untilAsserted(() -> template.sendBody("seda:foo", "B"));
mock.assertIsSatisfied(1000);
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultClassResolverTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultClassResolverTest.java
index 6d6ca0180d6..d3526b89a61 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultClassResolverTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultClassResolverTest.java
@@ -23,7 +23,7 @@ import org.apache.camel.impl.engine.DefaultClassResolver;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class DefaultClassResolverTest {
@@ -123,12 +123,10 @@ public class DefaultClassResolverTest {
@Test
public void testResolveMandatoryClassNotFound() {
DefaultClassResolver resolver = new DefaultClassResolver();
- try {
- resolver.resolveMandatoryClass("com.FooBar");
- fail("Should thrown an exception");
- } catch (ClassNotFoundException e) {
- // expected
- }
+
+ assertThrows(ClassNotFoundException.class,
+ () -> resolver.resolveMandatoryClass("com.FooBar"),
+ "Should thrown an exception");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java
index 7c39afab159..779707bc9ef 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java
@@ -36,7 +36,10 @@ import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+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;
/**
* Unit test for reference properties
@@ -191,25 +194,21 @@ public class DefaultComponentReferencePropertiesTest extends ContextTestSupport
}
@Test
- public void testTypoInParameter() throws Exception {
+ public void testTypoInParameter() {
MyComponent component = new MyComponent(context);
- try {
- component.createEndpoint("foo://?xxxexpression=#hello");
- fail("Should have throw a ResolveEndpointFailedException");
- } catch (ResolveEndpointFailedException e) {
- // ok
- }
+
+ assertThrows(ResolveEndpointFailedException.class,
+ () -> component.createEndpoint("foo://?xxxexpression=#hello"),
+ "Should have throw a ResolveEndpointFailedException");
}
@Test
- public void testTypoInParameterValue() throws Exception {
+ public void testTypoInParameterValue() {
MyComponent component = new MyComponent(context);
- try {
- component.createEndpoint("foo://?special=#dummy");
- fail("Should have throw a Exception");
- } catch (Exception e) {
- // ok
- }
+
+ assertThrows(Exception.class,
+ () -> component.createEndpoint("foo://?special=#dummy"),
+ "Should have throw a Exception");
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java
index 5d83975a11d..11d13841df9 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentTest.java
@@ -31,7 +31,10 @@ import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultComponent;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+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;
/**
* Unit test for helper methods on the DefaultComponent.
@@ -119,14 +122,15 @@ public class DefaultComponentTest extends ContextTestSupport {
Map<String, Object> parameters = new HashMap<>();
parameters.put("number", "#non-numeric");
MyComponent my = new MyComponent(this.context);
- try {
- my.resolveAndRemoveReferenceParameter(parameters, "number", Integer.class);
- } catch (TypeConversionException ex) {
- assertEquals(
- "Error during type conversion from type: java.lang.String " + "to the required type: java.lang.Integer "
- + "with value abc due to java.lang.NumberFormatException: For input string: \"abc\"",
- ex.getMessage());
- }
+
+ TypeConversionException ex = assertThrows(TypeConversionException.class,
+ () -> my.resolveAndRemoveReferenceParameter(parameters, "number", Integer.class),
+ "Should have thrown an exception");
+
+ assertEquals(
+ "Error during type conversion from type: java.lang.String " + "to the required type: java.lang.Integer "
+ + "with value abc due to java.lang.NumberFormatException: For input string: \"abc\"",
+ ex.getMessage());
}
@Test
@@ -134,12 +138,12 @@ public class DefaultComponentTest extends ContextTestSupport {
Map<String, Object> parameters = new HashMap<>();
parameters.put("date", "#somewhen");
MyComponent my = new MyComponent(this.context);
- try {
- my.resolveAndRemoveReferenceParameter(parameters, "date", Date.class);
- fail("returned without finding object in registry");
- } catch (NoSuchBeanException e) {
- assertEquals("No bean could be found in the registry for: somewhen of type: java.util.Date", e.getMessage());
- }
+
+ NoSuchBeanException e = assertThrows(NoSuchBeanException.class, () ->
+ my.resolveAndRemoveReferenceParameter(parameters, "date", Date.class),
+ "returned without finding object in registry");
+
+ assertEquals("No bean could be found in the registry for: somewhen of type: java.util.Date", e.getMessage());
}
@Test
@@ -226,12 +230,12 @@ public class DefaultComponentTest extends ContextTestSupport {
Map<String, Object> parameters = new HashMap<>();
parameters.put("dates", "#bean1,#bean3");
MyComponent my = new MyComponent(this.context);
- try {
- my.resolveAndRemoveReferenceListParameter(parameters, "dates", Date.class);
- fail("returned without finding object in registry");
- } catch (NoSuchBeanException e) {
- assertEquals("No bean could be found in the registry for: bean3 of type: java.util.Date", e.getMessage());
- }
+
+ NoSuchBeanException e = assertThrows(NoSuchBeanException.class, () ->
+ my.resolveAndRemoveReferenceListParameter(parameters, "dates", Date.class),
+ "returned without finding object in registry");
+
+ assertEquals("No bean could be found in the registry for: bean3 of type: java.util.Date", e.getMessage());
}
@Test
@@ -257,14 +261,13 @@ public class DefaultComponentTest extends ContextTestSupport {
}
@Test
- public void testContextShouldBeSet() throws Exception {
+ public void testContextShouldBeSet() {
MyComponent my = new MyComponent(null);
- try {
- my.start();
- fail("Should have thrown a IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("camelContext must be specified", e.getMessage());
- }
+
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, my::start,
+ "Should have thrown a IllegalArgumentException");
+
+ assertEquals("camelContext must be specified", e.getMessage());
}
@Override
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java
index 86ef98f004c..24b2c404a66 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentValidateURITest.java
@@ -22,7 +22,7 @@ import org.apache.camel.ResolveEndpointFailedException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Unit test for URI validation when creating an endpoint
@@ -36,33 +36,21 @@ public class DefaultComponentValidateURITest extends ContextTestSupport {
}
@Test
- public void testUnknownParameter() throws Exception {
- try {
- context.getEndpoint("timer://foo?delay=250&unknown=1&period=500");
- fail("Should have thrown ResolveEndpointFailedException");
- } catch (ResolveEndpointFailedException e) {
- // ok
- }
+ public void testUnknownParameter() {
+ assertThrows(ResolveEndpointFailedException.class, () -> context.getEndpoint("timer://foo?delay=250&unknown=1&period=500"),
+ "Should have thrown ResolveEndpointFailedException");
}
@Test
- public void testDoubleAmpersand() throws Exception {
- try {
- context.getEndpoint("timer://foo?delay=250&&period=500");
- fail("Should have thrown ResolveEndpointFailedException");
- } catch (ResolveEndpointFailedException e) {
- // ok
- }
+ public void testDoubleAmpersand() {
+ assertThrows(ResolveEndpointFailedException.class, () -> context.getEndpoint("timer://foo?delay=250&&period=500"),
+ "Should have thrown ResolveEndpointFailedException");
}
@Test
- public void testTrailingAmpersand() throws Exception {
- try {
- context.getEndpoint("timer://foo?delay=250&period=500&");
- fail("Should have thrown ResolveEndpointFailedException");
- } catch (ResolveEndpointFailedException e) {
- // ok
- }
+ public void testTrailingAmpersand() {
+ assertThrows(ResolveEndpointFailedException.class, () -> context.getEndpoint("timer://foo?delay=250&period=500&"),
+ "Should have thrown ResolveEndpointFailedException");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateWithCustomCacheMaxSizeTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateWithCustomCacheMaxSizeTest.java
index ea5b5ac5606..bcb2f544126 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateWithCustomCacheMaxSizeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultConsumerTemplateWithCustomCacheMaxSizeTest.java
@@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test;
import static org.awaitility.Awaitility.await;
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 DefaultConsumerTemplateWithCustomCacheMaxSizeTest extends ContextTestSupport {
@@ -64,22 +64,20 @@ public class DefaultConsumerTemplateWithCustomCacheMaxSizeTest extends ContextTe
@Test
public void testInvalidSizeABC() {
context.getGlobalOptions().put(Exchange.MAXIMUM_CACHE_POOL_SIZE, "ABC");
- try {
- context.createConsumerTemplate();
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: ABC", e.getCause().getMessage());
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.createConsumerTemplate(),
+ "Should have thrown an exception");
+
+ assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: ABC", e.getCause().getMessage());
}
@Test
public void testInvalidSizeZero() {
context.getGlobalOptions().put(Exchange.MAXIMUM_CACHE_POOL_SIZE, "0");
- try {
- context.createConsumerTemplate();
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: 0", e.getCause().getMessage());
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.createConsumerTemplate(),
+ "Should have thrown an exception");
+
+ assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: 0", e.getCause().getMessage());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultEndpointRegistryTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultEndpointRegistryTest.java
index 4bcae075a55..dc447376b47 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultEndpointRegistryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultEndpointRegistryTest.java
@@ -29,6 +29,7 @@ import org.apache.camel.spi.EndpointRegistry;
import org.apache.camel.support.NormalizedUri;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultEndpointRegistryTest {
@@ -114,7 +115,7 @@ public class DefaultEndpointRegistryTest {
allThreadCompletionSemaphore.await();
- assertTrue(endpointRegistry.values().toArray() != null);
+ assertNotNull(endpointRegistry.values().toArray());
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java
index fd6273b7591..93eb89aa9a7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeHolderTest.java
@@ -29,7 +29,10 @@ import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.DefaultExchangeHolder;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+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;
public class DefaultExchangeHolderTest extends ContextTestSupport {
@@ -138,16 +141,12 @@ public class DefaultExchangeHolderTest extends ContextTestSupport {
}
@Test
- public void testFileNotSupported() throws Exception {
+ public void testFileNotSupported() {
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody(new File("src/test/resources/log4j2.properties"));
- try {
- DefaultExchangeHolder.marshal(exchange);
- fail("Should have thrown exception");
- } catch (RuntimeExchangeException e) {
- // expected
- }
+ assertThrows(RuntimeExchangeException.class, () -> DefaultExchangeHolder.marshal(exchange),
+ "Should have thrown exception");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
index df59c67ac52..6147d9877ec 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
@@ -39,8 +39,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
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;
public class DefaultExchangeTest extends ExchangeTestSupport {
@@ -63,24 +63,18 @@ public class DefaultExchangeTest extends ExchangeTestSupport {
assertNotNull(exchange.getIn().getBody());
assertEquals("<hello id='m123'>world!</hello>", exchange.getIn().getBody());
- try {
- assertNull(exchange.getIn().getBody(Integer.class));
- fail("Should have thrown a TypeConversionException");
- } catch (TypeConversionException e) {
- // expected
- }
+
+ assertThrows(TypeConversionException.class, () -> assertNull(exchange.getIn().getBody(Integer.class)),
+ "Should have thrown a TypeConversionException");
assertEquals("<hello id='m123'>world!</hello>", exchange.getIn().getMandatoryBody());
- try {
- exchange.getIn().getMandatoryBody(Integer.class);
- fail("Should have thrown an InvalidPayloadException");
- } catch (InvalidPayloadException e) {
- // expected
- }
+
+ assertThrows(InvalidPayloadException.class, () -> exchange.getIn().getMandatoryBody(Integer.class),
+ "Should have thrown an InvalidPayloadException");
}
@Test
- public void testExceptionAsType() throws Exception {
+ public void testExceptionAsType() {
exchange.setException(
RuntimeCamelException.wrapRuntimeCamelException(new ConnectException("Cannot connect to remote server")));
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
index 52c4251f459..f66ebfc042d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
@@ -28,7 +28,14 @@ import org.apache.camel.util.concurrent.ThreadPoolRejectedPolicy;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+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.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultExecutorServiceManagerTest extends ContextTestSupport {
@@ -117,14 +124,13 @@ public class DefaultExecutorServiceManagerTest extends ContextTestSupport {
}
@Test
- public void testGetThreadNameCustomPatternInvalid() throws Exception {
+ public void testGetThreadNameCustomPatternInvalid() {
context.getExecutorServiceManager().setThreadNamePattern("Cool #xxx#");
- try {
- context.getExecutorServiceManager().resolveThreadName("foo");
- fail("Should thrown an exception");
- } catch (IllegalArgumentException e) {
- assertEquals("Pattern is invalid: [Cool #xxx#] in resolved thread name: [Cool #xxx#]", e.getMessage());
- }
+
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> context.getExecutorServiceManager().resolveThreadName("foo"),
+ "Should thrown an exception");
+
+ assertEquals("Pattern is invalid: [Cool #xxx#] in resolved thread name: [Cool #xxx#]", e.getMessage());
// reset it so we can shutdown properly
context.getExecutorServiceManager().setThreadNamePattern("Camel Thread #counter# - #name#");
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java
index ed09a391a2e..fe0559bc1af 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateAsyncTest.java
@@ -29,7 +29,9 @@ import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultProducerTemplateAsyncTest extends ContextTestSupport {
@@ -223,12 +225,10 @@ public class DefaultProducerTemplateAsyncTest extends ContextTestSupport {
String echo = template.requestBody("direct:echo", "Hi", String.class);
assertEquals("HiHi", echo);
- try {
- template.extractFutureBody(future, Exchange.class);
- fail("Should have thrown exception");
- } catch (RuntimeCamelException e) {
- assertEquals("Damn forced by unit test", e.getCause().getMessage());
- }
+ RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> template.extractFutureBody(future, Exchange.class),
+ "Should have thrown exception");
+
+ assertEquals("Damn forced by unit test", e.getCause().getMessage());
long delta = System.currentTimeMillis() - start;
assertTrue(delta > 50, "Should take longer than: " + delta);
@@ -243,12 +243,10 @@ public class DefaultProducerTemplateAsyncTest extends ContextTestSupport {
String echo = template.requestBody("direct:echo", "Hi", String.class);
assertEquals("HiHi", echo);
- try {
- template.extractFutureBody(future, String.class);
- fail("Should have thrown exception");
- } catch (RuntimeCamelException e) {
- assertEquals("Damn forced by unit test", e.getCause().getMessage());
- }
+ RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () -> template.extractFutureBody(future, String.class),
+ "Should have thrown exception");
+
+ assertEquals("Damn forced by unit test", e.getCause().getMessage());
long delta = System.currentTimeMillis() - start;
assertTrue(delta > 50, "Should take longer than: " + delta);
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateTest.java
index a25b2d8819f..f6552c38caa 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateTest.java
@@ -33,7 +33,10 @@ import org.apache.camel.impl.engine.DefaultProducerTemplate;
import org.junit.jupiter.api.Test;
import static org.awaitility.Awaitility.await;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for DefaultProducerTemplate
@@ -87,15 +90,12 @@ public class DefaultProducerTemplateTest extends ContextTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
- try {
- template.sendBody("direct:exception", "Hello World");
- fail("Should have thrown RuntimeCamelException");
- } catch (RuntimeCamelException e) {
- boolean b = e.getCause() instanceof IllegalArgumentException;
- assertTrue(b);
- assertEquals("Forced exception by unit test", e.getCause().getMessage());
- }
+ RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () ->
+ template.sendBody("direct:exception", "Hello World"),
+ "Should have thrown RuntimeCamelException");
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced exception by unit test", e.getCause().getMessage());
assertMockEndpointsSatisfied();
}
@@ -104,15 +104,12 @@ public class DefaultProducerTemplateTest extends ContextTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
- try {
- template.requestBody("direct:exception", "Hello World", Integer.class);
- fail("Should have thrown RuntimeCamelException");
- } catch (RuntimeCamelException e) {
- boolean b = e.getCause() instanceof IllegalArgumentException;
- assertTrue(b);
- assertEquals("Forced exception by unit test", e.getCause().getMessage());
- }
+ RuntimeCamelException e = assertThrows(RuntimeCamelException.class, () ->
+ template.requestBody("direct:exception", "Hello World", Integer.class),
+ "Should have thrown RuntimeCamelException");
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced exception by unit test", e.getCause().getMessage());
assertMockEndpointsSatisfied();
}
@@ -154,15 +151,12 @@ public class DefaultProducerTemplateTest extends ContextTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
- try {
- template.requestBody("direct:exception", "Hello World");
- fail("Should have thrown RuntimeCamelException");
- } catch (RuntimeCamelException e) {
- boolean b = e.getCause() instanceof IllegalArgumentException;
- assertTrue(b);
- assertEquals("Forced exception by unit test", e.getCause().getMessage());
- }
+ RuntimeCamelException e = assertThrows(RuntimeCamelException.class,
+ () -> template.requestBody("direct:exception", "Hello World"),
+ "Should have thrown RuntimeCamelException");
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Forced exception by unit test", e.getCause().getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateWithCustomCacheMaxSizeTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateWithCustomCacheMaxSizeTest.java
index 2aa9d90999f..f90cc39f6d8 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateWithCustomCacheMaxSizeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultProducerTemplateWithCustomCacheMaxSizeTest.java
@@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test;
import static org.awaitility.Awaitility.await;
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 DefaultProducerTemplateWithCustomCacheMaxSizeTest extends ContextTestSupport {
@@ -63,22 +63,20 @@ public class DefaultProducerTemplateWithCustomCacheMaxSizeTest extends ContextTe
@Test
public void testInvalidSizeABC() {
context.getGlobalOptions().put(Exchange.MAXIMUM_CACHE_POOL_SIZE, "ABC");
- try {
- context.createProducerTemplate();
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: ABC", e.getCause().getMessage());
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.createProducerTemplate(),
+ "Should have thrown an exception");
+
+ assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: ABC", e.getCause().getMessage());
}
@Test
public void testInvalidSizeZero() {
context.getGlobalOptions().put(Exchange.MAXIMUM_CACHE_POOL_SIZE, "0");
- try {
- context.createProducerTemplate();
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: 0", e.getCause().getMessage());
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.createProducerTemplate(),
+ "Should have thrown an exception");
+
+ assertEquals("Property CamelMaximumCachePoolSize must be a positive number, was: 0", e.getCause().getMessage());;
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/EventDrivenPollingConsumerQueueSizeTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/EventDrivenPollingConsumerQueueSizeTest.java
index b3badbe81e5..11696379e07 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/EventDrivenPollingConsumerQueueSizeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/EventDrivenPollingConsumerQueueSizeTest.java
@@ -35,7 +35,10 @@ import org.apache.camel.support.service.ServiceHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+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.assertThrows;
public class EventDrivenPollingConsumerQueueSizeTest extends ContextTestSupport {
@@ -68,13 +71,10 @@ public class EventDrivenPollingConsumerQueueSizeTest extends ContextTestSupport
assertEquals(10, edpc.getQueueSize());
- try {
- template.sendBody(uri, "Message 10");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- // queue should be full
- assertIsInstanceOf(IllegalStateException.class, e.getCause());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class, () -> template.sendBody(uri, "Message 10"),
+ "Should have thrown exception");
+
+ assertIsInstanceOf(IllegalStateException.class, e.getCause());
Exchange out = consumer.receive(5000);
assertNotNull(out);
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/FromHasNoOutputRouteTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/FromHasNoOutputRouteTest.java
index 42e8f4c2ac6..daecb8a31be 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/FromHasNoOutputRouteTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/FromHasNoOutputRouteTest.java
@@ -21,8 +21,8 @@ import org.apache.camel.FailedToCreateRouteException;
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 FromHasNoOutputRouteTest extends ContextTestSupport {
@@ -40,16 +40,14 @@ public class FromHasNoOutputRouteTest extends ContextTestSupport {
from("direct:start");
}
});
- try {
- context.start();
- fail("Should throw exception");
- } catch (Exception e) {
- FailedToCreateRouteException failed = assertIsInstanceOf(FailedToCreateRouteException.class, e);
- assertTrue(failed.getRouteId().matches("route[0-9]+"));
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertTrue(cause.getMessage().matches(
- "Route route[0-9]+\\Q has no output processors. You need to add outputs to the route such as to(\"log:foo\").\\E"));
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.start(), "Should throw exception");
+
+ FailedToCreateRouteException failed = assertIsInstanceOf(FailedToCreateRouteException.class, e);
+ assertTrue(failed.getRouteId().matches("route[0-9]+"));
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertTrue(cause.getMessage().matches(
+ "Route route[0-9]+\\Q has no output processors. You need to add outputs to the route such as to(\"log:foo\").\\E"));
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/GracefulShutdownNoAutoStartOrderClashTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/GracefulShutdownNoAutoStartOrderClashTest.java
index 1d1a1a359c0..c6a27b949cb 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/GracefulShutdownNoAutoStartOrderClashTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/GracefulShutdownNoAutoStartOrderClashTest.java
@@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteBuilder;
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 GracefulShutdownNoAutoStartOrderClashTest extends ContextTestSupport {
@@ -39,15 +39,13 @@ public class GracefulShutdownNoAutoStartOrderClashTest extends ContextTestSuppor
from("direct:bar").routeId("bar").startupOrder(5).noAutoStartup().to("mock:bar");
}
});
- try {
- context.start();
- fail("Should have thrown an exception");
- } catch (Exception e) {
- assertEquals(
- "Failed to start route bar because of startupOrder clash. Route foo already has startupOrder 5 configured"
- + " which this route have as well. Please correct startupOrder to be unique among all your routes.",
- e.getMessage());
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.start(), "Should have thrown an exception");
+
+ assertEquals(
+ "Failed to start route bar because of startupOrder clash. Route foo already has startupOrder 5 configured"
+ + " which this route have as well. Please correct startupOrder to be unique among all your routes.",
+ e.getMessage());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyFailOnStartupTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyFailOnStartupTest.java
index 9939f0b2c7c..4565cd74637 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyFailOnStartupTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyFailOnStartupTest.java
@@ -22,7 +22,7 @@ import org.apache.camel.VetoCamelContextStartException;
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 LifecycleStrategyFailOnStartupTest extends TestSupport {
@@ -37,12 +37,8 @@ public class LifecycleStrategyFailOnStartupTest extends TestSupport {
@Test
public void testLifecycleStrategyFailOnStartup() throws Exception {
CamelContext context = createCamelContext();
- try {
- context.start();
- fail("Should have thrown exception");
- } catch (Exception e) {
- assertEquals("Forced", e.getMessage());
- }
+ Exception e = assertThrows(Exception.class, context::start, "Should have thrown exception");
+ assertEquals("Forced", e.getMessage());
}
private static class MyLifecycleStrategy extends DummyLifecycleStrategy {
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/MultipleConsumersSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/MultipleConsumersSupportTest.java
index c0fae923f3d..a58d60536ff 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/MultipleConsumersSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/MultipleConsumersSupportTest.java
@@ -26,8 +26,8 @@ import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.support.DefaultEndpoint;
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 MultipleConsumersSupportTest extends ContextTestSupport {
@@ -52,12 +52,9 @@ public class MultipleConsumersSupportTest extends ContextTestSupport {
from(my).to("mock:b");
}
});
- try {
- context.start();
- fail("Should have thrown exception");
- } catch (Exception e) {
- assertTrue(e.getMessage().endsWith("Multiple consumers for the same endpoint is not allowed: my:endpoint"));
- }
+
+ Exception e = assertThrows(Exception.class, () -> context.start(), "Should have thrown exception");
+ assertTrue(e.getMessage().endsWith("Multiple consumers for the same endpoint is not allowed: my:endpoint"));
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/RouteDirectSuspendResumeTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/RouteDirectSuspendResumeTest.java
index 9b9fb634558..30a5d8b45b4 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/RouteDirectSuspendResumeTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/RouteDirectSuspendResumeTest.java
@@ -23,7 +23,7 @@ import org.apache.camel.component.mock.MockEndpoint;
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 RouteDirectSuspendResumeTest extends ContextTestSupport {
@@ -46,12 +46,8 @@ public class RouteDirectSuspendResumeTest extends ContextTestSupport {
// direct consumer supports suspension
assertEquals("Suspended", context.getRouteController().getRouteStatus("foo").name());
- try {
- template.sendBody("direct:foo", "B");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class, () -> template.sendBody("direct:foo", "B"),
+ "Should have thrown an exception");
log.info("Resuming");
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/RouteMustHaveOutputOnExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/RouteMustHaveOutputOnExceptionTest.java
index 2c32cb21a35..939c8876209 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/RouteMustHaveOutputOnExceptionTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/RouteMustHaveOutputOnExceptionTest.java
@@ -20,7 +20,7 @@ import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class RouteMustHaveOutputOnExceptionTest extends ContextTestSupport {
@@ -54,12 +54,9 @@ public class RouteMustHaveOutputOnExceptionTest extends ContextTestSupport {
.end().to("mock:result");
}
});
- try {
- context.start();
- fail("Should have thrown an exception");
- } catch (Exception e) {
- // expected
- }
+
+ assertThrows(Exception.class, () -> context.start(),
+ "Should have thrown an exception");
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/RouteNoOutputTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/RouteNoOutputTest.java
index 124aac3cc19..28bf805e146 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/RouteNoOutputTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/RouteNoOutputTest.java
@@ -22,24 +22,23 @@ import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.BeforeEach;
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 RouteNoOutputTest extends ContextTestSupport {
@Override
@BeforeEach
public void setUp() throws Exception {
- try {
- super.setUp();
- fail("Should have thrown exception");
- } catch (Exception e) {
- FailedToCreateRouteException failed = assertIsInstanceOf(FailedToCreateRouteException.class, e);
- assertTrue(failed.getRouteId().matches("route[0-9]+"));
- assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertTrue(e.getCause().getMessage().matches(
- "Route route[0-9]+\\Q has no output processors. You need to add outputs to the route such as to(\"log:foo\").\\E"));
- }
+
+ Exception e = assertThrows(Exception.class, super::setUp,
+ "Should have thrown exception");
+
+ FailedToCreateRouteException failed = assertIsInstanceOf(FailedToCreateRouteException.class, e);
+ assertTrue(failed.getRouteId().matches("route[0-9]+"));
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertTrue(e.getCause().getMessage().matches(
+ "Route route[0-9]+\\Q has no output processors. You need to add outputs to the route such as to(\"log:foo\").\\E"));
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/RouteSetRouteIdTwoTimesTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/RouteSetRouteIdTwoTimesTest.java
index dbdb824e78d..a4a908682b8 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/RouteSetRouteIdTwoTimesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/RouteSetRouteIdTwoTimesTest.java
@@ -21,24 +21,22 @@ import org.apache.camel.TestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class RouteSetRouteIdTwoTimesTest extends TestSupport {
@Test
- public void testRouteIdTwice() throws Exception {
+ public void testRouteIdTwice() {
CamelContext context = new DefaultCamelContext();
- try {
+
+ assertThrows(IllegalArgumentException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:hello").routeId("foo").to("mock:result").to("mock:bar").routeId("bar");
}
});
- fail("Should have thrown exception");
- } catch (IllegalArgumentException e) {
- // expected
- }
+ }, "Should have thrown exception");
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
index 2c6c482a21d..4f493d7598e 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
@@ -23,7 +23,7 @@ import org.apache.camel.TestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Testing for mistyped component name
@@ -31,9 +31,9 @@ import static org.junit.jupiter.api.Assertions.fail;
public class RouteWithMistypedComponentNameTest extends TestSupport {
@Test
- public void testNoSuchEndpoint() throws Exception {
+ public void testNoSuchEndpoint() {
CamelContext context = new DefaultCamelContext();
- try {
+ assertThrows(NoSuchEndpointException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
@@ -43,16 +43,14 @@ public class RouteWithMistypedComponentNameTest extends TestSupport {
endpoint("mistyped:hello");
}
});
- fail("Should have thrown a NoSuchEndpointException");
- } catch (NoSuchEndpointException e) {
- // expected
- }
+ }, "Should have thrown a NoSuchEndpointException");
}
@Test
- public void testNoSuchEndpointType() throws Exception {
+ public void testNoSuchEndpointType() {
CamelContext context = new DefaultCamelContext();
- try {
+
+ assertThrows(NoSuchEndpointException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
@@ -62,9 +60,6 @@ public class RouteWithMistypedComponentNameTest extends TestSupport {
endpoint("mistyped:hello", Endpoint.class);
}
});
- fail("Should have thrown a NoSuchEndpointException");
- } catch (NoSuchEndpointException e) {
- // expected
- }
+ }, "Should have thrown a NoSuchEndpointException");
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledNoStreamCachingTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledNoStreamCachingTest.java
index 616fcab5ca9..f8a0adad8ec 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledNoStreamCachingTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledNoStreamCachingTest.java
@@ -23,8 +23,8 @@ import org.apache.camel.spi.TypeConverterRegistry;
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;
public class TypeConverterRegistryStatisticsEnabledNoStreamCachingTest extends ContextTestSupport {
@@ -53,12 +53,8 @@ public class TypeConverterRegistryStatisticsEnabledNoStreamCachingTest extends C
Long miss = reg.getStatistics().getMissCounter();
assertEquals(0, miss.intValue());
- try {
- template.sendBody("direct:start", "foo");
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class, () -> template.sendBody("direct:start", "foo"),
+ "Should have thrown exception");
// should now have a failed
failed = reg.getStatistics().getFailedCounter();
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledTest.java
index ab6fb661a76..6c2549323f6 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/TypeConverterRegistryStatisticsEnabledTest.java
@@ -22,7 +22,9 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.TypeConverterRegistry;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
public class TypeConverterRegistryStatisticsEnabledTest extends ContextTestSupport {
@@ -50,12 +52,8 @@ public class TypeConverterRegistryStatisticsEnabledTest extends ContextTestSuppo
Long miss = reg.getStatistics().getMissCounter();
assertEquals(4, miss.intValue()); // stream caching misses
- try {
- template.sendBody("direct:start", "foo");
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class, () -> template.sendBody("direct:start", "foo"),
+ "Should have thrown exception");
// should now have a failed
failed = reg.getStatistics().getFailedCounter();
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
index 1de5a3a9e8d..39dd0f0e2b3 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
@@ -43,7 +43,14 @@ import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.URISupport;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.*;
+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.assertNull;
+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;
public class DefaultCamelContextTest extends TestSupport {
@@ -118,18 +125,13 @@ public class DefaultCamelContextTest extends TestSupport {
}
@Test
- public void testGetEndpoint() throws Exception {
+ public void testGetEndpoint() {
DefaultCamelContext ctx = new DefaultCamelContext(false);
ctx.disableJMX();
Endpoint endpoint = ctx.getEndpoint("log:foo");
assertNotNull(endpoint);
- try {
- ctx.getEndpoint((String) null);
- fail("Should have thrown exception");
- } catch (IllegalArgumentException e) {
- // expected
- }
+ assertThrows(IllegalArgumentException.class, () -> ctx.getEndpoint(null), "Should have thrown exception");
}
@Test
@@ -175,27 +177,25 @@ public class DefaultCamelContextTest extends TestSupport {
}
@Test
- public void testGetEndpointNotFound() throws Exception {
+ public void testGetEndpointNotFound() {
DefaultCamelContext ctx = new DefaultCamelContext(false);
ctx.disableJMX();
- try {
- ctx.getEndpoint("xxx:foo");
- fail("Should have thrown a ResolveEndpointFailedException");
- } catch (NoSuchEndpointException e) {
- assertTrue(e.getMessage().contains("No endpoint could be found for: xxx:"));
- }
+
+ NoSuchEndpointException e = assertThrows(NoSuchEndpointException.class,
+ () -> ctx.getEndpoint("xxx:foo"),
+ "Should have thrown a ResolveEndpointFailedException");
+
+ assertTrue(e.getMessage().contains("No endpoint could be found for: xxx:"));
}
@Test
- public void testGetEndpointUnknownComponentNoScheme() throws Exception {
+ public void testGetEndpointUnknownComponentNoScheme() {
DefaultCamelContext ctx = new DefaultCamelContext(false);
ctx.disableJMX();
- try {
- CamelContextHelper.getMandatoryEndpoint(ctx, "unknownname");
- fail("Should have thrown a NoSuchEndpointException");
- } catch (NoSuchEndpointException e) {
- // expected
- }
+
+ assertThrows(NoSuchEndpointException.class,
+ () -> CamelContextHelper.getMandatoryEndpoint(ctx, "unknownname"),
+ "Should have thrown a NoSuchEndpointException");
}
@Test
@@ -258,17 +258,15 @@ public class DefaultCamelContextTest extends TestSupport {
LogComponent log = ctx.getComponent("log", LogComponent.class);
assertNotNull(log);
- try {
+
+ assertThrows(IllegalArgumentException.class, () -> {
ctx.addComponent("direct", new DirectComponent());
ctx.getComponent("log", DirectComponent.class);
- fail("Should have thrown exception");
- } catch (IllegalArgumentException e) {
- // expected
- }
+ }, "Should have thrown exception");
}
@Test
- public void testHasEndpoint() throws Exception {
+ public void testHasEndpoint() {
DefaultCamelContext ctx = new DefaultCamelContext(false);
ctx.disableJMX();
ctx.getEndpoint("mock://foo");
@@ -279,12 +277,8 @@ public class DefaultCamelContextTest extends TestSupport {
EndpointRegistry map = ctx.getEndpointRegistry();
assertEquals(1, map.size());
- try {
- ctx.hasEndpoint((String) null);
- fail("Should have thrown exception");
- } catch (ResolveEndpointFailedException e) {
- // expected
- }
+ assertThrows(ResolveEndpointFailedException.class, () -> ctx.hasEndpoint(null),
+ "Should have thrown exception");
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java
index 10b7e793865..524da6ed9a7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/IntrospectionSupportTest.java
@@ -39,7 +39,13 @@ import static org.apache.camel.impl.engine.IntrospectionSupport.getPropertySette
import static org.apache.camel.impl.engine.IntrospectionSupport.isGetter;
import static org.apache.camel.impl.engine.IntrospectionSupport.isSetter;
import static org.apache.camel.impl.engine.IntrospectionSupport.setProperty;
-import static org.junit.jupiter.api.Assertions.*;
+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.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for IntrospectionSupport
@@ -417,12 +423,11 @@ public class IntrospectionSupportTest extends ContextTestSupport {
Method name = getPropertyGetter(ExampleBean.class, "name");
assertEquals("getName", name.getName());
- try {
- getPropertyGetter(ExampleBean.class, "xxx");
- fail("Should have thrown exception");
- } catch (NoSuchMethodException e) {
- assertEquals("org.apache.camel.support.jndi.ExampleBean.getXxx()", e.getMessage());
- }
+ NoSuchMethodException e = assertThrows(NoSuchMethodException.class,
+ () -> getPropertyGetter(ExampleBean.class, "xxx"),
+ "Should have thrown exception");
+
+ assertEquals("org.apache.camel.support.jndi.ExampleBean.getXxx()", e.getMessage());
}
@Test
@@ -434,12 +439,11 @@ public class IntrospectionSupportTest extends ContextTestSupport {
Method name = getPropertySetter(ExampleBean.class, "name");
assertEquals("setName", name.getName());
- try {
- getPropertySetter(ExampleBean.class, "xxx");
- fail("Should have thrown exception");
- } catch (NoSuchMethodException e) {
- assertEquals("org.apache.camel.support.jndi.ExampleBean.setXxx", e.getMessage());
- }
+ NoSuchMethodException e = assertThrows(NoSuchMethodException.class,
+ () -> getPropertySetter(ExampleBean.class, "xxx"),
+ "Should have thrown exception");
+
+ assertEquals("org.apache.camel.support.jndi.ExampleBean.setXxx", e.getMessage());
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/event/SimpleEventNotifierEventsTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/event/SimpleEventNotifierEventsTest.java
index 7e6077ef447..0168da04c63 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/event/SimpleEventNotifierEventsTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/event/SimpleEventNotifierEventsTest.java
@@ -31,7 +31,9 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.TestSupport.assertIsInstanceOf;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class SimpleEventNotifierEventsTest {
@@ -111,13 +113,11 @@ public class SimpleEventNotifierEventsTest {
// optimized as this does not require exchange events
assertFalse(context.getCamelContextExtension().isEventNotificationApplicable());
- try {
- template.sendBody("direct:fail", "Hello World");
- fail("Should have thrown an exception");
- } catch (Exception e) {
- // expected
- assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:fail", "Hello World"),
+ "Should have thrown an exception");
+
+ assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
assertEquals(12, events.size());
assertIsInstanceOf(CamelEvent.CamelContextInitializingEvent.class, events.get(0));
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithErrorHandlerRemoveTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithErrorHandlerRemoveTest.java
index 9f768f2de4a..54c9413d7ed 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithErrorHandlerRemoveTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithErrorHandlerRemoveTest.java
@@ -23,7 +23,7 @@ import org.apache.camel.builder.RouteBuilder;
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 AdviceWithErrorHandlerRemoveTest extends ContextTestSupport {
@@ -51,12 +51,10 @@ public class AdviceWithErrorHandlerRemoveTest extends ContextTestSupport {
context.start();
- try {
- template.sendBody("direct:foo", "Hello World");
- fail("Should throw exception");
- } catch (Exception e) {
- assertEquals("Forced", e.getCause().getMessage());
- }
+ Exception e = assertThrows(Exception.class, () -> template.sendBody("direct:foo", "Hello World"),
+ "Should throw exception");
+
+ assertEquals("Forced", e.getCause().getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithInvalidConfiguredTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithInvalidConfiguredTest.java
index 3255093898b..09e72e49f81 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithInvalidConfiguredTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithInvalidConfiguredTest.java
@@ -23,7 +23,7 @@ import org.apache.camel.builder.RouteBuilder;
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;
/**
*
@@ -31,8 +31,8 @@ import static org.junit.jupiter.api.Assertions.fail;
public class AdviceWithInvalidConfiguredTest extends ContextTestSupport {
@Test
- public void testNoErrorHandler() throws Exception {
- try {
+ public void testNoErrorHandler() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
AdviceWith.adviceWith(context.getRouteDefinition("route-a"), context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
@@ -42,29 +42,27 @@ public class AdviceWithInvalidConfiguredTest extends ContextTestSupport {
.throwException(new IllegalArgumentException("Forced"));
}
});
- fail("Should have thrown an exception");
- } catch (IllegalArgumentException e) {
- assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.",
- e.getMessage());
- }
+ },"Should have thrown an exception");
+
+ assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.",
+ e.getMessage());
}
@Test
- public void testNoExtraRoutes() throws Exception {
- try {
+ public void testNoExtraRoutes() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
AdviceWith.adviceWith(context.getRouteDefinition("route-a"), context, new AdviceWithRouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
from("direct:foo").to("mock:foo");
}
});
- fail("Should have thrown an exception");
- } catch (IllegalArgumentException e) {
- assertEquals(
- "You can only advice from a RouteBuilder which has no existing routes. Remove all routes from the route builder.",
- e.getMessage());
- }
+ }, "Should have thrown an exception");
+
+ assertEquals(
+ "You can only advice from a RouteBuilder which has no existing routes. Remove all routes from the route builder.",
+ e.getMessage());
}
@Override
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java
index dace204b634..d7f59ba8cca 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java
@@ -26,7 +26,7 @@ import org.apache.camel.model.RouteDefinition;
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 AdviceWithIssueTest extends ContextTestSupport {
@@ -35,31 +35,27 @@ public class AdviceWithIssueTest extends ContextTestSupport {
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
template.sendBody("direct:start", "World");
- 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();
}
@Test
- public void testAdviceWithErrorHandler() throws Exception {
+ public void testAdviceWithErrorHandler() {
RouteDefinition route = context.getRouteDefinitions().get(0);
- try {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
AdviceWith.adviceWith(route, context, new AdviceWithRouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
errorHandler(deadLetterChannel("mock:dead"));
}
});
- fail("Should have thrown exception");
- } catch (IllegalArgumentException e) {
- assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.",
- e.getMessage());
- }
+ }, "Should have thrown exception");
+
+ assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.",
+ e.getMessage());
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnExceptionRemoveTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnExceptionRemoveTest.java
index ae88300d8fd..ca5f6646ae8 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnExceptionRemoveTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnExceptionRemoveTest.java
@@ -23,7 +23,7 @@ import org.apache.camel.builder.RouteBuilder;
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 AdviceWithOnExceptionRemoveTest extends ContextTestSupport {
@@ -51,12 +51,10 @@ public class AdviceWithOnExceptionRemoveTest extends ContextTestSupport {
context.start();
- try {
- template.sendBody("direct:foo", "Hello World");
- fail("Should throw exception");
- } catch (Exception e) {
- assertEquals("Forced", e.getCause().getMessage());
- }
+ Exception e = assertThrows(Exception.class, () -> template.sendBody("direct:foo", "Hello World"),
+ "Should throw exception");
+
+ assertEquals("Forced", e.getCause().getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/CharlesSplitAndTryCatchRollbackIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/CharlesSplitAndTryCatchRollbackIssueTest.java
index bd02463bd6f..268cc1eaa85 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/CharlesSplitAndTryCatchRollbackIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/CharlesSplitAndTryCatchRollbackIssueTest.java
@@ -26,8 +26,8 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
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 CharlesSplitAndTryCatchRollbackIssueTest extends ContextTestSupport {
@@ -71,15 +71,14 @@ public class CharlesSplitAndTryCatchRollbackIssueTest extends ContextTestSupport
ile.expectedMessageCount(0);
exception.expectedMessageCount(1);
- try {
- template.sendBody("direct:start", "A,B,Kaboom,C");
- fail("Should thrown an exception");
- } catch (CamelExecutionException e) {
- CamelExchangeException ee = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
- assertTrue(ee.getMessage().startsWith("Multicast processing failed for number 2."));
- RollbackExchangeException re = assertIsInstanceOf(RollbackExchangeException.class, ee.getCause());
- assertTrue(re.getMessage().startsWith("Intended rollback"));
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "A,B,Kaboom,C"),
+ "Should thrown an exception");
+
+ CamelExchangeException ee = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
+ assertTrue(ee.getMessage().startsWith("Multicast processing failed for number 2."));
+ RollbackExchangeException re = assertIsInstanceOf(RollbackExchangeException.class, ee.getCause());
+ assertTrue(re.getMessage().startsWith("Intended rollback"));
assertMockEndpointsSatisfied();
}
@@ -94,15 +93,14 @@ public class CharlesSplitAndTryCatchRollbackIssueTest extends ContextTestSupport
ile.expectedMessageCount(1);
exception.expectedMessageCount(1);
- try {
- template.sendBody("direct:start", "A,Forced,B,Kaboom,C");
- fail("Should thrown an exception");
- } catch (CamelExecutionException e) {
- CamelExchangeException ee = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
- assertTrue(ee.getMessage().startsWith("Multicast processing failed for number 3."));
- RollbackExchangeException re = assertIsInstanceOf(RollbackExchangeException.class, ee.getCause());
- assertTrue(re.getMessage().startsWith("Intended rollback"));
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "A,Forced,B,Kaboom,C"),
+ "Should thrown an exception");
+
+ CamelExchangeException ee = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
+ assertTrue(ee.getMessage().startsWith("Multicast processing failed for number 3."));
+ RollbackExchangeException re = assertIsInstanceOf(RollbackExchangeException.class, ee.getCause());
+ assertTrue(re.getMessage().startsWith("Intended rollback"));
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ChoiceEndOrEndChoiceIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ChoiceEndOrEndChoiceIssueTest.java
index c2a9ae0afcc..31d0d5016bf 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ChoiceEndOrEndChoiceIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ChoiceEndOrEndChoiceIssueTest.java
@@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteBuilder;
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 ChoiceEndOrEndChoiceIssueTest extends ContextTestSupport {
@@ -31,26 +31,24 @@ public class ChoiceEndOrEndChoiceIssueTest extends ContextTestSupport {
}
@Test
- public void testEndChoiceInvalid() throws Exception {
- try {
+ public void testEndChoiceInvalid() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> {
context.addRoutes(new RouteBuilder() {
@Override
- public void configure() throws Exception {
+ public void configure() {
from("direct:start")
.choice()
- .when(header("number").isEqualTo("one")).to("mock:one")
- .when(header("number").isEqualTo("two")).to("mock:two")
- .when(header("number").isEqualTo("three")).to("mock:three").endChoice()
+ .when(header("number").isEqualTo("one")).to("mock:one")
+ .when(header("number").isEqualTo("two")).to("mock:two")
+ .when(header("number").isEqualTo("three")).to("mock:three").endChoice()
.to("mock:finally");
}
});
- context.start();
- fail("Should have thrown exception");
- } catch (IllegalArgumentException e) {
- assertEquals("A new choice clause should start with a when() or otherwise()."
- + " If you intend to end the entire choice and are using endChoice() then use end() instead.",
- e.getMessage());
- }
+ }, "Should have thrown exception");
+
+ assertEquals("A new choice clause should start with a when() or otherwise()."
+ + " If you intend to end the entire choice and are using endChoice() then use end() instead.",
+ e.getMessage());
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTest.java
index 9ca6d5318a9..6d4d0d42186 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTest.java
@@ -24,7 +24,7 @@ import org.apache.camel.spi.Registry;
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;
/**
*
@@ -37,13 +37,12 @@ public class ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTest extends
getMockEndpoint("mock:handled").expectedMessageCount(1);
getMockEndpoint("mock:dead").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Damn", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Damn", cause.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest.java
index 0e1150a656d..69c90a620a3 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest.java
@@ -26,7 +26,7 @@ import org.apache.camel.spi.Registry;
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;
/**
*
@@ -39,13 +39,12 @@ public class ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest
getMockEndpoint("mock:handled").expectedMessageCount(1);
getMockEndpoint("mock:dead").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Damn", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Damn", cause.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTest.java
index 537a1e4bc69..6523ed2c698 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTest.java
@@ -25,7 +25,7 @@ import org.apache.camel.spi.Registry;
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;
/**
*
@@ -38,13 +38,12 @@ public class ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTe
getMockEndpoint("mock:handled").expectedMessageCount(1);
getMockEndpoint("mock:dead").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Damn", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Damn", cause.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTwoRoutesTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTwoRoutesTest.java
index 516954788c6..0ea8fc8b6d8 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTwoRoutesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTwoRoutesTest.java
@@ -26,7 +26,7 @@ import org.apache.camel.spi.Registry;
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;
/**
*
@@ -39,13 +39,12 @@ public class ContextScopedOnExceptionNotHandledRouteScopedErrorHandlerRefIssueTw
getMockEndpoint("mock:handled").expectedMessageCount(1);
getMockEndpoint("mock:dead").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Damn", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Damn", cause.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ErrorHandlerOnRedeliveryStopTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ErrorHandlerOnRedeliveryStopTest.java
index a3b1ee91b03..dc748f0d6ca 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ErrorHandlerOnRedeliveryStopTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ErrorHandlerOnRedeliveryStopTest.java
@@ -27,7 +27,7 @@ import org.apache.camel.support.ExpressionAdapter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class ErrorHandlerOnRedeliveryStopTest extends ContextTestSupport {
@@ -37,13 +37,12 @@ public class ErrorHandlerOnRedeliveryStopTest extends ContextTestSupport {
public void testRetryWhile() throws Exception {
getMockEndpoint("mock:result").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should throw an exception");
- } catch (Exception e) {
- RejectedExecutionException ree = assertIsInstanceOf(RejectedExecutionException.class, e.getCause());
- Assertions.assertEquals("I do not want to do this anymore", ree.getMessage());
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should throw an exception");
+
+ RejectedExecutionException ree = assertIsInstanceOf(RejectedExecutionException.class, e.getCause());
+ Assertions.assertEquals("I do not want to do this anymore", ree.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionCamel4022Test.java b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionCamel4022Test.java
index 86e71944e95..e040d35f1a6 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionCamel4022Test.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionCamel4022Test.java
@@ -23,7 +23,7 @@ import org.apache.camel.builder.RouteBuilder;
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 ExceptionCamel4022Test extends ContextTestSupport {
@@ -36,13 +36,11 @@ public class ExceptionCamel4022Test extends ContextTestSupport {
getMockEndpoint("mock:onexception").expectedMessageCount(0);
getMockEndpoint("mock:dlc").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "<body/>");
- fail("Should throw an exception");
- } catch (Exception e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Damn Again", cause.getMessage());
- }
+ Exception e = assertThrows(Exception.class, () -> template.sendBody("direct:start", "<body/>"),
+ "Should throw an exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Damn Again", cause.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionTest.java
index e582e343962..500613d2403 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionTest.java
@@ -17,13 +17,12 @@
package org.apache.camel.issues;
import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class ExceptionTest extends ContextTestSupport {
@@ -59,12 +58,9 @@ public class ExceptionTest extends ContextTestSupport {
exceptionEndpoint.expectedBodiesReceived("<exception/>");
resultEndpoint.expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "<body/>");
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class,
+ () -> template.sendBody("direct:start", "<body/>"),
+ "Should have thrown exception");
assertMockEndpointsSatisfied();
}
@@ -79,12 +75,9 @@ public class ExceptionTest extends ContextTestSupport {
exceptionEndpoint.expectedBodiesReceived("<not-handled/>");
resultEndpoint.expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "<body/>");
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class,
+ () -> template.sendBody("direct:start", "<body/>"),
+ "Should have thrown exception");
assertMockEndpointsSatisfied();
}
@@ -99,23 +92,18 @@ public class ExceptionTest extends ContextTestSupport {
exceptionEndpoint.expectedBodiesReceived("<exception/>");
resultEndpoint.expectedMessageCount(0);
- try {
- template.sendBody("direct:start2", "<body/>");
- fail("Should have thrown exception");
- } catch (Exception e) {
- // expected
- }
+ assertThrows(Exception.class,
+ () -> template.sendBody("direct:start2", "<body/>"),
+ "Should have thrown exception");
assertMockEndpointsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
- final Processor exceptionThrower = new Processor() {
- public void process(Exchange exchange) throws Exception {
- exchange.getIn().setBody("<exception/>");
- throw new IllegalArgumentException("Exception thrown intentionally.");
- }
+ final Processor exceptionThrower = exchange -> {
+ exchange.getIn().setBody("<exception/>");
+ throw new IllegalArgumentException("Exception thrown intentionally.");
};
return new RouteBuilder() {
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionNoEndlessLoopTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionNoEndlessLoopTest.java
index ca0754b20d2..f51aeb73d44 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionNoEndlessLoopTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionNoEndlessLoopTest.java
@@ -28,7 +28,7 @@ import org.junit.jupiter.api.Disabled;
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;
@Disabled("TODO: fix me")
public class ExceptionThrownFromOnExceptionNoEndlessLoopTest extends ContextTestSupport {
@@ -92,13 +92,12 @@ public class ExceptionThrownFromOnExceptionNoEndlessLoopTest extends ContextTest
getMockEndpoint("mock:result").expectedMessageCount(0);
getMockEndpoint("mock:end").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
- assertEquals("Not supported", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertEquals("Not supported", cause.getMessage());
assertMockEndpointsSatisfied();
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionTest.java
index bc3c904141a..5103ed2e679 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/ExceptionThrownFromOnExceptionTest.java
@@ -28,7 +28,7 @@ import org.junit.jupiter.api.Disabled;
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;
/*
*/
@@ -80,13 +80,12 @@ public class ExceptionThrownFromOnExceptionTest extends ContextTestSupport {
getMockEndpoint("mock:result").expectedMessageCount(0);
getMockEndpoint("mock:end").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
- assertEquals("Some other IOException", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class, () ->
+ template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
+
+ IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
+ assertEquals("Some other IOException", cause.getMessage());
assertMockEndpointsSatisfied();
@@ -133,13 +132,12 @@ public class ExceptionThrownFromOnExceptionTest extends ContextTestSupport {
getMockEndpoint("mock:result").expectedMessageCount(0);
getMockEndpoint("mock:end").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
- assertEquals("Some other IOException", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class, () ->
+ template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
+
+ IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
+ assertEquals("Some other IOException", cause.getMessage());
assertMockEndpointsSatisfied();
@@ -191,13 +189,12 @@ public class ExceptionThrownFromOnExceptionTest extends ContextTestSupport {
// so the exchange will throw an exception
getMockEndpoint("mock:error").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
- assertEquals("Some other IOException", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
+
+ IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
+ assertEquals("Some other IOException", cause.getMessage());
assertMockEndpointsSatisfied();
@@ -251,13 +248,12 @@ public class ExceptionThrownFromOnExceptionTest extends ContextTestSupport {
// so the exchange will throw an exception
getMockEndpoint("mock:error").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
- assertEquals("Some other IOException", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
+
+ IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
+ assertEquals("Some other IOException", cause.getMessage());
assertMockEndpointsSatisfied();
@@ -309,15 +305,14 @@ public class ExceptionThrownFromOnExceptionTest extends ContextTestSupport {
// and this time there was no exception thrown from onException,
// but the caller still fails since handled is false on onException
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should have thrown an exception");
- } catch (CamelExecutionException e) {
- // this time its the first exception thrown from the route
- IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
- assertEquals("IO error", cause.getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should have thrown an exception");
+
+ IOException cause = assertIsInstanceOf(IOException.class, e.getCause());
+ assertEquals("IO error", cause.getMessage());
+
assertMockEndpointsSatisfied();
assertEquals(4, RETRY.get(), "Should try 4 times (1 first, 3 retry)");
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/MockValueBuilderIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/MockValueBuilderIssueTest.java
index 62dcaddbc99..5f3b2fdd003 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/MockValueBuilderIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/MockValueBuilderIssueTest.java
@@ -21,8 +21,8 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
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 MockValueBuilderIssueTest extends ContextTestSupport {
@@ -38,20 +38,18 @@ public class MockValueBuilderIssueTest extends ContextTestSupport {
}
@Test
- public void testMockValueBuilderFail() throws Exception {
+ public void testMockValueBuilderFail() {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.message(0).exchangeProperty("foo").convertTo(String.class).contains("4");
template.sendBody("direct:start", "Hello World");
- try {
- assertMockEndpointsSatisfied();
- fail("Should fail");
- } catch (Throwable e) {
- String s = "Assertion error at index 0 on mock mock://result with predicate: exchangeProperty(foo) contains 4 evaluated as: 123 contains 4";
- assertTrue(e.getMessage().startsWith(s));
- }
+ Throwable e = assertThrows(Throwable.class, this::assertMockEndpointsSatisfied,
+ "Should fail");
+
+ String s = "Assertion error at index 0 on mock mock://result with predicate: exchangeProperty(foo) contains 4 evaluated as: 123 contains 4";
+ assertTrue(e.getMessage().startsWith(s));
}
@Test
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/MulticastShareUnitOfWorkOnExceptionHandledFalseIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/MulticastShareUnitOfWorkOnExceptionHandledFalseIssueTest.java
index 59da92f7588..7821f7c75a7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/MulticastShareUnitOfWorkOnExceptionHandledFalseIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/MulticastShareUnitOfWorkOnExceptionHandledFalseIssueTest.java
@@ -21,7 +21,7 @@ import org.apache.camel.builder.RouteBuilder;
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 MulticastShareUnitOfWorkOnExceptionHandledFalseIssueTest extends ContextTestSupport {
@@ -31,13 +31,12 @@ public class MulticastShareUnitOfWorkOnExceptionHandledFalseIssueTest extends Co
getMockEndpoint("mock:b").expectedMessageCount(1);
getMockEndpoint("mock:result").expectedMessageCount(0);
- try {
- template.sendBody("direct:start", "Hello World");
- fail("Should throw exception");
- } catch (Exception e) {
- IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause());
- assertEquals("Forced", cause.getMessage());
- }
+ Exception e = assertThrows(Exception.class,
+ () -> template.sendBody("direct:start", "Hello World"),
+ "Should throw exception");
+
+ IllegalArgumentException cause = assertIsInstanceOf(IllegalArgumentException.class, e.getCause().getCause());
+ assertEquals("Forced", cause.getMessage());
assertMockEndpointsSatisfied();
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java
index ab5d47ea6c3..f2332b5e7d7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/OnCompletionIssueTest.java
@@ -24,7 +24,7 @@ import org.apache.camel.component.mock.MockEndpoint;
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 OnCompletionIssueTest extends ContextTestSupport {
@@ -44,19 +44,17 @@ public class OnCompletionIssueTest extends ContextTestSupport {
template.sendBody("direct:input", "ile");
template.sendBody("direct:input", "markRollback");
- try {
- template.sendBody("direct:input", "npe");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- assertEquals("Darn NPE", e.getCause().getMessage());
- }
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:input", "npe"),
+ "Should have thrown exception");
- try {
- template.sendBody("direct:input", "rollback");
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- assertIsInstanceOf(RollbackExchangeException.class, e.getCause());
- }
+ assertEquals("Darn NPE", e.getCause().getMessage());
+
+ CamelExecutionException ex = assertThrows(CamelExecutionException.class,
+ () -> template.sendBody("direct:input", "rollback"),
+ "Should have thrown exception");
+
+ assertIsInstanceOf(RollbackExchangeException.class, ex.getCause());
assertMockEndpointsSatisfied();
}