Skip to content

Commit c088d68

Browse files
committed
Propagate exceptions from java api operations
1 parent a47a627 commit c088d68

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

core/src/main/java/org/jsmart/zerocode/core/AddService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public Integer squareMyNumber(MyNumber myNumber) {
2121
return myNumber.getNumber() * myNumber.getNumber();
2222
}
2323

24+
public Double squareRoot(Double number) {
25+
if (number < 0.0)
26+
throw new RuntimeException("Can not square root a negative number");
27+
return Math.sqrt(number);
28+
}
29+
2430
public Integer anInteger() {
2531
logger.debug("Returning a number ");
2632

core/src/main/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Object executeWithParams(String qualifiedClassName, String methodName, Object...
111111
} catch (Exception e) {
112112
String errMsg = format("Java exec(): Invocation failed for method %s in class %s", methodName, qualifiedClassName);
113113
LOGGER.error(errMsg + ". Exception - " + e);
114-
throw new RuntimeException(errMsg);
114+
throw new RuntimeException(errMsg, e);
115115
}
116116
}
117117

core/src/test/java/org/jsmart/zerocode/core/engine/executor/javaapi/JavaMethodExecutorImplTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.google.inject.Guice;
55
import com.google.inject.Injector;
6+
import java.lang.reflect.InvocationTargetException;
67
import java.lang.reflect.Method;
78
import java.util.List;
89
import org.jsmart.zerocode.core.di.main.ApplicationMainModule;
@@ -16,6 +17,9 @@
1617

1718
import static org.hamcrest.MatcherAssert.assertThat;
1819
import static org.hamcrest.core.Is.is;
20+
import static org.hamcrest.core.StringContains.containsString;
21+
import static org.hamcrest.CoreMatchers.notNullValue;
22+
import static org.junit.Assert.assertThrows;
1923

2024
public class JavaMethodExecutorImplTest {
2125

@@ -124,6 +128,29 @@ public void willExecuteJsonRequestFor_CustomObject_java_method() throws Exceptio
124128
assertThat(result, is(900));
125129
}
126130

131+
@Test
132+
public void willPropagateExceptions() throws Exception {
133+
String scenariosJsonAsString = SmartUtils.readJsonAsString("unit_test_files/java_apis/03_test_json_java_service_method_Exception.json");
134+
final ScenarioSpec scenarioSpec = smartUtils.getMapper().readValue(scenariosJsonAsString, ScenarioSpec.class);
135+
136+
String serviceName = scenarioSpec.getSteps().get(0).getUrl();
137+
String methodName = scenarioSpec.getSteps().get(0).getOperation();
138+
String requestJson = scenarioSpec.getSteps().get(0).getRequest().toString();
139+
List<Class<?>> argumentTypes = methodExecutor.getParameterTypes(serviceName, methodName);
140+
141+
Object request = mapper.readValue(requestJson, argumentTypes.get(0));
142+
143+
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
144+
methodExecutor.executeWithParams(serviceName, methodName, request);
145+
});
146+
exception.printStackTrace();
147+
assertThat(exception.getMessage(), containsString("Invocation failed for method squareRoot"));
148+
// The target exception is included in this exception (inside of the InvocationTargetException)
149+
assertThat(exception.getCause(), is(notNullValue()));
150+
assertThat(((InvocationTargetException)exception.getCause()).getTargetException().getMessage(),
151+
is("Can not square root a negative number"));
152+
}
153+
127154
@Test
128155
public void willExecuteJsonWithParams_CustomObject_viaJson() throws Exception {
129156
String requestJson = "{\n" +
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"scenarioName": "Given_When_Then-Flow name For Java Service",
3+
"steps": [
4+
{
5+
"name": "javaMethodException",
6+
"url": "org.jsmart.zerocode.core.AddService",
7+
"operation": "squareRoot",
8+
"request": "-255.0", //<-- This negative number should throw an exception
9+
"assertions": {
10+
}
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)