19
CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 1 Software Testing 2 Unit Testing with  JUnit 4 Cem Kaner Nawwar Kabbani August 2009

Junit Final

Embed Size (px)

Citation preview

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 1/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 1

Software Testing 2

Unit Testing

with

 JUnit 4

Cem Kaner

Nawwar Kabbani

August 2009

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 2/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 2

 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”.

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 3/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 3

First example

import org.junit.Test; // for @Test

import static org.junit.Assert.*; // assertEquals()

public class ArithmeticTest {

@Testpublic void addition() {

assertEquals(2, 1 + 1);

}

}

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 4/19CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 4

Compare to JUnit 3

import junit.framework.TestCase;

 public class ArithmeticTest extends TestCase {

 public void testAddition() {

assertEquals(2, 1 + 1); // inherited from TestCase}

}

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 5/19CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 5

Notes  JUnit 4 automatically recognizes test methods preceded by

@Test annotation.

There is no need for main() to run the tests (see next slides forrunning 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 theproduction class, although it's technically possible to combinethem in the same class in JUnit 4. Combining them *might* beuseful to test private methods and variables and is useful fortesting protected methods and variables.

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 6/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 6

 JUnit tests output

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 7/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 7

 JUnit tests output

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 8/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 8

 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)

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 9/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 9

How do I run JUnit tests?

Command Line JUnit 3

java junit.textui.TestRunner TestClass

 JUnit 4 java org.junit.runner.JUnitCore TestClass

Remember to make sure junit.jar is in the current path or inthe CLASSPATH environment variable. Or you can manuallyinclude its path in the java command:

Unix:

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

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

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 10/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 10

How do I run JUnit tests

Eclipse

1. Add JUnit Library to the project if it is not already there:

 –  Right-click on the project → Build Path → Add

Libraries...→

 JUnit2. 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, ...);

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 11/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 11

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 optionalcustom error message as a first parameter

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 12/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 12

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

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 13/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 13

The “flexible” assertThat()

New assertion mechanism introduced by JMock.

More readable expressions, and more friendly/informative failuremessages.

Can be extended to create your own custom matchers. Ability to use boolean operators (and, or, not) to create complex

test expressions.

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 14/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 14

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

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 15/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 15

Other JUnit Annotations

@Before, @After: Initialization/Finalization code that has to berun 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

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 16/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 16

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

}

}

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 17/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 17

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

}

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 18/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 18

Testing Exceptions

@Test(expected = ExceptionClass.class)

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

 JUnit 3 way:

@Test(expected = ArithmeticException.class)

 public void divide() {int x = 10 / 0;

}

 public void testException() {

try {

int x = 10 / 0;

fail(“Division by zero exception was expected!”);

}

catch (ArithmeticException e) { /* Pass! */ }

}

7/30/2019 Junit Final

http://slidepdf.com/reader/full/junit-final 19/19

CSE 4415 Day 2 Copyright © 2009 Nawwar Kabbani 19

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 recipelong!)

•http://pub.admc.com/howtos/junit4x/junit4x.html

•http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/junit/junit.html (JUnit 3 tutorial)