You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Matt Raible <ma...@raibledesigns.com> on 2002/12/11 10:32:56 UTC

Is it possible to test for exceptions?

Is it possible to test for exceptions?  I want to verify that an
exception is thrown when a user is not found - right now my test fails.

I'd love an assertException or something of that sort ;)

[junit] Testcase:
testGetUser(org.apache.persistence.ApplicationDAOHibernateTest):  Caused
an ERROR
[junit] org.apache.persistence.DAOException: User not found in database!
[junit] org.apache.persistence.DAOException:
org.apache.persistence.DAOException: User not found in database!
[junit]     at
org.apache.persistence.ApplicationDAOHibernate.getUser(ApplicationDAOHib
ernate.java:74)
[junit]     at
org.apache.persistence.ApplicationDAOHibernateTest.testGetUser(Applicati
onDAOHibernateTest.java:54)
[junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
[junit]     at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)
[junit]     at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)
[junit] Caused by: org.apache.persistence.DAOException: User not found
in database!
[junit]     at
org.apache.persistence.ApplicationDAOHibernate.getUser(ApplicationDAOHib
ernate.java:60)
[junit]     ... 15 more


[junit] TEST org.apache.persistence.ApplicationDAOHibernateTest FAILED

Thanks,

Matt

Re: Is it possible to test for exceptions?

Posted by Erik Hatcher <li...@ehatchersolutions.com>.
duh, obviously the below logic is not quite right since it would always 
fail.  but you could set a flag in the catch and check it afterwards, or 
return within the catch if thats the only thing that test was doing. 
you get the idea!

Erik Hatcher wrote:
> This is normally done with a construct like this:
> 
> try {
>   // stuff that causes expected exception
> }
> catch (...) {
>   // expected
> }
> fail("did not get exception expected");
> 
> 
> 
> 
> Matt Raible wrote:
> 
>> Is it possible to test for exceptions?  I want to verify that an
>> exception is thrown when a user is not found - right now my test fails.
>>
>> I'd love an assertException or something of that sort ;)
>>
>> [junit] Testcase:
>> testGetUser(org.apache.persistence.ApplicationDAOHibernateTest):  Caused
>> an ERROR
>> [junit] org.apache.persistence.DAOException: User not found in database!
>> [junit] org.apache.persistence.DAOException:
>> org.apache.persistence.DAOException: User not found in database!
>> [junit]     at
>> org.apache.persistence.ApplicationDAOHibernate.getUser(ApplicationDAOHib
>> ernate.java:74)
>> [junit]     at
>> org.apache.persistence.ApplicationDAOHibernateTest.testGetUser(Applicati
>> onDAOHibernateTest.java:54)
>> [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
>> Method)
>> [junit]     at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
>> a:39)
>> [junit]     at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
>> Impl.java:25)
>> [junit] Caused by: org.apache.persistence.DAOException: User not found
>> in database!
>> [junit]     at
>> org.apache.persistence.ApplicationDAOHibernate.getUser(ApplicationDAOHib
>> ernate.java:60)
>> [junit]     ... 15 more
>>
>>
>> [junit] TEST org.apache.persistence.ApplicationDAOHibernateTest FAILED
>>
>> Thanks,
>>
>> Matt
>>
> 
> 
> --
> To unsubscribe, e-mail:   
> <ma...@jakarta.apache.org>
> For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
> 
> 
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Is it possible to test for exceptions?

Posted by Erik Hatcher <li...@ehatchersolutions.com>.
This is normally done with a construct like this:

try {
   // stuff that causes expected exception
}
catch (...) {
   // expected
}
fail("did not get exception expected");




Matt Raible wrote:
> Is it possible to test for exceptions?  I want to verify that an
> exception is thrown when a user is not found - right now my test fails.
> 
> I'd love an assertException or something of that sort ;)
> 
> [junit] Testcase:
> testGetUser(org.apache.persistence.ApplicationDAOHibernateTest):  Caused
> an ERROR
> [junit] org.apache.persistence.DAOException: User not found in database!
> [junit] org.apache.persistence.DAOException:
> org.apache.persistence.DAOException: User not found in database!
> [junit]     at
> org.apache.persistence.ApplicationDAOHibernate.getUser(ApplicationDAOHib
> ernate.java:74)
> [junit]     at
> org.apache.persistence.ApplicationDAOHibernateTest.testGetUser(Applicati
> onDAOHibernateTest.java:54)
> [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
> [junit]     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
> a:39)
> [junit]     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
> Impl.java:25)
> [junit] Caused by: org.apache.persistence.DAOException: User not found
> in database!
> [junit]     at
> org.apache.persistence.ApplicationDAOHibernate.getUser(ApplicationDAOHib
> ernate.java:60)
> [junit]     ... 15 more
> 
> 
> [junit] TEST org.apache.persistence.ApplicationDAOHibernateTest FAILED
> 
> Thanks,
> 
> Matt
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>