R
Rhino
I think I understand the basics of using JUnit - I'm still using JUnit3
for the moment - but I want to ask some things that I don't find clear in
the documentation that I've found.
Would it be reasonable to say all of the following?
1. For each class that I develop, I should have one JUnit class that
extends TestCase and that this TestCase class should test each of the
methods in the class I developed?
2. When I look at the JUnit window in Eclipse to see the results of that
TestCase class, all of them should have a Green icon beside them? In
other words, should each test be written to show a positive result? That
would seem to make life a lot easier in one way: whenever I ran a
TestCase that was written that way, any bug would be easy to spot because
it didn't have the Green icon beside it.
3. Is a TestSuite just a group of TestCases that can all be invoked from
the same JUnit class? For instance, given a project Foo, would I
typically have a single TestSuite for Foo that, when invoked, runs the
TestCases for each of the classes that makes up Foo?
Basically, I'm having a bit of confusion over exactly how much you put in
each class that extends TestCase (sorry for the strange wording but I
want to distinguish between the class that is being tested and the class
that is doing the testing and don't know the most concise wording) and
what a TestSuite should comprise.
Also, I'm very uncertain about the individual methods in each class that
extends TestCase and what sorts of results they should return. Should all
test methods result in a Green icon?
I can see where that would be very convenient: you simply run the test
case and even if it has dozens of methods, you just scan down the list
and make sure all of them have Green icons. If they do, all your tests
just passed and you can move on to the next thing.
On the other hand, I'm not completely sure how to accomplish that. I'm
picturing a method like bar() which takes two input parameters. Whenever
the values of the two input parameters are valid, bar() should produce a
valid result, which I can predict and put in a test that would look like
this:
int actualResult = myclass.bar(4);
int expectedResult = 2;
assertTrue("The actual result, " + actualResult + ", does not equal the
expected result, " + expectedResult, actualResult==expectedResult);
But bar() also anticipates bad values for input parameters and throws
IllegalArgumentException for those. What would my test for that look
like? Any case I've ever written like that returns a Black icon. How
would I write a case involving an Exception so that it gives a Green icon
as the result?
for the moment - but I want to ask some things that I don't find clear in
the documentation that I've found.
Would it be reasonable to say all of the following?
1. For each class that I develop, I should have one JUnit class that
extends TestCase and that this TestCase class should test each of the
methods in the class I developed?
2. When I look at the JUnit window in Eclipse to see the results of that
TestCase class, all of them should have a Green icon beside them? In
other words, should each test be written to show a positive result? That
would seem to make life a lot easier in one way: whenever I ran a
TestCase that was written that way, any bug would be easy to spot because
it didn't have the Green icon beside it.
3. Is a TestSuite just a group of TestCases that can all be invoked from
the same JUnit class? For instance, given a project Foo, would I
typically have a single TestSuite for Foo that, when invoked, runs the
TestCases for each of the classes that makes up Foo?
Basically, I'm having a bit of confusion over exactly how much you put in
each class that extends TestCase (sorry for the strange wording but I
want to distinguish between the class that is being tested and the class
that is doing the testing and don't know the most concise wording) and
what a TestSuite should comprise.
Also, I'm very uncertain about the individual methods in each class that
extends TestCase and what sorts of results they should return. Should all
test methods result in a Green icon?
I can see where that would be very convenient: you simply run the test
case and even if it has dozens of methods, you just scan down the list
and make sure all of them have Green icons. If they do, all your tests
just passed and you can move on to the next thing.
On the other hand, I'm not completely sure how to accomplish that. I'm
picturing a method like bar() which takes two input parameters. Whenever
the values of the two input parameters are valid, bar() should produce a
valid result, which I can predict and put in a test that would look like
this:
int actualResult = myclass.bar(4);
int expectedResult = 2;
assertTrue("The actual result, " + actualResult + ", does not equal the
expected result, " + expectedResult, actualResult==expectedResult);
But bar() also anticipates bad values for input parameters and throws
IllegalArgumentException for those. What would my test for that look
like? Any case I've ever written like that returns a Black icon. How
would I write a case involving an Exception so that it gives a Green icon
as the result?