Changing throwable message

29 views
Skip to first unread message

Baubak

unread,
Aug 8, 2021, 3:13:55 PM8/8/21
to testng-users

Hi there,

I would like to enrich the throwable messages.

The idea is to add additional information in the exceptiion message.

However it seems to me that we cannot modify the throwable messsage.

using the TestListener and the inFinishMethod I perform:

context.getFailedTests().getAllResults().iterator().next().getThrowable().????

As you know there is no setter for the TestNGResult. Do you have any suggestions?

Best regards,

Baubak


⇜Krishnan Mahadevan⇝

unread,
Aug 8, 2021, 11:50:38 PM8/8/21
to testng-users
Would it help if you tried catching the exception and then used ITestResult.setThrowable() to basically push in a new exception that captures an enhanced message ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/7aad5e3c-66c9-453a-ac85-559e3fc0fd35n%40googlegroups.com.

Baubak

unread,
Aug 9, 2021, 3:09:55 PM8/9/21
to testng-users
Thanks for the suggestion Krishnan,

I did something similar. I wrapped the original exception in a new Throwable of my making (MY_OWN_THROWABLE).

In my onFinish(context):

Iterator<ITestResult> lt_failedTestIterator = context.getFailedTests().getAllResults()
                            .iterator();
                    while (lt_failedTestIterator.hasNext()) {
                        ITestResult lt_currentFail = lt_failedTestIterator.next();

                       StringBuilder sb = new StringBuilder("MY PREFIX ");
                      sb.append(lt_currentFail.getMethod().getMethodName());
                      sb.append(" - ");
                      sb.append(lt_currentFail.getThrowable().getClass().getName());
                      sb.append(": ");
                      sb.append(lt_currentFail.getThrowable().getMessage());       

                   lt_currentFail.setThrowable(new MY_OWN_THROWABLE(sb.toString(), lt_currentFail.getThrowable()));
                           
                        }
                    }

Maybe I could have actually thrown an exception in the onFailure() method maybe?

⇜Krishnan Mahadevan⇝

unread,
Aug 10, 2021, 1:31:50 AM8/10/21
to testng-users
I dont think you should be throwing exceptions from listeners. AFAIK listeners are not supposed to be throwing exceptions and even if they do, it doesnt affect the test result of a particular test, but it would perhaps affect the entire execution, because listeners are not specific to tests, but they are available to all tests across the board for a given execution.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

akash gupta

unread,
Aug 10, 2021, 7:33:10 AM8/10/21
to testng...@googlegroups.com
Implement IInvokedMethodListener:

public
class InvokedMethodListener implements IInvokedMethodListener { @Override public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { } @Override public void afterInvocation(IInvokedMethod method, ITestResult result) { if (method.isTestMethod() && ITestResult.FAILURE == result.getStatus()) { Throwable throwable = result.getThrowable(); String originalMessage = throwable.getMessage(); String newMessage = originalMessage + "\nReproduction Seed: ...\nCountry: ..."; try { FieldUtils.writeField(throwable, "detailMessage", newMessage, true); } catch (Exception e) { e.printStackTrace(); } } } }
Register it in your test:
@Listeners(InvokedMethodListener.class)
public class YourTest {
    @Test
    public void test() {
        Assert.fail("some message");
    }
}
or in testng.xml.
If you execute it, you should get:
java.lang.AssertionError: some message
Reproduction Seed: ...
Country: ...
-- 
Best regards

Akash Gupta


image.gif
Mailtrack Sender notified by
Mailtrack
10/08/21, 16:47:39

Baubak

unread,
Aug 12, 2021, 2:51:12 AM8/12/21
to testng-users
Thanks for the input Akash,

For other reasons I have implemented it in ITestListener. But thanks for the input regarding Apache commmons FieldUtils.writeField. I didn't know of this library. IT seems lika a good alernative.

Best regards,

Baubak
Reply all
Reply to author
Forward
0 new messages