Transcript Using JUnit

Using JUnit
How we can use JUnit for testing in
Bandera
By Todd Wallentine ([email protected])
Overview
What is JUnit?
Example Usage
Talking Points
References
What is JUnit?
JUnit is a regression testing framework
built by Erich Gamma and Kent Beck[1].
It can be used for regression testing as
well as for simple Unit testing.
It will not work for regression testing of
the GUI components.
Works very well to test methods!
Example
We need to test a class called Math that performs simple
operations (add, subtract, multiply, and divide). It has four
method calls:
•add(int a, int b) : int
•subtract(int a, int b) : int
•multiply(int a, int b) : int
•divide(int a, int b) : int
To exercise this class, we will create a JUnit TestCase that
will perform simple tests.
•The addition of 1 and 1 will be 2.
•The subtraction of 3 from 5 will be 2.
•The multiplication of 2 and 2 will be 4.
•The division of 10 by 2 will be 5.
Example (continued)
To create a JUnit test, we will extend the class TestCase.
public class MathTestCase extends TestCase {
We will then create tests. Example for add(a, b):
public void testAdd1() {
int result = Math.add(1, 1);
assertTrue(“1 + 1 should be 2.”, result == 2);
}
We will then instrument the TestCase so that all tests will
be run.
public static Test suite() {
return(new TestSuite(MathTestCase.class));
}
Example – Text Output
..F..F..F..F
Time: 0.04
There were 4 failures:
1) testAdd2(math.test.MathTestCase)junit.framework.AssertionFailedError:
1 + 1 should equal 3. result = 2
at java.lang.Throwable.<init>(Compiled Code)
at java.lang.Error.<init>(Compiled Code)
at junit.framework.AssertionFailedError.<init>(Compiled Code)
at math.test.MathTestCase.testAdd2(Compiled Code)
2) testSubtract2(math.test.MathTestCase)junit.framework.AssertionFailedError:
5 - 3 should equal 3. result = 2
at java.lang.Throwable.<init>(Compiled Code)
at java.lang.Error.<init>(Compiled Code)
at junit.framework.AssertionFailedError.<init>(Compiled Code)
at math.test.MathTestCase.testSubtract2(Compiled Code)
… [divide and multiplication errors here] …
FAILURES!!!
Tests run: 8, Failures: 4, Errors: 0
Example – GUI Output
Example – MathTestCase code
public void testAdd1() {
int result = Math.add(1, 1);
assertTrue("1 + 1 should equal 2. result = " + result, result == 2);
}
public void testAdd2() {
int result = Math.add(1, 1);
assertTrue("1 + 1 should equal 3. result = " + result, result == 3);
}
public void testSubtract1() {
int result = Math.subtract(5, 3);
assertTrue("5 - 3 should equal 2. result = " + result, result == 2);
}
public void testSubtract2() {
int result = Math.subtract(5, 3);
assertTrue("5 - 3 should equal 3. result = " + result, result == 3);
}
Talking Points
A good practice is to have a TestCase class for
each class in a package.
A good practice is to have all TestCase classes
in a subpackage, test, for each package.
A good practice is to have an all encompassing
TestCase for the whole package to make
regression testing easier.
JUnit will integrate into IDEs like VisualAge for
Java.
A good practice is to add regression testing to
the CVS submisison process so that no code gets
submitted on accident that would break the
regression tests.
References
1. http://junit.org
2. http://junit.sourceforge.net/doc/testinfec
ted/testing.htm
3. http://www.junit.org/junit/doc/vaj/vaj.ht
m