this is an assignement question from software engineering course and its a text book question..need help with the assignement as soon as possible!! please!and the textbook is fundamentals of software engineering second addition by carlo ghezzi and mehdi

profileJenifer-bore_45
JUnit.pdf

JUnit

CSI 5390

Department of Computer Science and Engineering

Oakland University

Dae-Kyoo Kim

JUnit: Overview

• A Java framework for writing and running unit tests – Test fixtures – Test cases – Test suites – Test runner

• By Kent Beck and Erich Gamma • Written with “test first” and pattern-based

development in mind – Tests written before code – Allows for regression testing – Facilitates refactoring

• JUnit is Open Source – www.junit.org – JUnit Version 4, released Mar 2006

Unit

Testing

Acceptance

Testing

Integration

Testing

System

Testing

JUnit Classes

*

Test

run(TestResult)

ConcreteTestCase

setUp()

tearDown()

runTest()

TestResult

TestCase

run(TestResult)

setUp()

tearDown()

testName:String

runTest()

TestSuite

run(TestResult)

addTest()

UnitToBeTested

Methods under Test

Design patterns in JUnit

Test

run(TestResult)

ConcreteTestCase

setUp()

tearDown()

runTest()

TestResult

TestCase

run(TestResult)

setUp()

tearDown()

testName:String

runTest()

TestSuite

run(TestResult)

addTest()

Command Pattern

TestedUnit

*

Design patterns in JUnit

Test

run(TestResult)

ConcreteTestCase

setUp()

tearDown()

runTest()

TestResult

TestCase

run(TestResult)

setUp()

tearDown()

testName:String

runTest()

TestSuite

run(TestResult)

addTest()

Composite

Pattern

TestedUnit

*

Design patterns in JUnit

Test

run(TestResult)

ConcreteTestCase

setUp()

tearDown()

runTest()

TestResult

TestCase

run(TestResult)

setUp()

tearDown()

testName:String

runTest()

TestSuite

run(TestResult)

addTest()

Template Method

Pattern

TestedUnit

*

Design patterns in JUnit

Test

run(TestResult)

ConcreteTestCase

setUp()

tearDown()

runTest()

TestResult

TestCase

run(TestResult)

setUp()

tearDown()

testName:String

runTest()

TestSuite

run(TestResult)

addTest()

Adapter

Pattern

TestedUnit

*

Example: Testing MyList

• Unit to be tested

– MyList

• Methods under test

– add()

– remove()

– contains()

– size()

• Concrete Test case

– MyListTestCase

Test

run(TestResult)

MyListTestCase

setUp()

tearDown()

runTest()

testAdd()

testRemove()

TestResult

TestCase

run(TestResult)

setUp()

tearDown()

testName:String

runTest()

TestSuite

run(TestResult)

addTest()

MyList

add()

remove()

contains()

size()

*

Writing TestCases in JUnit

public class MyListTestCase extends TestCase {

public MyListTestCase(String name) {

super(name);

}

public void testAdd() {

// Set up the test

List aList = new MyList();

String anElement = “a string”;

// Perform the test

aList.add(anElement);

// Check if test succeeded

assertTrue(aList.size() == 1);

assertTrue(aList.contains(anElement));

}

protected void runTest() {

testAdd();

}

}

