Skip to content

Commit

Permalink
Merge pull request #690 from javiertuya/688-exceptions-java-api
Browse files Browse the repository at this point in the history
ISSUE-688 # Propagate exceptions from java api operations
  • Loading branch information
javiertuya authored Oct 25, 2024
2 parents a47a627 + c088d68 commit 9be3dba
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions core/src/main/java/org/jsmart/zerocode/core/AddService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<Class<?>> 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" +
Expand Down
Original file line number Diff line number Diff line change
@@ -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": {
}
}
]
}

0 comments on commit 9be3dba

Please sign in to comment.