Home » Eclipse Projects » SWTBot » Detect exceptions throws during test
|
Re: Detect exceptions throws during test [message #540265 is a reply to message #540155] |
Tue, 15 June 2010 13:39 |
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 #540657 is a reply to message #540655] |
Wed, 16 June 2010 21:10 |
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 |
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 |
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.*
|
|
| | |
Goto Forum:
Current Time: Tue Sep 24 09:16:54 GMT 2024
Powered by FUDForum. Page generated in 0.04124 seconds
|