?
Solved

More Junit insanity!

Posted on 2005-05-09
6
Medium Priority
?
149 Views
Last Modified: 2010-03-31
Is there a way to cause junit to execute a method on failure?

If I have a test case that has several tests in it, and one fails, it goes on to the next one. The problem is that I am testing a web interface, and if it goes to the next test without first logging out, then the card won't let the second test in.

I need a way to detect the failure, and when it fails, force the logout, so the next tests are unhampered.

Thanks!
0
Comment
Question by:k41d3n
  • 5
6 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13963210
>> Is there a way to cause junit to execute a method on failure?
put your test logic in
try
{
// test logic which can throw AssertionFailedError
}
finally
{
// and here add the stuff you need to be done regardles if the test failed or not
}

0
 
LVL 15

Accepted Solution

by:
aozarov earned 2000 total points
ID: 13963675
BTW, you can also do that in the Test junit method setUp and tearDown which are called always per executed test. (which is more junit style)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13964982
so setUp can do the login and tearDown can do the logout (if a Test file contains 2 test methods setup and tearDown will be called on
each one of the methods before and after execution).
As for the finally option (non junit specific) I assume it was clear (adding here more regarding capturing the exception):

your test_function()
{
Throwable te = null;
try
{
log in and notify test start
do your test logic
}
catch (Error t) // junit error indication
{
te = t;
throw t;
}
finally
{
log out and notify test completed (you can use te which will be null in case of seccuss or not null if failed)
}
}

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:aozarov
ID: 13965028
Also, as disccuessed in the seperate thread you may want to consider extending junit.framework.TestResult and capture
all the information there including delegating the calls to log in/ log out. (see the source for TestResult#runProtected)
0
 
LVL 2

Author Comment

by:k41d3n
ID: 13967492
The setUp and tearDown worked as I hoped it would.

I know have a fully logging test running suite setup.

Boo Yah. Thanks for pointing it out to me.

500 points seems so little for the outcome of this situation.

Thanks a million times over.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13969399
NP. :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question