This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 9522db7e1a31cd1dd8bb59e42c7999b85560a853 Author: Otavio Rodolfo Piske <[email protected]> AuthorDate: Sat Jan 24 18:54:01 2026 +0000 CAMEL-21196: modernize exception-based assertions in camel-cxf Replace try-catch-fail patterns with assertThrows() and update exception handling in test files to use modern JUnit 5 assertion methods. Converted 22 test files to use assertThrows for better test readability and consistency. --- .../jaxws/CXFWsdlOnlyPayloadModeNoSpringTest.java | 11 ++----- .../cxf/jaxws/CxfConsumerPayloadFaultTest.java | 13 +++----- .../cxf/jaxws/CxfConsumerProviderTest.java | 9 ++---- .../cxf/jaxws/CxfConsumerStartTwiceTest.java | 16 ++++------ .../cxf/jaxws/CxfConsumerStreamCacheTest.java | 9 ++---- .../cxf/jaxws/CxfCustomizedExceptionTest.java | 28 ++++++---------- .../cxf/jaxws/CxfMultipleConsumersSupportTest.java | 14 +++----- .../cxf/jaxws/CxfProducerSessionTest.java | 11 +++---- .../cxf/jaxws/CxfSchemaValidationTest.java | 17 +++------- .../component/cxf/jaxrs/CxfRsConsumerTest.java | 30 +++--------------- .../CxfRsConsumerSimpleBindingTest.java | 3 +- .../cxf/AbstractCXFGreeterRouterTest.java | 13 +++----- .../component/cxf/AbstractCxfWsdlFirstTest.java | 27 ++++++---------- .../component/cxf/CxfComponentEnableMtomTest.java | 37 +++++++--------------- .../CxfConsumerPayloadFaultCauseEnabledTest.java | 14 +++----- .../camel/component/cxf/CxfNonWrapperTest.java | 9 ++---- .../cxf/CxfSpringCustomizedExceptionTest.java | 23 ++++++-------- 17 files changed, 90 insertions(+), 194 deletions(-) diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CXFWsdlOnlyPayloadModeNoSpringTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CXFWsdlOnlyPayloadModeNoSpringTest.java index 8c86387af47f..c46dc8b73dff 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CXFWsdlOnlyPayloadModeNoSpringTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CXFWsdlOnlyPayloadModeNoSpringTest.java @@ -44,8 +44,8 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class CXFWsdlOnlyPayloadModeNoSpringTest extends CamelTestSupport { @@ -148,13 +148,8 @@ public class CXFWsdlOnlyPayloadModeNoSpringTest extends CamelTestSupport { personId.value = ""; Holder<String> ssn = new Holder<>(); Holder<String> name = new Holder<>(); - Throwable t = null; - try { - client.getPerson(personId, ssn, name); - fail("expect UnknownPersonFault"); - } catch (UnknownPersonFault e) { - t = e; - } + Throwable t = assertThrows(UnknownPersonFault.class, + () -> client.getPerson(personId, ssn, name)); assertNotNull(t); assertTrue(t instanceof UnknownPersonFault); diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayloadFaultTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayloadFaultTest.java index 0ea758b6858a..c65572b513ef 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayloadFaultTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerPayloadFaultTest.java @@ -44,8 +44,8 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; /** * Unit test to verify CxfConsumer to generate SOAP fault in PAYLOAD mode @@ -99,14 +99,9 @@ public class CxfConsumerPayloadFaultTest extends CamelTestSupport { personId.value = ""; Holder<String> ssn = new Holder<>(); Holder<String> name = new Holder<>(); - Throwable t = null; - try { - client.getPerson(personId, ssn, name); - fail("expect UnknownPersonFault"); - } catch (UnknownPersonFault e) { - t = e; - assertEquals("", e.getFaultInfo().getPersonId(), "Get the wrong fault detail"); - } + UnknownPersonFault t = assertThrows(UnknownPersonFault.class, + () -> client.getPerson(personId, ssn, name)); + assertEquals("", t.getFaultInfo().getPersonId(), "Get the wrong fault detail"); assertNotNull(t); assertTrue(t instanceof UnknownPersonFault); diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerProviderTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerProviderTest.java index 1e73a225c674..45c847c0e3a1 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerProviderTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerProviderTest.java @@ -28,8 +28,8 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; 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 CxfConsumerProviderTest extends CamelTestSupport { @@ -75,12 +75,7 @@ public class CxfConsumerProviderTest extends CamelTestSupport { String response = template.requestBody(simpleEndpointAddress, REQUEST_MESSAGE, String.class); assertTrue(response.startsWith(RESPONSE_MESSAGE_BEGINE), "Get a wrong response"); assertTrue(response.endsWith(RESPONSE_MESSAGE_END), "Get a wrong response"); - try { - template.requestBody(simpleEndpointAddress, null, String.class); - fail("Excpetion to get exception here"); - } catch (Exception ex) { - // do nothing here - } + assertThrows(Exception.class, () -> template.requestBody(simpleEndpointAddress, null, String.class)); response = template.requestBody(simpleEndpointAddress, REQUEST_MESSAGE, String.class); assertTrue(response.startsWith(RESPONSE_MESSAGE_BEGINE), "Get a wrong response"); diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStartTwiceTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStartTwiceTest.java index 592476188afd..ceb93e4e4164 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStartTwiceTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStartTwiceTest.java @@ -22,8 +22,8 @@ import org.apache.camel.component.cxf.common.CXFTestSupport; import org.apache.camel.impl.DefaultCamelContext; 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 CxfConsumerStartTwiceTest { static final int PORT = CXFTestSupport.getPort6(); @@ -50,15 +50,11 @@ public class CxfConsumerStartTwiceTest { } }); - try { - context.start(); - fail("Expect to catch an exception here"); - } catch (Exception ex) { - assertTrue(ex.getMessage().endsWith( - "Multiple consumers for the same endpoint is not allowed: cxf://http://localhost:" + PORT - + "/" + getClass().getSimpleName() - + "/test?serviceClass=org.apache.camel.component.cxf.jaxws.HelloService")); - } + Exception ex = assertThrows(Exception.class, context::start); + assertTrue(ex.getMessage().endsWith( + "Multiple consumers for the same endpoint is not allowed: cxf://http://localhost:" + PORT + + "/" + getClass().getSimpleName() + + "/test?serviceClass=org.apache.camel.component.cxf.jaxws.HelloService")); context.stop(); } diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStreamCacheTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStreamCacheTest.java index b28c5835b27c..05b4eae7a2e6 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStreamCacheTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfConsumerStreamCacheTest.java @@ -32,8 +32,8 @@ import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; 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; //Modified from https://issues.apache.org/jira/secure/attachment/12730161/0001-CAMEL-8419-Camel-StreamCache-does-not-work-with-CXF-.patch public class CxfConsumerStreamCacheTest extends CamelTestSupport { @@ -99,12 +99,7 @@ public class CxfConsumerStreamCacheTest extends CamelTestSupport { String response = template.requestBody(simpleEndpointAddress, REQUEST_MESSAGE, String.class); assertTrue(response.startsWith(RESPONSE_MESSAGE_BEGINE), "Get a wrong response"); assertTrue(response.endsWith(RESPONSE_MESSAGE_END), "Get a wrong response"); - try { - template.requestBody(simpleEndpointAddress, null, String.class); - fail("Excpetion to get exception here"); - } catch (Exception ex) { - // do nothing here - } + assertThrows(Exception.class, () -> template.requestBody(simpleEndpointAddress, null, String.class)); response = template.requestBody(simpleEndpointAddress, REQUEST_MESSAGE, String.class); assertTrue(response.startsWith(RESPONSE_MESSAGE_BEGINE), "Get a wrong response"); diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfCustomizedExceptionTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfCustomizedExceptionTest.java index 3c1252d4345e..8250b16d4f57 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfCustomizedExceptionTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfCustomizedExceptionTest.java @@ -46,8 +46,8 @@ import org.apache.cxf.interceptor.Fault; 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 CxfCustomizedExceptionTest extends CamelTestSupport { @@ -139,17 +139,13 @@ public class CxfCustomizedExceptionTest extends CamelTestSupport { HelloService client = (HelloService) proxyFactory.create(); - try { - client.echo("hello world"); - fail("Expect to get an exception here"); - } catch (Exception e) { - assertEquals(EXCEPTION_MESSAGE, e.getMessage(), "Expect to get right exception message"); - assertTrue(e instanceof SoapFault, "Exception is not instance of SoapFault"); - assertEquals(DETAIL_TEXT, ((SoapFault) e).getDetail().getTextContent(), "Expect to get right detail message"); - //In CXF 2.1.2 , the fault code is per spec , the below fault-code is for SOAP 1.1 - assertEquals("{http://schemas.xmlsoap.org/soap/envelope/}Client", ((SoapFault) e).getFaultCode().toString(), - "Expect to get right fault-code"); - } + Exception e = assertThrows(Exception.class, () -> client.echo("hello world")); + assertEquals(EXCEPTION_MESSAGE, e.getMessage(), "Expect to get right exception message"); + assertTrue(e instanceof SoapFault, "Exception is not instance of SoapFault"); + assertEquals(DETAIL_TEXT, ((SoapFault) e).getDetail().getTextContent(), "Expect to get right detail message"); + //In CXF 2.1.2 , the fault code is per spec , the below fault-code is for SOAP 1.1 + assertEquals("{http://schemas.xmlsoap.org/soap/envelope/}Client", ((SoapFault) e).getFaultCode().toString(), + "Expect to get right fault-code"); } @@ -170,12 +166,8 @@ public class CxfCustomizedExceptionTest extends CamelTestSupport { out.flush(); is.close(); // check the response code - try { - urlConnection.getInputStream(); - fail("We except an IOException here"); - } catch (IOException exception) { - assertTrue(exception.getMessage().contains("500")); - } + IOException exception = assertThrows(IOException.class, urlConnection::getInputStream); + assertTrue(exception.getMessage().contains("500")); } diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfMultipleConsumersSupportTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfMultipleConsumersSupportTest.java index 1e6f152381fb..29dd8403014d 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfMultipleConsumersSupportTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfMultipleConsumersSupportTest.java @@ -22,8 +22,8 @@ import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit5.CamelTestSupport; 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 CxfMultipleConsumersSupportTest extends CamelTestSupport { protected static int port1 = CXFTestSupport.getPort1(); @@ -50,14 +50,10 @@ public class CxfMultipleConsumersSupportTest extends CamelTestSupport { from(SIMPLE_ENDPOINT_URI).to("mock:b"); } }); - try { - context.start(); - fail("Should have thrown an exception"); - } catch (Exception e) { - assertTrue(e.getMessage().endsWith( - "Multiple consumers for the same endpoint is not allowed: cxf://http://localhost:" + port1 - + "/CxfMultipleConsumersSupportTest/test?serviceClass=org.apache.camel.component.cxf.jaxws.HelloService")); - } + Exception e = assertThrows(Exception.class, context::start); + assertTrue(e.getMessage().endsWith( + "Multiple consumers for the same endpoint is not allowed: cxf://http://localhost:" + port1 + + "/CxfMultipleConsumersSupportTest/test?serviceClass=org.apache.camel.component.cxf.jaxws.HelloService")); } @Test diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfProducerSessionTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfProducerSessionTest.java index 88f200e58f59..3a504f03ce8d 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfProducerSessionTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfProducerSessionTest.java @@ -37,8 +37,8 @@ import org.junit.jupiter.api.BeforeAll; 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 CxfProducerSessionTest extends CamelTestSupport { private static final int PORT = CXFTestSupport.getPort1(); @@ -115,12 +115,9 @@ public class CxfProducerSessionTest extends CamelTestSupport { @Test public void testSessionWithInvalidPayload() throws Throwable { - try { - template.requestBody("direct:invalid", "World", String.class); - fail("Expected an exception"); - } catch (CamelExecutionException e) { - assertTrue(e.getCause() instanceof IllegalArgumentException); - } + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("direct:invalid", "World", String.class)); + assertTrue(e.getCause() instanceof IllegalArgumentException); } @Override diff --git a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfSchemaValidationTest.java b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfSchemaValidationTest.java index 99aa9326a6a2..02f530cd79d4 100644 --- a/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfSchemaValidationTest.java +++ b/components/camel-cxf/camel-cxf-soap/src/test/java/org/apache/camel/component/cxf/jaxws/CxfSchemaValidationTest.java @@ -40,8 +40,8 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class CxfSchemaValidationTest extends CamelTestSupport { @@ -107,11 +107,7 @@ public class CxfSchemaValidationTest extends CamelTestSupport { @Test public void schemaValidationDisabledServerTest() throws Exception { // invoke the service with a non-valid message - try { - invokeService(serviceAddressValidationDisabled, RandomStringUtils.secure().next(40, true, true)); - } catch (SOAPFaultException e) { - fail("Do not expect an exception here"); - } + invokeService(serviceAddressValidationDisabled, RandomStringUtils.secure().next(40, true, true)); } @Test @@ -133,12 +129,9 @@ public class CxfSchemaValidationTest extends CamelTestSupport { <xsd:element name="personId" type="tns:MyStringType"/> */ - try { - invokeService(serviceAddressValidationEnabled, RandomStringUtils.secure().next(40, true, true)); - fail("expect a Validation exception here"); - } catch (SOAPFaultException e) { - assertEquals("the length of the value is 40, but the required maximum is 30.", e.getMessage(), ""); - } + SOAPFaultException e = assertThrows(SOAPFaultException.class, + () -> invokeService(serviceAddressValidationEnabled, RandomStringUtils.secure().next(40, true, true))); + assertEquals("the length of the value is 40, but the required maximum is 30.", e.getMessage(), ""); } @Test diff --git a/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java b/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java index 1b4044741a88..44e869f45c93 100644 --- a/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java +++ b/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java @@ -57,7 +57,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.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; public class CxfRsConsumerTest extends CamelTestSupport { private static final String PUT_REQUEST = "<Customer><name>Mary</name><id>123</id></Customer>"; @@ -170,36 +170,16 @@ public class CxfRsConsumerTest extends CamelTestSupport { URL url; url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/789"); - try { - url.openStream(); - fail("Expect to get exception here"); - } catch (IOException exception) { - // expect the Internal error exception - } + assertThrows(IOException.class, url::openStream); url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/456"); - try { - url.openStream(); - fail("Expect to get exception here"); - } catch (FileNotFoundException exception) { - // do nothing here - } + assertThrows(FileNotFoundException.class, url::openStream); url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/234"); - try { - url.openStream(); - fail("Expect to get exception here"); - } catch (FileNotFoundException exception) { - // do nothing here - } + assertThrows(FileNotFoundException.class, url::openStream); url = new URL("http://localhost:" + CXT + "/rest/customerservice/customers/256"); - try { - url.openStream(); - fail("Expect to get exception here"); - } catch (IOException exception) { - // expect the Internal error exception - } + assertThrows(IOException.class, url::openStream); } diff --git a/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingTest.java b/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingTest.java index 736ba3f15ef8..42e43b8b60fb 100644 --- a/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingTest.java +++ b/components/camel-cxf/camel-cxf-spring-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/simplebinding/CxfRsConsumerSimpleBindingTest.java @@ -56,7 +56,6 @@ 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.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; /** * Tests for the Simple Binding style of CXF JAX-RS consumers. @@ -99,7 +98,7 @@ public class CxfRsConsumerSimpleBindingTest extends CamelTestSupport { } else if (id == 456) { exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 404); } else { - fail(); + throw new AssertionError("Unexpected customer ID: " + id); } } }); diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java index 337473644c9b..46ac60b9ede6 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCXFGreeterRouterTest.java @@ -33,7 +33,6 @@ 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; public abstract class AbstractCXFGreeterRouterTest extends CamelSpringTestSupport { @@ -81,14 +80,10 @@ public abstract class AbstractCXFGreeterRouterTest extends CamelSpringTestSuppor greeter.greetMeOneWay("call greetMe OneWay !"); // test throw the exception - try { - greeter.testDocLitFault("NoSuchCodeLitFault"); - // should get the exception here - fail("Should get the NoSuchCodeLitFault here."); - } catch (NoSuchCodeLitFault fault) { - // expect the fault here - assertNotNull(fault.getFaultInfo(), "The fault info should not be null"); - } + NoSuchCodeLitFault fault = assertThrows(NoSuchCodeLitFault.class, + () -> greeter.testDocLitFault("NoSuchCodeLitFault")); + // expect the fault here + assertNotNull(fault.getFaultInfo(), "The fault info should not be null"); } diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCxfWsdlFirstTest.java b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCxfWsdlFirstTest.java index 0d927fec645e..5cfb56f8eabc 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCxfWsdlFirstTest.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/AbstractCxfWsdlFirstTest.java @@ -38,8 +38,8 @@ import org.apache.camel.wsdl_first.UnknownPersonFault; 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 abstract class AbstractCxfWsdlFirstTest extends CamelSpringTestSupport { static int port1 = CXFTestSupport.getPort1(); @@ -77,24 +77,17 @@ public abstract class AbstractCxfWsdlFirstTest extends CamelSpringTestSupport { assertEquals("Bonjour", name.value, "we should get the right answer from router"); personId.value = ""; - try { - client.getPerson(personId, ssn, name); - fail("We expect to get the UnknowPersonFault here"); - } catch (UnknownPersonFault fault) { - // We expect to get fault here - } + assertThrows(UnknownPersonFault.class, + () -> client.getPerson(personId, ssn, name)); personId.value = "Invoking getPerson with invalid length string, expecting exception...xxxxxxxxx"; - try { - client.getPerson(personId, ssn, name); - fail("We expect to get the WebSerivceException here"); - } catch (WebServiceException ex) { - // Caught expected WebServiceException here - assertTrue(ex.getMessage().indexOf("MyStringType") > 0 - || ex.getMessage().indexOf("Could not parse the XML stream") != -1 - || ex.getMessage().indexOf("the required maximum is 30") > 0, - "Should get the xml vaildate error! " + ex.getMessage()); - } + WebServiceException ex = assertThrows(WebServiceException.class, + () -> client.getPerson(personId, ssn, name)); + // Caught expected WebServiceException here + assertTrue(ex.getMessage().indexOf("MyStringType") > 0 + || ex.getMessage().indexOf("Could not parse the XML stream") != -1 + || ex.getMessage().indexOf("the required maximum is 30") > 0, + "Should get the xml vaildate error! " + ex.getMessage()); verifyJaxwsHandlers(fromHandler, toHandler); } diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfComponentEnableMtomTest.java b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfComponentEnableMtomTest.java index 8949ecd16d16..c4e0d2900005 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfComponentEnableMtomTest.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfComponentEnableMtomTest.java @@ -34,7 +34,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = CxfComponentEnableMtomTest.TestConfig.class) @@ -48,48 +47,36 @@ public class CxfComponentEnableMtomTest { public void testIsMtomEnabledEnabledThroughBeanSetter() throws InterruptedException { Endpoint endpoint = context.getEndpoint("cxf:bean:mtomByBeanSetter"); - if (endpoint instanceof CxfEndpoint) { - CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; - assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); - } else { - fail("CXF Endpoint not found"); - } + assertTrue(endpoint instanceof CxfEndpoint, "CXF Endpoint not found"); + CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; + assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); } @Test public void testIsMtomEnabledEnabledThroughBeanProperties() throws InterruptedException { Endpoint endpoint = context.getEndpoint("cxf:bean:mtomByBeanProperties"); - if (endpoint instanceof CxfEndpoint) { - CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; - assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); - } else { - fail("CXF Endpoint not found"); - } + assertTrue(endpoint instanceof CxfEndpoint, "CXF Endpoint not found"); + CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; + assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); } @Test public void testIsMtomEnabledEnabledThroughURIProperties() throws InterruptedException { Endpoint endpoint = context.getEndpoint("cxf:bean:mtomByURIProperties?properties.mtom-enabled=true"); - if (endpoint instanceof CxfEndpoint) { - CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; - assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); - } else { - fail("CXF Endpoint not found"); - } + assertTrue(endpoint instanceof CxfEndpoint, "CXF Endpoint not found"); + CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; + assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); } @Test public void testIsMtomEnabledEnabledThroughQueryParameters() throws InterruptedException { Endpoint endpoint = context.getEndpoint("cxf:bean:mtomByQueryParameters?mtomEnabled=true"); - if (endpoint instanceof CxfEndpoint) { - CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; - assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); - } else { - fail("CXF Endpoint not found"); - } + assertTrue(endpoint instanceof CxfEndpoint, "CXF Endpoint not found"); + CxfEndpoint cxfEndpoint = (CxfEndpoint) endpoint; + assertTrue(cxfEndpoint.isMtomEnabled(), "Mtom should be enabled"); } @Configuration diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadFaultCauseEnabledTest.java b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadFaultCauseEnabledTest.java index 97ad72662ed0..eddeede9b7c1 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadFaultCauseEnabledTest.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfConsumerPayloadFaultCauseEnabledTest.java @@ -43,8 +43,8 @@ import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; /** * Unit test to verify CxfConsumer to generate SOAP fault in PAYLOAD mode with the exception cause returned @@ -92,14 +92,10 @@ public class CxfConsumerPayloadFaultCauseEnabledTest extends CamelSpringTestSupp personId.value = ""; Holder<String> ssn = new Holder<>(); Holder<String> name = new Holder<>(); - try { - client.getPerson(personId, ssn, name); - fail("SOAPFault expected!"); - } catch (Exception e) { - assertTrue(e instanceof SOAPFaultException); - SOAPFault fault = ((SOAPFaultException) e).getFault(); - assertEquals("Someone messed up the service. Caused by: Homer", fault.getFaultString()); - } + Exception e = assertThrows(Exception.class, () -> client.getPerson(personId, ssn, name)); + assertTrue(e instanceof SOAPFaultException); + SOAPFault fault = ((SOAPFaultException) e).getFault(); + assertEquals("Someone messed up the service. Caused by: Homer", fault.getFaultString()); } @Override diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfNonWrapperTest.java b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfNonWrapperTest.java index 14a24597c393..8c3d7ce0cc33 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfNonWrapperTest.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfNonWrapperTest.java @@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; 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 CxfNonWrapperTest extends CamelSpringTestSupport { int port1 = CXFTestSupport.getPort1(); @@ -60,12 +60,7 @@ public class CxfNonWrapperTest extends CamelSpringTestSupport { assertEquals("Bonjour", response.getName(), "we should get the right answer from router"); request.setPersonId(""); - try { - client.getPerson(request); - fail("We expect to get the UnknowPersonFault here"); - } catch (UnknownPersonFault fault) { - // We expect to get fault here - } + assertThrows(UnknownPersonFault.class, () -> client.getPerson(request)); } } diff --git a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfSpringCustomizedExceptionTest.java b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfSpringCustomizedExceptionTest.java index 48d0207f1d28..9d442de3f505 100644 --- a/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfSpringCustomizedExceptionTest.java +++ b/components/camel-cxf/camel-cxf-spring-soap/src/test/java/org/apache/camel/component/cxf/CxfSpringCustomizedExceptionTest.java @@ -34,8 +34,8 @@ import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; 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 CxfSpringCustomizedExceptionTest extends CamelSpringTestSupport { private static final String EXCEPTION_MESSAGE = "This is an exception test message"; @@ -64,18 +64,15 @@ public class CxfSpringCustomizedExceptionTest extends CamelSpringTestSupport { @Test public void testInvokingServiceFromCamel() throws Exception { - try { - template.sendBodyAndHeader("direct:start", ExchangePattern.InOut, "hello world", CxfConstants.OPERATION_NAME, - "echo"); - fail("Should have thrown an exception"); - } catch (Exception ex) { - Throwable result = ex.getCause(); - assertTrue(result instanceof SoapFault, "Exception is not instance of SoapFault"); - assertEquals(DETAIL_TEXT, ((SoapFault) result).getDetail().getTextContent(), "Expect to get right detail message"); - assertEquals("{http://schemas.xmlsoap.org/soap/envelope/}Client", ((SoapFault) result).getFaultCode().toString(), - "Expect to get right fault-code"); - } - + Exception ex = assertThrows(Exception.class, + () -> template.sendBodyAndHeader("direct:start", ExchangePattern.InOut, "hello world", + CxfConstants.OPERATION_NAME, + "echo")); + Throwable result = ex.getCause(); + assertTrue(result instanceof SoapFault, "Exception is not instance of SoapFault"); + assertEquals(DETAIL_TEXT, ((SoapFault) result).getDetail().getTextContent(), "Expect to get right detail message"); + assertEquals("{http://schemas.xmlsoap.org/soap/envelope/}Client", ((SoapFault) result).getFaultCode().toString(), + "Expect to get right fault-code"); } protected AbstractApplicationContext createApplicationContext() {
