23
JUnit Tutorial ECE453/SE465/ECE653/CS447/CS647 presented and modified by: Jinqiu Yang email: [email protected] by: Mehdi Amoui 1 Friday, 24 January, 14

T1-JUnit

Embed Size (px)

DESCRIPTION

www.scribd.com/doc/30630255/PeopleSoft-Component-Interfaces

Citation preview

Page 1: T1-JUnit

JUnit Tutorial

ECE453/SE465/ECE653/CS447/CS647

presented and modified by: Jinqiu Yangemail: [email protected]

by: Mehdi Amoui

1Friday, 24 January, 14

Page 2: T1-JUnit

Introduction

Introduction to the JUnit framework.

Hopefully, after this tutorial you will have a basic understanding of JUnit to:– implement test classes in JUnit 4.– test exceptions using JUnit.

2Friday, 24 January, 14

Page 3: T1-JUnit

What is JUnit• Open source Java testing framework that helps in the

automation of unit tests for Java classes.

• Used to write and run repeatable automated test cases.

• JUnit can be used as stand alone Java programs (from the command line) or within common Java IDEs such as Eclipse and Netbeans

• By Kent Beck and Erich Gamma. • Check: junit.org• Current stable version: junit-4.11.jar

3Friday, 24 January, 14

Page 4: T1-JUnit

Steps to perform unit tests (Junit)

1. Prepare (or set up) environment conditions that must be met, according to the test plan.

2. Execute the test case.

3. Evaluate the results.

4. Clean up (or tear down) the test environment if needed so that further testing activities can be done.

4Friday, 24 January, 14

Page 5: T1-JUnit

Step 1: Unit Testing with JUnit 4

1. Prepare (or set up) the test environment:- Annotate with @Before: Those methods are executed before each test case (test method).

@Before

public void setUp() {

s = new Sample();

}

5Friday, 24 January, 14

Page 6: T1-JUnit

Step 2&3: Unit Testing with JUnit 4

2. Execute the test case. 3. Evaluate the results (or side effects).

This is done by exercising the code under test with the appropriate test values. Each method is:– Annotated with @Test– Evaluation of the expected results (or side effects) is done in the

same method using assertions

@Testpublic void testAddition( ) { int a=3 , b=6; int expectedOutput = (a+b); int res = s.Addition(a, b); assertEquals(expectedOutput, res);}

6Friday, 24 January, 14

Page 7: T1-JUnit

Step 4: Unit Testing with JUnit 44. Clean up (or tear down) the test environment is done

in one or several methods that are run after execution of each test method. – A method has to be annotated with @After.– If you allocate external resources in a @Before method,

you need to release them after the test runs. – Annotating a public void method with @After causes that

method to be run after the @Test method. – All @After methods are guaranteed to run even if a

@Before or @Test method throws an exception.

@Afterpublic void tearDown() { s = null;}

7Friday, 24 January, 14

Page 8: T1-JUnit

@BeforeClass and @AfterClass• @BeforeClass uses:

– Sometimes several tests need to share computationally expensive setup (like logging into a database).

– This can compromise the independence of tests! But sometimes it is a necessary optimization.

– @BeforeClass causes a method to be run once before any of the test methods in the class.

• @AfterClass uses:– A method with @AfterClass causes that method to be run after all the

tests in the class have been run.

– The @BeforeClass and @AfterClass methods of superclasses will be run before those the current class.

8Friday, 24 January, 14

Page 9: T1-JUnit

An Exampleimport org.junit.*;import static org.junit.Assert.*;

public class TestM {

private M m;

public TestM() { }

@Before public void setup() { m = new M(); }

@After public void tearDown() { m = null; }

@Test public void testDummyMethod() { int expected = 0; int res = m.dummyMethod(); assertTrue( (expected == res) ); }}

9Friday, 24 January, 14

Page 10: T1-JUnit

Asserting Desired Results

• An assert is simply a comparison between an expected value and an actual value.

• There are variants of the assert methods for each primitive datatype and for java.lang.String and java.lang.Object, each with the following signatures:– assertEquals(expected, actual)

– assertEquals(String message, expected, actual)

10Friday, 24 January, 14

Page 11: T1-JUnit

JUnit Assertion API• assertEquals(expected, actual)

– This assertion states that the test expected.equals(actual) returns true, or both objects are null. The equality test for a double also lets you specify a range, to cope with floating point errors better. There are overloaded versions of this method for all Java’s primitive types.

• assertNull(Object object),– This asserts that an object reference equals null.

• assertNotNull(Object object),– This asserts that an object reference is not null.

• assertSame(Object expected, Object actual),– Asserts that the two objects are the same. This is a stricter condition than simple

equality, as it compares the object identities using expected == actual.

• assertTrue(boolean condition),– This assertion fails if the condition is false, printing a message string if supplied.

11Friday, 24 January, 14

Page 12: T1-JUnit

Assertion Examplepublic class Calc{ static public long add (int a, int b) { return a + b; }}

import org.junit.Testimport static org.junit.Assert.*;

