junit swe 619 summer 2007. july 18, 2007 swe 619 (c) aynur abdurazik 2 what is junit? open source...

20
JUnit SWE 619 Summer 2007

Upload: adelia-may

Post on 19-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

JUnit

SWE 619Summer 2007

Page 2: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 2

What is JUnit?

Open source Java testing framework used to write and run repeatable automated tests

JUnit features include: Assertions for testing expected results Test fixtures for sharing common test data Test suites for easily organizing and running

tests Graphical and textual test runners

Page 3: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 3

JUnit Architecture

Page 4: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 4

Test Scenarios with JUnit

Used to test A whole object Part of an object - a method or some

interacting methods Interaction between several objects

A test case represents one scenario A tester class contains more than one

scenario Each senario is written into one test method

Page 5: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 5

Writing Tests for JUnit

Need to use the methods of the junit.framework.assert class javadoc gives a complete description of its

capabilities Each of these methods checks some

condition and reports back to the test runner whether the test failed or succeeded.

The test runner uses the result to update the display. All of the methods return void (i.e. they are procedures)

Page 6: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 6

Writing Tests for JUnit – (cont’d) A few representative methods of

junit.framework.assert assertTrue(boolean) assertTrue(String, boolean) assertEquals(Object, Object) assertNull(Object) fail(String)

Page 7: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 7

Sample Assertions (cont’d) static void assertEquals(boolean expected, boolean actual)

          Asserts that two booleans are equal. static void assertEquals(byte expected, byte actual)

          Asserts that two bytes are equal. static void assertEquals(char expected, char actual)

          Asserts that two chars are equal. static void assertEquals(double expected, double actual,

double delta)           Asserts that two doubles are equal concerning a delta.

static void assertEquals(float expected, float actual, float delta)           Asserts that two floats are equal concerning a delta.

static void assertEquals(int expected, int actual)           Asserts that two ints are equal.

For a complete list, see http://junit.sourceforge.net/javadoc/junit/framework/Assert.html

Page 8: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 8

How to write test case

Two different ways for junit versions 3.X and 4.x.

In Junit 3.X 1) import junit.framework.*

2) extend TestCase. 3) name the test methods with a prefix of

‘test’ 4) validate conditions using one of the

several assert methods

Page 9: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 9

How to write test case (cont’d)

In Junit 4.0 and later: Do not extend from

Junit.framework.TestCase Do not prefix the test method with ‘test’ Use one of the assert methods Run the test using JUnit4TestAdapter

Page 10: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 10

Simple Example

public class Math {         static public int add(int a, int b) {                   return a + b;         } }

Page 11: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 11

Example JUnit test case

import junit.framework.*; public class TestMath extends TestCase {   public void testAdd() {

        int num1 = 3;         int num2 = 2;         int total = 5;         int sum = 0;         sum = Math.add(num1, num2);         assertEquals(sum, total);   } } 

Page 12: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 12

Testing the Stack class 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; }}

Page 13: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 13

TestCases with JUnit 4.X for Stack Necessary classes to be imported:

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

import junit.framework.JUnit4TestAdapter;

Page 14: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 14

TestCases with JUnit (Cont’d)

public class StackTest {

private Stack stack;

/**

* setUp method using @Before annotation.

* Method can be named anything.

*/

@Before public void runBeforeEachTest(){

stack = new Stack();

}

Page 15: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 15

Test case with JUnit (cont’d)

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

}

Page 16: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 16

Test case with JUnit (cont’d)@Test public void testToString(){ stack = stack.push(new Integer(1)); stack = stack.push(new Integer(2)); assertEquals("{2, 1}",

stack.toString());

}

@After public void runAfterEachTest(){ stack = null; } public static junit.framework.Test suite(){ return new

JUnit4TestAdapter(StackTest.class); }

}

Page 17: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 17

How to Run Tests

JUnit provides test drivers Character-based test driver runs from the

command line GUI-based test driver-junit.swingui.TestRunner allows to specify the test class to run, "Run" button examines the chosen class and finds all methods whose

names begin with "test."

If a test fails, Junit gives the location of the failure and any exceptions that were thrown

Page 18: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 18

JUnit Installation in Eclipse In Eclipse, select Help->Software Updates-

>Find and Install Choose 'Search For New Features to Install'

and select 'Next' Select 'New Remote Site' Enter a name for this server: JUnit Factory Enter (or copy/paste) this url:

http://www.junitfactory.com/update/ Install all plug-ins in the 'JUnit Factory'

category and restart Eclipse

Page 19: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 19

Generating Tests in Eclipse1. Select a Java class in Eclipse and

click the Generate Tests ( )button 2. When the JUnit Factory view says

your test is done, click the link in the Result column to open it

3. Select the test and choose Run->Run As->Agitar JUnit Test from the main menu

Page 20: JUnit SWE 619 Summer 2007. July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable

July 18, 2007 SWE 619 (c) Aynur Abdurazik 20

Eclipse JUnit Plugin

See tutorial http://open.ncsu.edu/se/tutorials/junit/ http://www.laliluna.de/eclipse-junit-testing-

tutorial.html