Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » SWTBot » Detect exceptions throws during test
Detect exceptions throws during test [message #540155] Tue, 15 June 2010 08:20 Go to next message
Caroline Bourdeu d'Aguerre is currently offline Caroline Bourdeu d'Aguerre
Messages: 6
Registered: July 2009
Junior Member
Hello,

I am running a SWTBot that throw a Null Pointer Exception. But the test finish well with no failure.

If I look at the runtime workspace, the exception is visible in the error log.

If I suround the instruction that throw the exception with a try/catch the catch block is never reached.

How I can do to make my test failed if an exception is throws?
Does the dialog that usally show the exception in eclipse is disable by SWTBot?

Thanks for your help in advance.
Caroline
Re: Detect exceptions throws during test [message #540265 is a reply to message #540155] Tue, 15 June 2010 13:39 Go to previous messageGo to next message
Pascal G is currently offline Pascal G
Messages: 157
Registered: July 2009
Senior Member
On 10-06-15 04:20 AM, Caroline Bourdeu d'Aguerre wrote:
> Hello,
>
> I am running a SWTBot that throw a Null Pointer Exception. But the test
> finish well with no failure.
>
> If I look at the runtime workspace, the exception is visible in the
> error log.
>
> If I suround the instruction that throw the exception with a try/catch
> the catch block is never reached.
>
> How I can do to make my test failed if an exception is throws?
> Does the dialog that usally show the exception in eclipse is disable by
> SWTBot?
>
> Thanks for your help in advance.
> Caroline

The exception is probably thrown in the UI thread, not the thread SWTBot
is running in. So SWTBot can't catch the exception, the UI thread
catches it instead and that's why it is shown in the error log.

The clean way would be the take a look at Eclipse's log and fail if
something is reported. However, what I do is I make sure that the Error
Log is opened during my tests, and, at the end of each tests (tearDown
method or @After annotation), I check in this view if there is an error
and fail accordingly.

You just made me thought that I wanted to clean this part of my testing
procedure, stay tuned for an update. I'll try to make it as generic as
possible so it could be reused as part of SWTBot.

Hope this helps.
--
Pascal Gélinas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/

*Because performance matters.*
Re: Detect exceptions throws during test [message #540655 is a reply to message #540265] Wed, 16 June 2010 20:40 Go to previous messageGo to next message
Pascal G is currently offline Pascal G
Messages: 157
Registered: July 2009
Senior Member
As promised, I have returned! Here is some code that could be integrated
into SWTBot, as part of a TestHelper or whatever. Let me break it down
for you (althought it's pretty simple, actually).

private static final LogListener LISTENER = new LogListener();

private static final class LogListener implements ILogListener
{
private final List<IStatus> mStatus = new ArrayList<IStatus>();

public void logging(IStatus status, String plugin)
{
mStatus.add(status);
}
}

@BeforeClass
public static void registerLog()
{
getLog().addLogListener(LISTENER);
}

@AfterClass
public static void removeListener()
{
getLog().removeLogListener(LISTENER);
}

private static ILog getLog()
{
return Platform.getLog(Platform.getBundle(SwtBotPlugin.PLUGIN_ID));
// SwtBotPlugin is our own plugin which has all sort of utilities. I had
to put somthing here...
}

// A small test case to proof that it is working
@Test(expected = NullPointerException.class)
public void testLogging() throws Throwable
{
// This should be done in a @Before or setUp method. To make sure that
errors that happened in the other tests don't pollute this test.
LISTENER.mStatus.clear();
// Just to test
getLog().log(new Status(IStatus.ERROR,
SwtBotPlugin.PLUGIN_ID,
"fake error",
new NullPointerException("Fake NPE")));
// This should be done in a @After or tearDown method.
for (IStatus status : LISTENER.mStatus)
{
if (status.getException() != null)
{
// Since it's a list, the first exception that gets logged will be thrown.
throw status.getException();
}
}
}

That's it. I was amazed how simple it was to do this... I'll fill a bug
as a feature requeste to have this integrated somehow.

Hope this helps.
--
Pascal Gélinas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/

*Because performance matters.*
Re: Detect exceptions throws during test [message #540657 is a reply to message #540655] Wed, 16 June 2010 21:10 Go to previous messageGo to next message
Ketan Padegaonkar is currently offline Ketan Padegaonkar
Messages: 873
Registered: July 2009
Senior Member
Awesome stuff!

Could you put this on the snippets page :)

--
Ketan
http://ketan.padegaonkar.name | http://eclipse.org/swtbot

On 6/16/10 1:40 PM, Pascal Gelinas wrote:
> As promised, I have returned! Here is some code that could be integrated
> into SWTBot, as part of a TestHelper or whatever. Let me break it down
> for you (althought it's pretty simple, actually).
>
> private static final LogListener LISTENER = new LogListener();
>
> private static final class LogListener implements ILogListener
> {
> private final List<IStatus> mStatus = new ArrayList<IStatus>();
>
> public void logging(IStatus status, String plugin)
> {
> mStatus.add(status);
> }
> }
>
> @BeforeClass
> public static void registerLog()
> {
> getLog().addLogListener(LISTENER);
> }
>
> @AfterClass
> public static void removeListener()
> {
> getLog().removeLogListener(LISTENER);
> }
>
> private static ILog getLog()
> {
> return Platform.getLog(Platform.getBundle(SwtBotPlugin.PLUGIN_ID));
> // SwtBotPlugin is our own plugin which has all sort of utilities. I had
> to put somthing here...
> }
>
> // A small test case to proof that it is working
> @Test(expected = NullPointerException.class)
> public void testLogging() throws Throwable
> {
> // This should be done in a @Before or setUp method. To make sure that
> errors that happened in the other tests don't pollute this test.
> LISTENER.mStatus.clear();
> // Just to test
> getLog().log(new Status(IStatus.ERROR,
> SwtBotPlugin.PLUGIN_ID,
> "fake error",
> new NullPointerException("Fake NPE")));
> // This should be done in a @After or tearDown method.
> for (IStatus status : LISTENER.mStatus)
> {
> if (status.getException() != null)
> {
> // Since it's a list, the first exception that gets logged will be thrown.
> throw status.getException();
> }
> }
> }
>
> That's it. I was amazed how simple it was to do this... I'll fill a bug
> as a feature requeste to have this integrated somehow.
>
> Hope this helps.
Re: Detect exceptions throws during test [message #540741 is a reply to message #540265] Thu, 17 June 2010 08:43 Go to previous messageGo to next message
Mariot Chauvin is currently offline Mariot Chauvin
Messages: 174
Registered: July 2009
Senior Member
Pascal Gelinas a écrit :
> On 10-06-15 04:20 AM, Caroline Bourdeu d'Aguerre wrote:
>> Hello,
>>
>> I am running a SWTBot that throw a Null Pointer Exception. But the test
>> finish well with no failure.
>>
>> If I look at the runtime workspace, the exception is visible in the
>> error log.
>>
>> If I suround the instruction that throw the exception with a try/catch
>> the catch block is never reached.
>>
>> How I can do to make my test failed if an exception is throws?
>> Does the dialog that usally show the exception in eclipse is disable by
>> SWTBot?
>>
>> Thanks for your help in advance.
>> Caroline
>
> The exception is probably thrown in the UI thread, not the thread SWTBot
> is running in. So SWTBot can't catch the exception, the UI thread
> catches it instead and that's why it is shown in the error log.
>
> The clean way would be the take a look at Eclipse's log and fail if
> something is reported. However, what I do is I make sure that the Error
> Log is opened during my tests, and, at the end of each tests (tearDown
> method or @After annotation), I check in this view if there is an error
> and fail accordingly.
>
> You just made me thought that I wanted to clean this part of my testing
> procedure, stay tuned for an update. I'll try to make it as generic as
> possible so it could be reused as part of SWTBot.
>
> Hope this helps.

There is another clean way to handle exceptions which occur on other threads :


private synchronized boolean doesAnErrorOccurs() {
return error;
}

private synchronized void errorOccurs() {
error = true;
}


public void yourTest() throws Exception() {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
errorOccurs();
}
});

....
....
test what you want
....
....
/* check if an exception occurs in another thread */
if (doesAnErrorOccurs())
fail();

}

It may help.

Regards,

Mariot
Re: Detect exceptions throws during test [message #541158 is a reply to message #540741] Fri, 18 June 2010 13:33 Go to previous messageGo to next message
Pascal G is currently offline Pascal G
Messages: 157
Registered: July 2009
Senior Member
On 10-06-17 04:43 AM, Mariot Chauvin wrote:
> Pascal Gelinas a écrit :
>> On 10-06-15 04:20 AM, Caroline Bourdeu d'Aguerre wrote:
>>> Hello,
>>>
>>> I am running a SWTBot that throw a Null Pointer Exception. But the test
>>> finish well with no failure.
>>>
>>> If I look at the runtime workspace, the exception is visible in the
>>> error log.
>>>
>>> If I suround the instruction that throw the exception with a try/catch
>>> the catch block is never reached.
>>>
>>> How I can do to make my test failed if an exception is throws?
>>> Does the dialog that usally show the exception in eclipse is disable by
>>> SWTBot?
>>>
>>> Thanks for your help in advance.
>>> Caroline
>>
>> The exception is probably thrown in the UI thread, not the thread SWTBot
>> is running in. So SWTBot can't catch the exception, the UI thread
>> catches it instead and that's why it is shown in the error log.
>>
>> The clean way would be the take a look at Eclipse's log and fail if
>> something is reported. However, what I do is I make sure that the Error
>> Log is opened during my tests, and, at the end of each tests (tearDown
>> method or @After annotation), I check in this view if there is an error
>> and fail accordingly.
>>
>> You just made me thought that I wanted to clean this part of my testing
>> procedure, stay tuned for an update. I'll try to make it as generic as
>> possible so it could be reused as part of SWTBot.
>>
>> Hope this helps.
>
> There is another clean way to handle exceptions which occur on other threads :
>
>
> private synchronized boolean doesAnErrorOccurs() {
> return error;
> }
>
> private synchronized void errorOccurs() {
> error = true;
> }
>
>
> public void yourTest() throws Exception() {
> Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
> public void uncaughtException(Thread t, Throwable e) {
> errorOccurs();
> }
> });
>
> ...
> ...
> test what you want
> ...
> ...
> /* check if an exception occurs in another thread */
> if (doesAnErrorOccurs())
> fail();
>
> }
>
> It may help.
>
> Regards,
>
> Mariot

I don't think they are actually uncaught exception, since the Eclipse
runtime catches them and displays them in the platform log. Otherwise,
yes, that would be the #1 way IMHO.

--
Pascal Gélinas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/

*Because performance matters.*
Re: Detect exceptions throws during test [message #541168 is a reply to message #540657] Fri, 18 June 2010 14:13 Go to previous messageGo to next message
Pascal G is currently offline Pascal G
Messages: 157
Registered: July 2009
Senior Member
On 10-06-16 05:10 PM, Ketan Padegaonkar wrote:
> private static final LogListener LISTENER = new LogListener();
>
> private static final class LogListener implements ILogListener
> {
> private final List<IStatus> mStatus = new ArrayList<IStatus>();
>
> public void logging(IStatus status, String plugin)
> {
> mStatus.add(status);
> }
> }
>
> @BeforeClass
> public static void registerLog()
> {
> getLog().addLogListener(LISTENER);
> }
>
> @AfterClass
> public static void removeListener()
> {
> getLog().removeLogListener(LISTENER);
> }
>
> private static ILog getLog()
> {
> return Platform.getLog(Platform.getBundle(SwtBotPlugin.PLUGIN_ID));
> // SwtBotPlugin is our own plugin which has all sort of utilities. I had
> to put somthing here...
> }
>
> // A small test case to proof that it is working
> @Test(expected = NullPointerException.class)
> public void testLogging() throws Throwable
> {
> // This should be done in a @Before or setUp method. To make sure that
> errors that happened in the other tests don't pollute this test.
> LISTENER.mStatus.clear();
> // Just to test
> getLog().log(new Status(IStatus.ERROR,
> SwtBotPlugin.PLUGIN_ID,
> "fake error",
> new NullPointerException("Fake NPE")));
> // This should be done in a @After or tearDown method.
> for (IStatus status : LISTENER.mStatus)
> {
> if (status.getException() != null)
> {
> // Since it's a list, the first exception that gets logged will be thrown.
> throw status.getException();
> }
> }
> }

Done: http://wiki.eclipse.org/SWTBot/Snippets

I also made some cleanup, so you might want to add the class into SWTBot
to make it easily available.

p.s.: I also added a link to the snippets page, 'cause nothing was
linking to it ;)
--
Pascal Gélinas | Software Developer
*Nu Echo Inc.*
http://www.nuecho.com/ | http://blog.nuecho.com/

*Because performance matters.*
Re: Detect exceptions throws during test [message #541988 is a reply to message #540155] Wed, 23 June 2010 13:08 Go to previous message
Caroline Bourdeu d'Aguerre is currently offline Caroline Bourdeu d'Aguerre
Messages: 6
Registered: July 2009
Junior Member
Thank you very much for your answers!

I have tried with the snippet code and it work fine. Because I need to detect all Exceptions and it is not possible to know which plug-in will throws the exceptions I have register to the logger all the plug-ins this way:

    @BeforeClass
    public static void registerLog()
    {
        Platform.addLogListener(LISTENER);
    }
   
    @AfterClass
    public static void unregisterLog()
    {
      Platform.removeLogListener(LISTENER);
    }


It is exactly what I needed so thank you all for your help.

Caroline
Previous Topic:CTab Item double click
Next Topic:SWTBot + Selenium for SWT & browser testing?
Goto Forum:
  


Current Time: Thu Oct 23 16:08:10 GMT 2014

Powered by FUDForum. Page generated in 0.02572 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software