Professional Documents
Culture Documents
JUnit
Based on a collection of Unit Testing frameworks called xUnit. Adapted from SUnit (proposed by Kent Beck for Smalltalk) Ported to other languages: C# (NUnit), Python (PyUnit), Javascript (JSUnit), Perl (Test::Class & Test::Unit) Currently version 4.x, however JUnit 3.8 is still widely used. JUnit 4 requires Java 1.5 at least, because it depends on Java Annotations.
First example
import org.junit.Test; // for @Test import static org.junit.Assert.*; // assertEquals() public class ArithmeticTest { @Test public void addition() { assertEquals(2, 1 + 1); }
Compare to JUnit 3
import junit.framework.TestCase; public class ArithmeticTest extends TestCase {
Notes
There is no need for main() to run the tests (see next slides for running JUnit tests). Test methods must be public, void, with no parameters. Test Method does not have to prefixed with 'test' (as in JUnit 3). It's strongly recommended to separate the test class from the production class, although it's technically possible to combine them in the same class in JUnit 4. Combining them *might* be useful to test private methods and variables and is useful for testing protected methods and variables.
Total number of tests run Failures Tests that failed. For example assertions that failed. Errors Tests that generated unhandled (unexpected) exceptions. Time elapsed (ms)
JUnit 4
Remember to make sure junit.jar is in the current path or in the CLASSPATH environment variable. Or you can manually include its path in the java command:
Unix: Windows:
Eclipse
Add JUnit Library to the project if it is not already there: Right-click on the project Build Path Add Libraries... JUnit 2. Run the unit tests Right-click Run As JUnit Test Java code (invoke the tests from your program) org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);
1.
10
assertEquals (expected, actual) Works with object, int, long, byte, string, etc. Object: it invokes object.equals(object) to check for equality. assertEquals (expected, actual, psilon) // for float and double. assertTrue / assertFalse (bool) assertNull / assertNotNull (object) assertSame / assertNotSame (object, object) assertArrayEquals (object[], object[]) assertThat (object, [JMock epression])
For all assertXXX except assertThat(), you can specify an optional custom error message as a first parameter
11
Examples
assertEquals(x must be 5, but it's not!, 5, x);
String x = Hello, y = world; assertNotSame(Hello world, x + y); Eq. to: assertFalse(Hello world == (x+y));
assertEquals(Hello world, x + y); Eq. to: assertTrue(Hello world.equals(x + y));
12
More readable expressions, and more friendly/informative failure messages. Can be extended to create your own custom matchers. Ability to use boolean operators (and, or, not) to create complex test expressions.
13
AssertThat examples
import static org.hamcrest.CoreMatchers.*;
assertThat(x, is(3)); assertThat(x, is(not(4))); assertThat(myList, hasItem("hello")); assertThat(Hello, is(String.class)); y = x = new object(); assertThat(x, sameInstance(y)); assertThat(null, nullValue()); assertThat(d, allOf(notNullValue(), instanceOf(Date.class));
14
@Before, @After: Initialization/Finalization code that has to be run before/after each test. (cf. setUp(), tearDown() methods in JUnit 3).
@BeforeClass, @AfterClass: Code to be run before/after all of the tests. Methods with these annotations must be static. @Ignore: skip the following test. @Test(timeout = [x]): fail if the test took more than [x] milliseconds
15
Examples
class ListTest { private ArrayList<String> myList;
@Before public void setUp() { myList = new ArrayList<String>(); } @Test public void addElement() { myList.add(hello); assertEquals(1, myList.size()); assertEquals(hello, myList.get(0)); } @Test public void emptyList() { AssertEquals(0, myList.size()); } }
16
Examples
@Ingore(Not ready); @Test public void newTest() { fail(This message won't show up); }
@Test(timeout = 1000) // fail if passed 1000ms public void tcpConnect() { tcpSocket.open(); }
17
Testing Exceptions
@Test(expected = ExceptionClass.class)
The test passes if the expected exception is thrown, and fails otherwise.
JUnit 3 way:
public void testException() { try { int x = 10 / 0; fail(Division by zero exception was expected!); } catch (ArithmeticException e) { /* Pass! */ } }
18
More resources
Official site: www.junit.org
JUnit Javadoc: http://junit.org/junit/javadoc/4.5/ JUnit cook book http://junit.sourceforge.net/doc/cookbook/cookbook.htm (one recipe long!) http://pub.admc.com/howtos/junit4x/junit4x.html http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/buil d/tutorials/junit/junit.html (JUnit 3 tutorial)
19