Mihail Stoynov's blog!
Surrender your ego
Tuesday, July 08, 2008
JUnit, exceptions in @Before and @After methods
JUnit spec (not very easy to find) states, that if there's an exception in the
@Before
method, the test is not called. True.
BUT
JUnit spec does not state that in this case the @After method
is
called.
JUnit spec also does not state that if there's an exception in both the
@Before
and
@After
methods,
the second exception
(guess which one)
overrides the first one
. In my case the first one causes the second one (the first one is
ConnectionClosed
or similar, the second one is a
NullPointerException
because the resource is not initialized).
So the real reason for the problem is lost. One will say - avoid exceptions in the
@After
method - that's what I just did (and the real exception was printed), but finding the reason for that was not easy and straightforward.
I'm using junit 4.4, the default runner is
JUnit4ClassRunner
,
JUnit4ClassRunner
is calling
ClassRoadie.runProtected()
:
public void runProtected() {
try {
runBefores();
runUnprotected();
} catch (FailedBefore e) {
} finally {
runAfters();
}
}
This is the reason for the mentioned override. I hate when
finally
does that.
Sucks
Comments [0]
|
Trackback
OpenID
Please login with either your
OpenID
above, or your details below.
Name
E-mail
(will show your
gravatar
icon)
Home page
Remember Me
Comment (Some html is allowed:
a@href@title, blockquote@cite, strike
) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.
Enter the code shown (prevents robots):
Live Comment Preview
dasBlog theme by
Mads Kristensen
RSS feed
Search
Archives
November, 2008 (15)
October, 2008 (16)
September, 2008 (30)
August, 2008 (15)
July, 2008 (14)
June, 2008 (26)
May, 2008 (6)
April, 2008 (21)
March, 2008 (14)
February, 2008 (28)
November, 2007 (5)
October, 2007 (7)
September, 2007 (1)
August, 2007 (7)
July, 2007 (3)
June, 2007 (1)
Blog Stats
Total Posts: 203
This Year: 179
This Month: 0
This Week: 0
Comments: 80
Categories
Did you know
Java
rulez
Sucks
БГ
Blogroll
Michael Moore (no rss)
Links
BG-JUG
Copyright policy
No rights reserved.
(You are going to
copy stuff anyway :)
If you mention my
name, thank you.
2008, Mihail Stoynov