Writing Fixtures and Test Cases public class MyListTestCase extends TestCase {

// …

private MyList aList;

private String anElement;

public void setUp() {

aList = new MyList();

anElement = “a string”;

}

public void testAdd() {

aList.add(anElement);

assertTrue(aList.size() == 1);

assertTrue(aList.contains(anElement));

}

public void testRemove() {

aList.add(anElement);

aList.remove(anElement);

assertTrue(aList.size() == 0);

assertFalse(aList.contains(anElement));

}

Test Fixture

Test Case

Test Case

Collecting TestCases into

TestSuites

public static Test suite() {

TestSuite suite = new TestSuite();

suite.addTest(new MyListTest(“testAdd”));

suite.addTest(new MyListTest(“testRemove”));

return suite;

}

*

jUnit 4 vs jUnit 3

• jUnit 4 is completely based on annotations

– No longer necessary to extend class TestCase

– Test-method names do not have to start with the prefix “test”.

– Instead they are annotated with the @Test annotation

13

jUnit 4 vs jUnit 3

jUnit 3

import

junit.framework.TestCase;

public class CalculatorTest

extends TestCase {

public void testadd() {

....

}

}

jUnit 4

import org.junit.Test;

public class CalculatorTest {

@Test public void add() {

....

}

}

Testing for Exceptions

jUnit 3

public class CalculatorTest { public void testDivideByZero() { try { int n = 2/0; fail(“Division by zero!”); } catch (ArithmeticException

success) { assertNotNull(Success.getMe ssage())}

} }

jUnit 4

public class CalculatorTest {

@Test(expected=ArithmeticEx ception.class)

public void DivideByZero() { int n = 2/0;

} }

Example

class Money {

private int fAmount;

private String fCurrency;

public Money(int amount, String currency) {

fAmount= amount;

fCurrency= currency;

}

public int amount() {

return fAmount;

}

public String currency() {

return fCurrency;

}

public Money add(Money m) {

return new Money(amount()+m.amount(), currency());

}

}

Unit Testing add()

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

public class MoneyTest { @Test public void add() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money observed= m12CHF.add(m14CHF); assertTrue(expected.equals(observed));

} } Assertion: Returns True if parameter of

type Boolean evaluates to True

Annotations in jUnit 4.0

• @Test public void foo()

– Annotation @Test identifies that foo() is a test method

• @Test(expected=IllegalArgumentException.class)

– Tests if the test method throws the named exception

@Before and @After:

Ensuring Pre- and Post Conditions

• @Before public void bar() – Perform bar() before executing a test method

• @After public void foobar() – A test method must finish with call to foobar()

public class CalculatorTest {

@Test public void add()

@Test public void sub()

@Before public void setupTestData(){ } //executed before every add/sub

@After public void teardownTestData() {} //executed after every add/sub

}

• @BeforeClass public void foofoo() – Perform foofoo() before the start of all tests

– e.g., database connection

• @AfterClass public void blabla() – Perform blabla() after all tests have finished.

– e.g., database disconnection

public class CalculatorTest {

@BeforeClass // executed at instantiation of class

public static void setupDatabase Connection() { ... }

@AfterClass // executed after remove instance of class

public static void teardownDatabase Connection() { ... }

}

@BeforeClass and @AfterClass:

Ensuring Pre- and Post Conditions

@Test(timeout): Shortening

Tests

• Some tests take time (e.g., network connectivity)

– Set an upper bound for the test to expire

@Test(timeout=5000)

public void testLengthyOperation() {

...

}

Fails if taking longer

then 100 milliseconds

@Ignore: Omitting Tests

• @Ignore(string S) – Ignore the test method, print out the string S

– Useful when the code has been changed, but the test has not yet been adapted

– Useful when the current release of a third-party library used in the SUT has a bug

public class CalculatorTest {

@Ignore(“Don‘t run now this test until bug in Foo is fixed”)

@Test public void testSpecialFunctionality() {

...

}

}

Assertions in jUnit 4.0

• assertTrue(predicate); – Returns True if predicate evaluates to True

• assertTrue([message], boolean condition) – Check if the boolean condition is True

• fail(String) – Let the method fail. Useful to check that a certain part of the code is not reached.

• assertsEquals([message], expected, actual) – Returns message if the values are the same

• assertsEquals([message], expected, actual, tolerance) – Used for float and double; tolerance specifies the number of decimals which must

be the same

Assertions in jUnit 4.0

• assertNull([message], object) – Checks if the object is null and prints message if it is

• assertNotNull([message], object) – Check if the object is not null

• assertSame([message], expected, actual) – Check if both variables refer to the same object

• assertNotSame([message], expected, actual) – Check that both variables refer not to the same object

Example

import org.junit.Test;

import static org.junit.Assert.*;

public class CalculatorTest {

private int x = 1;

private int y = 1;

@Test public void add() {

int z = x + y;

assertEquals(2, z);

}

}