authorjapps / zerocode

A community-developed, free, opensource, automated testing framework for microservices APIs, Kafka(Data Streams) and Load testing. Zerocode Open Source enables you to create, change and maintain your automated test scenarios via simple JSON or YAML files. Visit documentation below:
https://zerocode-tdd.tddfy.com
Apache License 2.0
909 stars 397 forks source link

Exceptions thrown from classes executed by the java api are not propagated #688

Closed javiertuya closed 1 month ago

javiertuya commented 1 month ago

When a java api operation throws an exception, the exception message and stacktrace are not shown as part of the scenario stacktrace, making it difficult to debug.

To reproduce:

Expected: Some information about the RuntimeException INTENTIONAL EXCEPTION, and its stacktrace should be shown Actual: It is ommited. This is the stacktrace:

2024-10-20 11:55:44,184 [main] ERROR org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl - Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService. Exception - java.lang.reflect.InvocationTargetException
2024-10-20 11:55:44,184 [main] ERROR org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl - Exception - java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
2024-10-20 11:55:44,185 [main] ERROR org.jsmart.zerocode.core.engine.executor.ApiServiceExecutorImpl - Java method execution exception - java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
    at org.jsmart.zerocode.core.engine.executor.ApiServiceExecutorImpl.executeJavaOperation(ApiServiceExecutorImpl.java:53)
    at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeApi(ZeroCodeMultiStepsScenarioRunnerImpl.java:471)
    at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeRetry(ZeroCodeMultiStepsScenarioRunnerImpl.java:256)
    at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeRetryWithSteps(ZeroCodeMultiStepsScenarioRunnerImpl.java:195)
    at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeSteps(ZeroCodeMultiStepsScenarioRunnerImpl.java:177)
    at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.runScenario(ZeroCodeMultiStepsScenarioRunnerImpl.java:140)
(...)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
    at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.execute(JavaMethodExecutorImpl.java:63)
    at org.jsmart.zerocode.core.engine.executor.ApiServiceExecutorImpl.executeJavaOperation(ApiServiceExecutorImpl.java:50)
    ... 22 more
Caused by: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
    at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.executeWithParams(JavaMethodExecutorImpl.java:114)
    at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.execute(JavaMethodExecutorImpl.java:47)
    ... 23 more
2024-10-20 11:55:44,186 [main] ERROR org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - ###Exception while executing a step in the zerocode dsl.
2024-10-20 11:55:44,188 [main] ERROR org.jsmart.zerocode.core.runner.StepNotificationHandler - Exception occurred while executing Scenario:[Given_When_Then-Flow name For Java Service], --> Step:[javaMethodExecution], Details: java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
2024-10-20 11:55:44,193[main] org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl -m
javiertuya commented 1 month ago

I've located the cause: org.jsmart.zerocode.core.engine.executor.javaapi.executeWithParams catches exceptions and rethrows a new RuntimeException with the message only.

If adding both the message and catched exception to the throw statement, the stacktrace is augmented with:

Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.executeWithParams(JavaMethodExecutorImpl.java:107)
    ... 24 more
Caused by: java.lang.RuntimeException: INTENTIONAL EXCEPTION
    at org.jsmart.zerocode.core.AddService.anInteger(AddService.java:27)
    ... 29 more

I will prepare a PR to fix it. @authorjapps can you please assign this to me?

authorjapps commented 1 month ago

I've located the cause: org.jsmart.zerocode.core.engine.executor.javaapi.executeWithParams catches exceptions and rethrows a new RuntimeException with the message only.

If adding both the message and catched exception to the throw statement, the stacktrace is augmented with:

Caused by: java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.executeWithParams(JavaMethodExecutorImpl.java:107)
  ... 24 more
Caused by: java.lang.RuntimeException: INTENTIONAL EXCEPTION
  at org.jsmart.zerocode.core.AddService.anInteger(AddService.java:27)
  ... 29 more

I will prepare a PR to fix it. @authorjapps can you please assign this to me?

yep, that's right. Sounds amazing. Please go ahead 🚀 Issue assigned to you now.

Also, added you now as collaborator. Click here to accept. Now, you can,

Also, can you add relevant logs as ERROR level for the DB executor, to surface important exception messages etc to the end user? This will be very helpful to Devs and SDETs to avoid rabbit holes when any problem arises.

(Ignore me if you've already thought of doing it anyways, as part of this ticket :) )

javiertuya commented 1 month ago

@authorjapps Great!! Thank you.

Also, can you add relevant logs as ERROR level for the DB executor, to surface important exception messages etc to the end user? This will be very helpful to Devs and SDETs to avoid rabbit holes when any problem arises.

I did, but because of this issue, I logged the messages and exceptions, which generates the needed stacktrace to locate failures (e.g. sql syntax, or invalid CSV cells). After fixing this issue, I think that only logging the error messages will be enough. I will check it.