a simple junit question

W

www

Hi,

In my junit test

....//run the program first

//checking the results now
super.assertEquals(...); //first check

super.assertEquals(...); //second check


But if the first check is false, the program halts and gives the message
that the check is false. I hope to know if the second check is true or
not, even though the first one fails. Right now, I am doing:


public void setUp() {
... //run the program

}

public void testFirst() {
super.assertEquals(...); //first check
}

public void testSecond() {
super.assertEquals(...); //second check
}


Above satisfiers my needs. The problem is that the program is
un-necessarily run twice. So it takes a little more time. I think my
problem is very basic and common scenario in using junit test. Is there
a better way to do it?

Thank you for your help.
 
D

Daniel Pitts

Hi,

In my junit test

...//run the program first

//checking the results now
super.assertEquals(...); //first check

super.assertEquals(...); //second check

But if the first check is false, the program halts and gives the message
that the check is false. I hope to know if the second check is true or
not, even though the first one fails. Right now, I am doing:

public void setUp() {
... //run the program

}

public void testFirst() {
super.assertEquals(...); //first check

}

public void testSecond() {
super.assertEquals(...); //second check

}

Above satisfiers my needs. The problem is that the program is
un-necessarily run twice. So it takes a little more time. I think my
problem is very basic and common scenario in using junit test. Is there
a better way to do it?

Thank you for your help.

if the testFirst and testSecond reuse some of the same code, then put
that shared code into its method(s). Then you don't duplicate quite so
much.

In general though, if you have a failed test, you should fix that test
without worrying what other part of the same test fails.

Hope this helps.
Daniel.
 
M

Mike Schilling

www said:
Hi,

In my junit test

...//run the program first

//checking the results now
super.assertEquals(...); //first check

super.assertEquals(...); //second check


But if the first check is false, the program halts and gives the
message that the check is false. I hope to know if the second check
is true or not, even though the first one fails. Right now, I am
doing:

public void setUp() {
... //run the program

}

public void testFirst() {
super.assertEquals(...); //first check
}

public void testSecond() {
super.assertEquals(...); //second check
}


Above satisfiers my needs. The problem is that the program is
un-necessarily run twice. So it takes a little more time. I think my
problem is very basic and common scenario in using junit test. Is
there a better way to do it?

Junit ends the test on the first failure, period. This can be annoying, say
when you want to run a lengthy process and then check a number of results
and see all the ones that are incorrect.. There's no out-of-the-box way to
accomplish this. One solution is to write a class that implements the same
assertXXX() methods as TestCase, but, instead of throwing an
AssertionFailedError if they fail, simply notes that a failure occurred,
appends a text description of the failure to a list, and returns. After
having checked multiple things, call the checkForErrors() method of this
class, which will, if anything has failed, throw an AssertionFailedError
whose message describes all of the failures seen.
 
S

sentientholon

Hi,

In my junit test

...//run the program first

//checking the results now
super.assertEquals(...); //first check

super.assertEquals(...); //second check

But if the first check is false, the program halts and gives the message
that the check is false. I hope to know if the second check is true or
not, even though the first one fails. Right now, I am doing:

public void setUp() {
... //run the program

}

public void testFirst() {
super.assertEquals(...); //first check

}

public void testSecond() {
super.assertEquals(...); //second check

}

Above satisfiers my needs. The problem is that the program is
un-necessarily run twice. So it takes a little more time. I think my
problem is very basic and common scenario in using junit test. Is there
a better way to do it?

Thank you for your help.

This is a common scenario when your fixture (setUp()) takes a long
time to create. If possible, your setUp() method should set up as
little application state as necessary for the test to exercise. If
you set up more than that, you risk the possibility that other
unrelated application changes will suddenly cause your tests to fail.

If a minimal fixture still takes a long time to set up, there are
advanced techniques like mocking and stubbing that you can look into,
to isolate your test from environmental conditions that are affecting
the performance of your tests. Presumably, this is a unit test, not a
system/integration/regression test, correct? If so, other subsystems
like databases, servers, etc. should not be dependencies of your test,
and should be stubbed out to return expected values. Test each
component or feature in isolation, so that when a test starts failing,
you know exactly where to start looking.

If you are writing an integration test, then you *do* want the
external dependencies. In that case, having a fully automated test is
worth the wait.

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top