CIT 591 Unit Testing in Java
Fall 2009, David Matuszek
JUnit is the original testing framework, since copied by every other major programming language.
In a sense, the purpose of JUnit testing is to reduce the number of bugs in the final code. Even more importantly, however, extensive use of JUnit, particularly in combination with Test Driven Development, results in substantially better code quality.
There are two versions of JUnit currently available: JUnit 3 and JUnit 4. Python's unittest is slightly out of date, as it is based on JUnit 3. The most important difference is that JUnit 4 uses annotations (words beginning with @).
| JUnit 3 | JUnit 4 |
|---|---|
import junit.framework.TestCase; |
|
Your class must extend TestCase. |
Your class can extend any other class you like (Object is the default). |
You can override the methodprotected void setUp() throws Exceptionand it will be executed before every test. |
You can annotate a method with @Before, like this:@Beforeand it will be executed before every test. The method can have any name (but it's commonly still named setUp). |
You can create as many test methods as you like, but:
|
You can create as many test methods as you like, but:
|
| You can test whether a method call throws an exception when it is supposed to, but it's complicated. | You can specify that a test method should get an exception with the annotation@Test(expected = ArithmeticException.class)(or whatever kind of Exception you are expecting). |
| If a method goes into an infinite loop, the test framework can't get past it. | You can set a limit on how many milliseconds a test method is allowed to take with the annotation@Test(timeout = 1000) |
Occasionally you need to do something after each test method. You can do this by overriding the methodprotected void tearDown() throws Exception |
To execute a JUnit method after each test method, annotate it with@After. |
There about 40 tests that you can use in your test methods; see http://junit.sourceforge.net/javadoc_40/index.html. Here are the ones that I have found to be the most useful.
assertEquals(expected, actual) actual value equals the expected value. Both expected and actual must be the same type (for example, you cannot test an int against a double). If the two parameters are objects of a class that you have created, the test will use your public boolean equals(Object o) method (or == if you haven't defined equals, which is almost certainly not what you want!). assertArrayEquals(expectedArray, actualArray)assertEquals, but for arrays. If the elements of the arrays are objects of a class that you have created, the test will use the equals method for that class. assertTrue(booleanCondition)booleanCondition evaluates to true. assertFalse(booleanCondition)booleanCondition evaluates to false. assertNull(object)object is null.assertNotNull(object)object isn't null.assertSame(expected, actual)actual argument is another reference to the expected object. assertNotSame(unexpected, actual)actual argument is not another reference to the unexpected object.fail(message)| Fraction.java | FractionTest.java |
|---|---|
The add method above is a stub--it's enough to start creatingthe test, but it's certainly returning the wrong answer. The method should compute and return a Fraction. |
|