Hi everyone,
Today I got bored and while I was checking out the Sun's Java developers
webpage (http://java.sun.com/)
I got my attention drawn to an interview
with
Cay Horstmann. While the interview itself sort of bored me I did pick up a
small section where they talked about unit testings and referred to
another
previous interview which basically pointed out that not many people seem
to be
using this, see also:
http://java.sun.com/developer/technicalArticles/Interviews/community/kabutz_qa.html.
(the section "The im****tance of unit testing").
Netbeans has sup****ted junit from the moment I started using it. I already
got
as far as to realizing that it needed a 'test' subdirectory in the main
project
directory but didn't really follow up on it. Not untill today anyway..
So now I'm wondering if unit testing really is providing all the
advantages
some people rave on about. In particular if spending the time to setup a
junit
test would be time better spend than to, say, write a specific testcase
for the
program you're developing.
Ofcourse there is a problem with discussing all this; there are many
websites
out there which will show you the way to implement and utilize junit in
your
own programs. But there are very few which will do so much as dive into
the
whole philosophy behind all those tests. I have discovered a few and one
article on the Java Boutique really provides some solid examples with
regards
to JUnit (IMO ofcourse). Check out
http://javaboutique.internet.com/tutorials/UnitTesting/index.html.
At first (first page and first paragraph of second page) the author tries
to
make us believe that the test code written on the first page could be
reduced
to 3 lines. Reading on however clearly shows us that there is much more
code
involved, the Course class instance still needs to be initialized and fed
with
the appropiate information.
The real advantage here basically boils down to not having to write (too)
much
code. So, instead of the authors test example:
....
if (c.parUpToHole(0) != 0 {
System.out.println("** Error in Course.parUpToHole: par for 0 holes not
0");
}
if (c.parUpToHole(2) != 8 {
System.out.println("** Error in Course.parUpToHole: par for 2 holes not
8");
}
....
this code is reduced to:
assertEquals(0, c.parUoToHole(0));
assertEquals(8, c.parUoToHole(2));
Now, granted, this is but a single example but does this really show the
JUnit
advantage here? Lets not forget that JUnit is merely a testing framework,
but
that doesn't automatically mean or imply that unit tests can only be done
while
using this framework. After all; a unit test is merely testing to see if a
class does as is desired. So taking the whole program used in the example
one
could argue that a mere:
public class CourseTest {
private Course c;
private static void test(int value, int result) {
if (value != c.parUpToHole(result)} {
System.out.println("Error in Course.parUpToHole: par for " + result
+ \
" holes not " + value);
}
}
public static void main(String[] args) {
c = new Course();
c.setName("Java Course");
int[] par = {4,4,4,4,5,4,4,3,4,4,3,4,4,5,4,4,4,4};
c.setPar{par};
test(0, 0);
test(8, 2);
test(72, 18);
}
} // End of class
Personally I'm tempted not to address this small snipplet of code as a
unit
test but merely a so called test case (see also
http://javafaq.mine.nu/lookup?364).
So in case anyone would ask me if I'd
be
doing unit tests I'd also tell them no. But how true is this ?
But diving more into the matter; where is the advantage of using a testing
framework like junit over writing your own specific test cases (whether
they're
programs or merely extra debug code) ? I'm even tempted to say that /not/
using
a simple pre-defined framework and setting something up manually which is
also
specific aimed at your project might even be a better solution in the
longer
run.
So summing up; do you see any specific advantages of using a testing
framework
like JUnit to be utilized in unit tests over writing specific testcode ?
Personally I'm tempted (even though I haven't really dived fully into
junit
yet) to conclude that the main advantage of JUnit is to provide a "simple"
test
case for people who are unable (for whatever reason) to create their own
specific testcase.
Going even deeper into that I'm also wondering if unit tests are really as
efficient as stated. What happens if the error is inside the unit itself?
Then
you know it has a bug, and then what? You can then start adding debug code
after the facts. Wouldn't it then have made more sense to add debug code
to the
project during the development stages?
What do you think?
Groetjes, Peter
--
..\\ PGP/GPG key: http://www.catslair.org/pubkey.asc


|