You are on page 1of 19

Software Testing 2

Unit Testing with JUnit 4


Cem Kaner Nawwar Kabbani August 2009
CSE 4415 Day 2 Copyright 2009 Nawwar Kabbani 1

JUnit

Unit testing framework for Java.

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.

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

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); }

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

Compare to JUnit 3
import junit.framework.TestCase; public class ArithmeticTest extends TestCase {

public void testAddition() { assertEquals(2, 1 + 1); // inherited from TestCase }


}

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

Notes

JUnit 4 automatically recognizes test methods preceded by @Test annotation.

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.

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

JUnit tests output

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

JUnit tests output

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

JUnit tests output


Runs

Total number of tests run Failures Tests that failed. For example assertions that failed. Errors Tests that generated unhandled (unexpected) exceptions. Time elapsed (ms)

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

How do I run JUnit tests?

Command Line JUnit 3

java junit.textui.TestRunner TestClass java org.junit.runner.JUnitCore TestClass

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:

java -cp .:/path/to/junit.jar org.junit.runner.JUnitCore TestClass

java -cp .;c:\path\to\junit.jar org.junit.runner.JUnitCore TestClass

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

How do I run JUnit tests

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.

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

10

org.junit.Assert.* static methods

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

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

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));

assertEquals(2.0, Math.sqrt(4.0), 1e-10);

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

12

The flexible assertThat()


New assertion mechanism introduced by JMock.

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.

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

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));

x = hello world; assertThat(x, anyOf(containsString(hello"), containsString(hi"))); assertThat(x, either(containsString("hello")) .or(containsString("hi")));

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

14

Other JUnit Annotations

@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

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

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()); } }

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

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(); }

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

17

Testing Exceptions

@Test(expected = ExceptionClass.class)

The test passes if the expected exception is thrown, and fails otherwise.

@Test(expected = ArithmeticException.class) public void divide() { int x = 10 / 0; }

JUnit 3 way:

public void testException() { try { int x = 10 / 0; fail(Division by zero exception was expected!); } catch (ArithmeticException e) { /* Pass! */ } }

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

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)

CSE 4415 Day 2

Copyright 2009 Nawwar Kabbani

19

You might also like