diff --git a/core/src/main/java/org/jsmart/zerocode/core/AddService.java b/core/src/main/java/org/jsmart/zerocode/core/AddService.java index 18c1e9b0b..e11937894 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/AddService.java +++ b/core/src/main/java/org/jsmart/zerocode/core/AddService.java @@ -21,6 +21,12 @@ public Integer squareMyNumber(MyNumber myNumber) { return myNumber.getNumber() * myNumber.getNumber(); } + public Double squareRoot(Double number) { + if (number < 0.0) + throw new RuntimeException("Can not square root a negative number"); + return Math.sqrt(number); + } + public Integer anInteger() { logger.debug("Returning a number "); diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImpl.java b/core/src/main/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImpl.java index 5eddfae9a..0d5d4f58c 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImpl.java +++ b/core/src/main/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImpl.java @@ -111,7 +111,7 @@ Object executeWithParams(String qualifiedClassName, String methodName, Object... } catch (Exception e) { String errMsg = format("Java exec(): Invocation failed for method %s in class %s", methodName, qualifiedClassName); LOGGER.error(errMsg + ". Exception - " + e); - throw new RuntimeException(errMsg); + throw new RuntimeException(errMsg, e); } } diff --git a/core/src/test/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImplTest.java b/core/src/test/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImplTest.java index 148de7f53..2b971687f 100644 --- a/core/src/test/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImplTest.java +++ b/core/src/test/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImplTest.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.Guice; import com.google.inject.Injector; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import org.jsmart.zerocode.core.di.main.ApplicationMainModule; @@ -16,6 +17,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.StringContains.containsString; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThrows; public class JavaMethodExecutorImplTest { @@ -124,6 +128,29 @@ public void willExecuteJsonRequestFor_CustomObject_java_method() throws Exceptio assertThat(result, is(900)); } + @Test + public void willPropagateExceptions() throws Exception { + String scenariosJsonAsString = SmartUtils.readJsonAsString("unit_test_files/java_apis/03_test_json_java_service_method_Exception.json"); + final ScenarioSpec scenarioSpec = smartUtils.getMapper().readValue(scenariosJsonAsString, ScenarioSpec.class); + + String serviceName = scenarioSpec.getSteps().get(0).getUrl(); + String methodName = scenarioSpec.getSteps().get(0).getOperation(); + String requestJson = scenarioSpec.getSteps().get(0).getRequest().toString(); + List> argumentTypes = methodExecutor.getParameterTypes(serviceName, methodName); + + Object request = mapper.readValue(requestJson, argumentTypes.get(0)); + + RuntimeException exception = assertThrows(RuntimeException.class, () -> { + methodExecutor.executeWithParams(serviceName, methodName, request); + }); + exception.printStackTrace(); + assertThat(exception.getMessage(), containsString("Invocation failed for method squareRoot")); + // The target exception is included in this exception (inside of the InvocationTargetException) + assertThat(exception.getCause(), is(notNullValue())); + assertThat(((InvocationTargetException)exception.getCause()).getTargetException().getMessage(), + is("Can not square root a negative number")); + } + @Test public void willExecuteJsonWithParams_CustomObject_viaJson() throws Exception { String requestJson = "{\n" + diff --git a/core/src/test/resources/unit_test_files/java_apis/03_test_json_java_service_method_Exception.json b/core/src/test/resources/unit_test_files/java_apis/03_test_json_java_service_method_Exception.json new file mode 100644 index 000000000..711f04b02 --- /dev/null +++ b/core/src/test/resources/unit_test_files/java_apis/03_test_json_java_service_method_Exception.json @@ -0,0 +1,13 @@ +{ + "scenarioName": "Given_When_Then-Flow name For Java Service", + "steps": [ + { + "name": "javaMethodException", + "url": "org.jsmart.zerocode.core.AddService", + "operation": "squareRoot", + "request": "-255.0", //<-- This negative number should throw an exception + "assertions": { + } + } + ] +}