|
|
This article, will
explore what is JUnit and
how to write simple Unit
test cases using JUnit. TestingGeek will also touch
upon history of JUnit and
will find out how it came
into existence. JUnit
is a unit testing framework
for the Java programming
language. This framework was
developed by Kent Beck and
Erich Gamma. JUnit is one of
the most successful and
widely used xUnit automation
framework. Usage of JUnit
have increased further with
the progress of Xtreme
programming and Test Driven
Development in particular.
|
Automated unit test cases
makes your life very easy
since you will spend less
time in doing debugging and
more on writing test and
eventually code that will pass those
tests. Presence of automated
unit testing will make you
more confident that your
tests are not breaking
anything and if something is
broken, unit test cases will
catch them. Writing test
cases in JUnit is not
difficult, in fact it is very
simple and straight forward
to write unit test cases. As
mentioned earlier, it is
faster to develop code using
JUnit since it increases
your effectiveness. As a
tester, you might not be
involved directly in writing
JUnit test cases, but you
can and should assist
developers in writing good
Unit test cases and making
sure that coverage from unit
test cases is proper.
As mentioned earlier,
It is very simple to write
JUnit test cases. Any JUnit class
that contain test methods
should extend TestCase
class. This class now can
define any number of testXXX()
methods. Validating any
functionality of system is
implemented with the help of
variation of assert methods.
This TestCase subclass can
also implement setup() and
tearDown() functions to
initialize and release
specific objects that you
might need for testing this
subclass. This can also be
used to ensure that there is
no side effect of running
test cases. TestCase
instances can be composed of
TestSuite and instance of
TestSuite can invoke all
testXXX methods defined in
the TestCase.JUnit also comes with two
type of Test Runners i.e.
textual test runners and
graphical test runners.
JUnit gives an indication of
successful execution in the
form of either OK message in
textual runner and green bar
in graphical runner. As
explained earlier, using
JUnit is not a rocket
science, it is very simple.
In order to get started with
JUnit, you can follow these
steps.
- Download the JUnit.
Mostly it will be in the
form of JUnit.zip
- To install JUnit,
you just need to unzip
it in any folder and add
that folder in your
classpath.
- If you want to test
your installation, you
can do so by running the
sample tests in the
textual or graphical
test runners (java
junit.textui.TestRunner
or
junit.swinggui.TestRunner).
- Now define a
Subclass of TestCase.
- Override setUP()
method to initialize the
test environment.
- Override tearDown()
method and make sure
that resources used in
the test are released
properly.
- Define one or more
testXXX() methods
that exercise the
objects under test and
asserts expected
results.
- You can create a
test suite to include
all the test cases in
one class. To do that
create a java class and
define a static
suite() method.
- Now add test cases
to this suite by calling
addTest and
addTestSuite
methods.
- After you are done
with writing all the
tests, you can execute
them individually, all
the cases present in a
test class or test suite
with the help of textual
or graphical test
runners.
- You are done :).
Hope this article gave
you good understanding of
JUnit. This is such a simple
and elegant tool and
extremely useful for whole
team, testers and developers
alike. This was a very brief
introduction of JUnit, since
idea was to make you
familiar with the concept
and tool.
|