• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Junit test unable to catch exceptions thrown from EJB

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My unit test code is as follows -

@Test(expected=InvalidOperationException.class)
pubic void testLogin()
{
int sessionId = startSession();
mLoginBean.login("TEST");
fail();
}

The login bean throws an InvalidOperationException that whwich I see on the Jboss console, but when I run the thejunit test case I get an assertion error from the fail() statement.\

I tried making of a variant of the test case as follows -

@Test
pubic void testLogin()
{
int sessionId = startSession();
try
{
mLoginBean.login("TEST");
fail();
}
catch(InvalidOperationException e)
{
e.printStackTrace();
}
}
I still get an asserstion error even though the exception is thrown from the bean.

Any ideas?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have a "fail" statement? The expected=InvalidOperationException.class tells JUnit to fail the test if the code doesn't throw the specified exception. And apparently it is smart enough to make sure it throws the exception from the last line of code in the method.
 
Anurag Bhatia
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed the fail statement like you said, but I can see the exception thrown by the bean in the jboss console, somehow junit does not detect the exception thrown from the bean.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you are getting a different exception in the test code. Maybe a remote exception? The way to find out is to try:


See what exception it thinks it is catching. If you share that, you are likely to get a more specific answer.
 
Anurag Bhatia
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried doing that as well. The issue is that it never enters the catch block. I cannot figure out why.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anurag Bhatia wrote:I tried doing that as well. The issue is that it never enters the catch block. I cannot figure out why.


So you are saying it does NOT throw an exception at all?
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic