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 2020/11/27 15:15:34 UTC
[camel] branch master updated: Simplified exception testing (#4695)
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new bfb7bf4 Simplified exception testing (#4695)
bfb7bf4 is described below
commit bfb7bf49bbcc467f115d3d47dc7b5bd85662ca17
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Fri Nov 27 16:15:05 2020 +0100
Simplified exception testing (#4695)
Avoids false positives when the expected exception is thrown by a different method other than the one under test.
Components: camel-quartz, camel-servicenow, camel-spring-main,
camel-spring, camel-sql, camel-undertow
---
.../component/quartz/QuartzNameCollisionTest.java | 24 +++++----
.../camel/component/servicenow/ServiceNowTest.java | 31 ++++++------
.../camel/spring/MisspelledRouteRefTest.java | 22 ++++-----
.../remoting/EchoSpringRemotingPojoDirectTest.java | 39 +++++++--------
.../apache/camel/component/sql/SqlRouteTest.java | 57 ++++++++++------------
...ndertowProducerThrowExceptionOnFailureTest.java | 46 ++++++++---------
.../undertow/handlers/CamelRootHandlerTest.java | 25 +++++-----
.../rest/RestUndertowHttpBindingModeJsonTest.java | 10 ++--
8 files changed, 118 insertions(+), 136 deletions(-)
diff --git a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java
index e571c8b..579115f 100644
--- a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java
+++ b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java
@@ -27,8 +27,8 @@ import org.quartz.TriggerKey;
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;
/**
* Check for duplicate name/group collision.
@@ -49,18 +49,16 @@ public class QuartzNameCollisionTest {
});
camel1.start();
- try {
- camel1.addRoutes(new RouteBuilder() {
- @Override
- public void configure() throws Exception {
- from("quartz://myGroup/myTimerName?cron=0/2+*+*+*+*+?").to("log:two", "mock:two");
- }
- });
- fail("Should have thrown an exception");
- } catch (FailedToCreateRouteException e) {
- String reason = e.getMessage();
- assertTrue(reason.contains("Trigger key myGroup.myTimerName is already in use"));
- }
+ RouteBuilder routeBuilder = new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("quartz://myGroup/myTimerName?cron=0/2+*+*+*+*+?").to("log:two", "mock:two");
+ }
+ };
+
+ Exception ex = assertThrows(FailedToCreateRouteException.class, () -> camel1.addRoutes(routeBuilder));
+ String reason = ex.getMessage();
+ assertTrue(reason.contains("Trigger key myGroup.myTimerName is already in use"));
}
@Test
diff --git a/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java b/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java
index a18910e..41120f0 100644
--- a/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java
+++ b/components/camel-servicenow/camel-servicenow-component/src/test/java/org/apache/camel/component/servicenow/ServiceNowTest.java
@@ -20,11 +20,13 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
+import java.util.Map;
import java.util.UUID;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.component.servicenow.model.Incident;
@@ -33,13 +35,13 @@ 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 ServiceNowTest extends ServiceNowTestSupport {
@Test
- public void testExceptions() throws Exception {
+ public void testExceptions() {
// 404
try {
template().sendBodyAndHeaders(
@@ -83,20 +85,19 @@ public class ServiceNowTest extends ServiceNowTestSupport {
@Test
public void testBodyMismatch() throws Exception {
- try {
- template().sendBodyAndHeaders(
- "direct:servicenow",
- "NotAnIncidentObject",
- kvBuilder()
- .put(ServiceNowConstants.RESOURCE, "table")
- .put(ServiceNowConstants.ACTION, ServiceNowConstants.ACTION_CREATE)
- .put(ServiceNowParams.PARAM_TABLE_NAME, "incident")
- .build());
- fail("Should fail as body is not compatible with model defined in route for table incident");
- } catch (CamelExecutionException e) {
- assertTrue(e.getCause() instanceof IllegalArgumentException);
- }
+ Map<String, Object> kv = kvBuilder()
+ .put(ServiceNowConstants.RESOURCE, "table")
+ .put(ServiceNowConstants.ACTION, ServiceNowConstants.ACTION_CREATE)
+ .put(ServiceNowParams.PARAM_TABLE_NAME, "incident")
+ .build();
+
+ ProducerTemplate template = template();
+
+ Exception ex = assertThrows(CamelExecutionException.class,
+ () -> template.sendBodyAndHeaders("direct:servicenow", "NotAnIncidentObject", kv));
+
+ assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
@Test
diff --git a/components/camel-spring-main/src/test/java/org/apache/camel/spring/MisspelledRouteRefTest.java b/components/camel-spring-main/src/test/java/org/apache/camel/spring/MisspelledRouteRefTest.java
index 3d4830b..d6768ab 100644
--- a/components/camel-spring-main/src/test/java/org/apache/camel/spring/MisspelledRouteRefTest.java
+++ b/components/camel-spring-main/src/test/java/org/apache/camel/spring/MisspelledRouteRefTest.java
@@ -22,20 +22,20 @@ import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
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 MisspelledRouteRefTest {
@Test
- public void testApplicationContextFailed() throws Exception {
- try {
- Main main = new Main();
- main.setApplicationContextUri("org/apache/camel/spring/MisspelledRouteRefTest.xml");
- main.start();
- fail("Should have thrown an exception");
- } catch (RuntimeCamelException e) {
- CamelException ce = assertIsInstanceOf(CamelException.class, e.getCause());
- assertEquals("Cannot find any routes with this RouteBuilder reference: RouteBuilderRef[xxxroute]", ce.getMessage());
- }
+ public void testApplicationContextFailed() {
+ Main main = new Main();
+ main.setApplicationContextUri("org/apache/camel/spring/MisspelledRouteRefTest.xml");
+
+ Exception ex = assertThrows(RuntimeCamelException.class, () -> main.start());
+
+ CamelException ce = assertIsInstanceOf(CamelException.class, ex.getCause());
+ assertEquals("Cannot find any routes with this RouteBuilder reference: RouteBuilderRef[xxxroute]",
+ ce.getMessage());
+
}
}
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/remoting/EchoSpringRemotingPojoDirectTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/remoting/EchoSpringRemotingPojoDirectTest.java
index 537c8b9..c9e67c1 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/spring/remoting/EchoSpringRemotingPojoDirectTest.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/remoting/EchoSpringRemotingPojoDirectTest.java
@@ -23,8 +23,7 @@ import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 EchoSpringRemotingPojoDirectTest extends SpringTestSupport {
@@ -40,29 +39,25 @@ public class EchoSpringRemotingPojoDirectTest extends SpringTestSupport {
}
@Test
- public void testPojoKabom() throws Exception {
- try {
- template.requestBody("direct:start", "Kabom", String.class);
- fail("Should have thrown exception");
- } catch (RuntimeCamelException e) {
- assertIsInstanceOf(MyEchoRuntimeException.class, e.getCause());
- assertEquals("Damn something went wrong", e.getCause().getMessage());
- }
+ public void testPojoKabom() {
+
+ Exception ex = assertThrows(RuntimeCamelException.class,
+ () -> template.requestBody("direct:start", "Kabom", String.class));
+
+ assertIsInstanceOf(MyEchoRuntimeException.class, ex.getCause());
+ assertEquals("Damn something went wrong", ex.getCause().getMessage());
}
@Test
- public void testPojoBeanKabom() throws Exception {
- try {
- // use the pojo directly to call the injected endpoint and have the
- // original runtime exception thrown
- EchoPojoDirect echoPojoDirect = applicationContext.getBean("myPojoDirect", EchoPojoDirect.class);
- String out = echoPojoDirect.onEcho("Kabom");
- assertNotNull(out);
- fail("Should have thrown exception");
- } catch (RuntimeException e) {
- assertIsInstanceOf(MyEchoRuntimeException.class, e);
- assertEquals("Damn something went wrong", e.getMessage());
- }
+ public void testPojoBeanKabom() {
+ EchoPojoDirect echoPojoDirect = applicationContext.getBean("myPojoDirect", EchoPojoDirect.class);
+
+ // use the pojo directly to call the injected endpoint and have the
+ // original runtime exception thrown
+ Exception ex = assertThrows(MyEchoRuntimeException.class,
+ () -> echoPojoDirect.onEcho("Kabom"));
+
+ assertEquals("Damn something went wrong", ex.getMessage());
}
}
diff --git a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java
index ce8fb5c..10196a9 100644
--- a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java
+++ b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlRouteTest.java
@@ -40,6 +40,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
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;
@@ -104,25 +105,20 @@ public class SqlRouteTest extends CamelTestSupport {
}
@Test
- public void testLowNumberOfParameter() throws Exception {
- try {
- template.sendBody("direct:list", "ASF");
- fail();
- } catch (RuntimeCamelException e) {
- // should have DataAccessException thrown
- assertTrue(e.getCause() instanceof DataAccessException, "Exception thrown is wrong");
- }
+ public void testLowNumberOfParameter() {
+ Exception ex = assertThrows(RuntimeCamelException.class, () -> template.sendBody("direct:list", "ASF"));
+
+ // should have DataAccessException thrown
+ assertTrue(ex.getCause() instanceof DataAccessException, "Exception thrown is wrong");
}
@Test
- public void testHighNumberOfParameter() throws Exception {
- try {
- template.sendBody("direct:simple", new Object[] { "ASF", "Foo" });
- fail();
- } catch (RuntimeCamelException e) {
- // should have DataAccessException thrown
- assertTrue(e.getCause() instanceof DataAccessException, "Exception thrown is wrong");
- }
+ public void testHighNumberOfParameter() {
+ Exception ex = assertThrows(RuntimeCamelException.class,
+ () -> template.sendBody("direct:simple", new Object[] { "ASF", "Foo" }));
+
+ // should have DataAccessException thrown
+ assertTrue(ex.getCause() instanceof DataAccessException, "Exception thrown is wrong");
}
@Test
@@ -230,14 +226,13 @@ public class SqlRouteTest extends CamelTestSupport {
}
@Test
- public void testBatchMissingParamAtEnd() throws Exception {
- try {
- List<?> data = Arrays.asList(Arrays.asList(9, "stu", "vwx"), Arrays.asList(10, "yza"));
- template.sendBody("direct:batch", data);
- fail();
- } catch (RuntimeCamelException e) {
- assertTrue(e.getCause() instanceof UncategorizedSQLException);
- }
+ public void testBatchMissingParamAtEnd() {
+ List<?> data = Arrays.asList(Arrays.asList(9, "stu", "vwx"), Arrays.asList(10, "yza"));
+
+ Exception ex = assertThrows(RuntimeCamelException.class, () -> template.sendBody("direct:batch", data));
+
+ assertTrue(ex.getCause() instanceof UncategorizedSQLException);
+
assertEquals(Integer.valueOf(0),
jdbcTemplate.queryForObject("select count(*) from projects where id = 9", Integer.class));
assertEquals(Integer.valueOf(0),
@@ -245,14 +240,12 @@ public class SqlRouteTest extends CamelTestSupport {
}
@Test
- public void testBatchMissingParamAtBeginning() throws Exception {
- try {
- List<?> data = Arrays.asList(Arrays.asList(9, "stu"), Arrays.asList(10, "vwx", "yza"));
- template.sendBody("direct:batch", data);
- fail();
- } catch (RuntimeCamelException e) {
- assertTrue(e.getCause() instanceof UncategorizedSQLException);
- }
+ public void testBatchMissingParamAtBeginning() {
+ List<?> data = Arrays.asList(Arrays.asList(9, "stu"), Arrays.asList(10, "vwx", "yza"));
+
+ Exception ex = assertThrows(RuntimeCamelException.class, () -> template.sendBody("direct:batch", data));
+ assertTrue(ex.getCause() instanceof UncategorizedSQLException);
+
assertEquals(Integer.valueOf(0),
jdbcTemplate.queryForObject("select count(*) from projects where id = 9", Integer.class));
assertEquals(Integer.valueOf(0),
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerThrowExceptionOnFailureTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerThrowExceptionOnFailureTest.java
index 4f33f13..7b441ee 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerThrowExceptionOnFailureTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowProducerThrowExceptionOnFailureTest.java
@@ -19,6 +19,8 @@ package org.apache.camel.component.undertow;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.http.base.HttpOperationFailedException;
import org.apache.camel.model.rest.RestBindingMode;
@@ -26,7 +28,7 @@ import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
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 UndertowProducerThrowExceptionOnFailureTest extends BaseUndertowTest {
@@ -38,30 +40,30 @@ public class UndertowProducerThrowExceptionOnFailureTest extends BaseUndertowTes
}
@Test
- public void testFailWithException() throws Exception {
- try {
- template().requestBody("undertow:http://localhost:{{port}}/fail?throwExceptionOnFailure=true", null, String.class);
- fail("Should throw an exception");
- } catch (CamelExecutionException e) {
- HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
- assertEquals(404, cause.getStatusCode());
- }
+ public void testFailWithException() {
+ ProducerTemplate template = template();
+ String uri = "undertow:http://localhost:{{port}}/fail?throwExceptionOnFailure=true";
+
+ Exception ex = assertThrows(CamelExecutionException.class,
+ () -> template.requestBody(uri, null, String.class));
+
+ HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, ex.getCause());
+ assertEquals(404, cause.getStatusCode());
}
@Test
- public void testFailWithException2() throws Exception {
- try {
- fluentTemplate().to("undertow:http://localhost:{{port2}}/test/fail?throwExceptionOnFailure=true")
- .withHeader(Exchange.HTTP_METHOD, "PUT")
- .withBody("This is not JSON format")
- .request(String.class);
- fail("Should throw an exception");
- } catch (CamelExecutionException e) {
- HttpOperationFailedException httpException = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
- assertEquals(400, httpException.getStatusCode());
- assertEquals("text/plain", httpException.getResponseHeaders().get(Exchange.CONTENT_TYPE));
- assertEquals("Invalid json data", httpException.getResponseBody());
- }
+ public void testFailWithException2() {
+ FluentProducerTemplate template = fluentTemplate()
+ .to("undertow:http://localhost:{{port2}}/test/fail?throwExceptionOnFailure=true")
+ .withHeader(Exchange.HTTP_METHOD, "PUT")
+ .withBody("This is not JSON format");
+
+ Exception ex = assertThrows(CamelExecutionException.class, () -> template.request(String.class));
+
+ HttpOperationFailedException httpException = assertIsInstanceOf(HttpOperationFailedException.class, ex.getCause());
+ assertEquals(400, httpException.getStatusCode());
+ assertEquals("text/plain", httpException.getResponseHeaders().get(Exchange.CONTENT_TYPE));
+ assertEquals("Invalid json data", httpException.getResponseBody());
}
@Override
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/handlers/CamelRootHandlerTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/handlers/CamelRootHandlerTest.java
index 1bf07f1..9c4802a 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/handlers/CamelRootHandlerTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/handlers/CamelRootHandlerTest.java
@@ -21,16 +21,15 @@ import io.undertow.server.handlers.RedirectHandler;
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 CamelRootHandlerTest {
private static final HttpHandler DEFAULT_HANDLER = new NotFoundHandler();
@Test
- public void httpAndWsUnssupportedForTheSamePath() {
-
+ public void httpAndWsUnsupportedForTheSamePath() {
final CamelRootHandler root = new CamelRootHandler(DEFAULT_HANDLER);
final RedirectHandler httpHandler = new RedirectHandler("http://whereever");
@@ -39,11 +38,11 @@ public class CamelRootHandlerTest {
root.add("/app1", null, false, httpHandler);
assertFalse(root.isEmpty());
- try {
- root.add("/app1", null, false, new CamelWebSocketHandler());
- fail(IllegalArgumentException.class.getName() + " expected");
- } catch (IllegalArgumentException expected) {
- }
+ CamelWebSocketHandler camelWebSocketHandler1 = new CamelWebSocketHandler();
+
+ assertThrows(IllegalArgumentException.class,
+ () -> root.add("/app1", null, false, camelWebSocketHandler1),
+ "IllegalArgumentException expected");
root.remove("/app1", null, false);
@@ -51,12 +50,10 @@ public class CamelRootHandlerTest {
/* now the other way round: register wsHandler and try to register httpHandler for the same path */
root.add("/app2", null, false, new CamelWebSocketHandler());
- try {
- root.add("/app2", null, false, httpHandler);
- fail(IllegalArgumentException.class.getName() + " expected");
- } catch (IllegalArgumentException expected) {
- }
-
+ CamelWebSocketHandler camelWebSocketHandler2 = new CamelWebSocketHandler();
+ assertThrows(IllegalArgumentException.class,
+ () -> root.add("/app2", null, false, httpHandler),
+ "IllegalArgumentException expected");
}
@Test
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpBindingModeJsonTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpBindingModeJsonTest.java
index 71809ef..934e644 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpBindingModeJsonTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpBindingModeJsonTest.java
@@ -25,7 +25,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 RestUndertowHttpBindingModeJsonTest extends BaseUndertowTest {
@@ -53,13 +53,9 @@ public class RestUndertowHttpBindingModeJsonTest extends BaseUndertowTest {
// we bind to json, but send in xml, which is not possible
String body = "<user name=\"Donald Duck\" id=\"123\"></user>";
- try {
- template.sendBody("http://localhost:" + getPort() + "/users/new", body);
- fail("Should have thrown exception");
- } catch (CamelExecutionException e) {
- // expected
- }
+ String uri = "http://localhost:" + getPort() + "/users/new";
+ assertThrows(CamelExecutionException.class, () -> template.sendBody(uri, body));
assertMockEndpointsSatisfied();
}