public class CalcTest{ @Test public void testAdd() { // Calc().add() returns long, // so we must cast 5 assertEquals((long) 5, new Calc().add(2,3)); }}

12Friday, 24 January, 14

Page 13: T1-JUnit

Testing Exceptions in JUnit 4

• We can use @Test with the attribute expected:

@Test (expected=ArithmeticException.class)

public void testDivideByZero( ) {

s.divideByZero( );

}

13Friday, 24 January, 14

Page 14: T1-JUnit

Manually Checking Exceptions• You can also use fail() to manually trigger failure of a test.

@Testpublic void testAnotherException ( ) { try{ s.anotherException(0); // With the method fail, we can notify // the framework that the test is failed. fail("MyException was not thrown!”); } catch (MyException e ) { //Here we have access to more info assertNotNull(e.getMessage()); }}

14Friday, 24 January, 14

Page 15: T1-JUnit

Assume API

• A set of methods useful for stating assumptions about the conditions in which a test is meaningful.

• A failed assumption does not mean the code is broken, but that the test provides no useful information.

• The default JUnit runner treats tests with failing assumptions as ignored.

// only provides information if database is reachable. @Test public void calculateTotalSalary() { DBConnection dbc = Database.connect(); assumeNotNull(dbc); // ...}

15Friday, 24 January, 14

Page 16: T1-JUnit

How to Run Test Cases• TestRunner classes provided by JUnit are used to execute all tests

prefixed by the word “test.”

• or you can write a self-executable test class:

java org.junit.runner.JUnitCore org.example.antbook.junit.SimpleTest

public static void main(String args[]) { org.junit.runner.JUnitCore.main(LoadTester.class.getName());}

16Friday, 24 January, 14

Page 17: T1-JUnit

Writing a TestSuite

• Tests can be grouped into a suite by using the TestSuite class.

• Test suites let you build several individual test cases for a particular subsystem and write an all-inclusive TestSuite that runs them all.

• It allows specific ordering of tests (ideally the order of tests should not be relevant as each should be able to stand alone).

17Friday, 24 January, 14

Page 18: T1-JUnit

Stack Example (JUnit 4.x)public class Stack { public String toString() { // EFFECTS: Returns the String representation // of this Stack from the top to the bottom. StringBuffer buf = new StringBuffer ("{"); for (int i = size-1; i >= 0; i--) { if (i < (size-1)) buf.append (", "); buf.append (elements[ i ].toString()); } buf.append ("}"); return buf.toString(); }

public boolean repOk() { if (elements == null) return false; if (size != elements.length) return false; for (int i = 0; i < size; i++) { if (elements[i] == null) return false; } return true; }}

18Friday, 24 January, 14

Page 19: T1-JUnit

Test Class Setup• Classes to Import

• Pre Operations:

• Post Operations:

import org.junit.After;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.assertEquals;import junit.framework.JUnit4TestAdapter;

private Stack stack;// setUp method using @Before syntax// @Before methods are run before each test@Before public void runBeforeEachTest(){ stack = new Stack();}

// tear-down method using @After// @After methods are run after each test@After public void runAfterEachTest(){ stack = null;}

19Friday, 24 January, 14

Page 20: T1-JUnit

Test Cases@Test public void testToString(){ stack = stack.push (new Integer (1)); stack = stack.push (new Integer (2)); assertEquals ("{2, 1}", stack.toString());}

@Test public void testRepOk(){ boolean result = stack.repOk(); assertEquals (true, result); stack = stack.push (new Integer (1)); result = stack.repOk(); assertEquals (true, result); stack = stack.pop(); result = stack.repOk(); assertEquals (true, result); stack = stack.push (new Integer (1)); stack.top(); result = stack.repOk(); assertEquals (true, result);}

20Friday, 24 January, 14

Page 21: T1-JUnit

Test Cases (Splited) @Test public void testRepOkA(){ boolean result = stack.repOk(); assertEquals (true, result);}

@Test public void testRepOkB(){ stack = stack.push (new Integer (1)); result = stack.repOk(); assertEquals (true, result);}

@Test public void testRepOkC(){ stack = stack.pop(); result = stack.repOk(); assertEquals (true, result);}

@Test public void testRepOkD(){ stack = stack.push (new Integer (1)); stack.top(); result = stack.repOk(); assertEquals (true, result);}

21Friday, 24 January, 14

Page 22: T1-JUnit

Test Suite import org.junit.runner.RunWith;import org.junit.runners.Suite;import junit.framework.JUnit4TestAdapter;

// This section declares all of the test classes in the program.@RunWith (Suite.class)@Suite.SuiteClasses ({ StackTest.class }) // Add test classes here.

public class AllTests{ // Execution begins at main(). In this test class, we will execute // a text test runner that will tell you if any of your tests fail. public static void main (String[] args) { junit.textui.TestRunner.run(suite()); }

// The suite() method is helpful when using JUnit 3 Test Runners or Ant. public static junit.framework.Test suite() { return new JUnit4TestAdapter(AllTests.class); }} 22Friday, 24 January, 14

Page 23: T1-JUnit

JUnit Resources & References• JUnit 4.x Howto, by Blaine Simpson • Testing with JUnit, by Erik Hatcher and Steve

Loughran • JUnit 4.0 - A Quick Start Guide , by J. Uwe Pipka• JUnit: A Starter Guide, by Diaspar software• JUnit Primer, by Clarkware consulting• JUnit Automated Software Testing Framework,

by Paul Ammann & Jeff Offutt• http://netbeans.org/kb/docs/java/junit-

intro.html#Exercise_11

23Friday, 24 January, 14