How to handle exceptions thrown by a test method defined by TestNG

8,601 views
Skip to first unread message

sachin

unread,
Apr 22, 2012, 1:51:18 AM4/22/12
to testng-users
HI All,

I want to handle exceptions that are defined in the throws clause of a
method. The method is annotated with @Test, which makes it a test
method. So, my requirement is to handle the exceptions thrown by a
test method.. I would like to know the message, stack trace and other
information of that exception. I don't want to use try/ catch block
inside the test method.

Can anyone help me out??

Regards,

Sachin

Krishnan Mahadevan

unread,
Apr 22, 2012, 2:03:37 AM4/22/12
to testng...@googlegroups.com
If you would like to have a test method which tests whether some functionality of yours throws an exception or not, you can use expectedExceptions attribute of @Test annotation.

So the test method passes, if the exception that you are looking for as part of expectedExceptions is thrown, else TestNG fails the test method.


Would that help ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


sachin

unread,
Apr 22, 2012, 2:56:51 AM4/22/12
to testng-users
Thanks for the information Krishnan, but i don't want to test whether
test method throws an exception or not, instead, i want to know the
information about the exception(like its stacktrace, etc) if in case
the exception is thrown. For example, if WebDriver throws
NoSuchElementException then i want to have information about that
exception. Hope i'm clear with my requirement.

Regards,

Sachin




On Apr 22, 11:03 am, Krishnan Mahadevan

Krishnan Mahadevan

unread,
Apr 22, 2012, 3:01:07 AM4/22/12
to testng...@googlegroups.com
Then you are better off using try....catch block. 
I dont think TestNG provides you with a direct way of doing it without failing your Test. 

You can explore the TestNG listeners to look into the exceptions being thrown but like I said it is going to fail your test. 

Hope that helps. 


--
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/

Tomek Kaczanowski

unread,
Apr 22, 2012, 3:43:24 AM4/22/12
to testng...@googlegroups.com
You could also try catch-exception library (see
http://code.google.com/p/catch-exception/) which provides a very nice
API for handling exceptions - without try/catch blocks.

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com


W dniu 22 kwietnia 2012 09:01 użytkownik Krishnan Mahadevan
<krishnan.ma...@gmail.com> napisał:

Gangadhar Subramanian

unread,
Jul 9, 2014, 3:14:48 AM7/9/14
to testng...@googlegroups.com
Hi Joel,

Thanks for explaining it with example. Are you saying that this test will get passed though it throws an exception?

-Gangadhar

On Monday, April 23, 2012 12:05:46 AM UTC+5:30, Joel Bijapurkar wrote:
Hi Sachin, I had a similar problem and the following code works fine
for me.

Class containing test method:

package a;

import org.testng.annotations.Test;

public class NewTest
{
  @Test
  public void f() throws Exception
  {
          int a = 5/0;
  }
}

Class containing the call to the test method:

package a;

import org.testng.TestNG;

public class TestRunner
{
        public static void main(String args[])
        {
                TestNG testng1 = new TestNG();
                testng1.setTestClasses(new Class[]{NewTest.class});
                testng1.addListener(new CL2());
                testng1.run();
        }
}

I then created my own custom test listener CL2 that overrode the
methods of the ITestListener interface

public class CL2 implements ITestListener
{
        @Override
        public void onTestFailure(ITestResult arg0)
        {
                // TODO Auto-generated method stub
                System.out.println(arg0.getThrowable().getMessage());

                StringWriter sw = new StringWriter();

                arg0.getThrowable().printStackTrace(new
PrintWriter(sw));

                String stacktrace = sw.toString();

                System.out.println(stacktrace);
        }
}

In this way i was able to get the error message,the stacktrace and
using arg0.getParameters() I was even able to retrieve the parameters
that were passed to the test.

In the case of a try-catch block just convert the exception to an
error and throw the error the test listener will detect the test has
failed.

catch(Exception e)
{
    Error e1 = new Error(e.getMessage());
    e1.setStackTrace(e.getStackTrace());
    throw e1;
}

Hope this helps.
Joel

Mrunal Gosar

unread,
Jul 9, 2014, 11:16:05 AM7/9/14
to testng...@googlegroups.com
Hi Sachin
You can do one thing Implement ITestListener class. Implement onTestFailure message as shown. At the end when you have got all the necessary information from the throwable object, you can set the result to (1) based on your condition which will make the test pass if you wish to.

@Override
public void onTestFailure(ITestResult result)
{
// TODO Auto-generated method stub
System.out.println("Test Failed.");
System.out.println(result.getThrowable());
result.getThrowable().getStackTrace();
result.getThrowable().getMessage();
result.setStatus(1);
}

I hope this helps
Regards
Mrunal

SUBRAMANYESWARA RAO BHAVIRISETTY

unread,
Jul 9, 2014, 5:27:57 PM7/9/14
to testng-users
Hi Gangadhar,

  If you want the test case to pass,then we should put the exception in expectedException.

As per Testng documentation:

The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.

~Subramanyam




~Subramanyam


--
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 post to this group, send email to testng...@googlegroups.com.



--
Subramanyam
Reply all
Reply to author
Forward
Message has been deleted
0 new messages