Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-688 # Propagate exceptions from java api operations #690

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {
}
}
]
}
Loading