23
JUnit Introduction and Advanced Features

JUnit Introduction and Advanced Features. Topics Covered Junit Introduction Fixtures Test Suites Currency Example

  • View
    243

  • Download
    1

Embed Size (px)

Citation preview

Page 1: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

JUnit

Introduction and Advanced Features

Page 2: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Topics Covered Junit Introduction Fixtures Test Suites Currency Example

Page 3: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

JUnit Introduction JUnit is a Unit Testing Tool JUnit is a regression testing framework Programmed by Erich Gamma and Kent Beck Open Source

– Hosted on SourceForge– http://sourceforge.net/index.php – http://junit.sourceforge.net/ – http://www.junit.org/

Used by Java programmer

Page 4: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

JUnit Framework

http://junit.sourceforge.net/javadoc/index.html

run()

<< interface >>Test

fName

setUp()runTest()tearDown()run()

TestCase

run()addTest()

TestSuite

*

TestResult

junit.framework

assertTrue()assertEquals()...

Assert

junit.textui.TestRunner junit.swingui.TestRunner

Page 5: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

junit

How to Test with JUnit?

Student

TestCase

exercise1..*

TestRunner

run1..*TestStudent

test1

test2

Page 6: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Currency Exampleclass Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; }

Page 7: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Currency (cont)public int getAmount() {

return fAmount; } public String getCurrency() {

return fCurrency; }public Money add(Money m) { return new Money(amount()+m.amount(), curren

cy()); } }// Class Money Ends

Page 8: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Test Expression without JUnit

public class MoneyTest {public static void main(String[] args){

Money m10USD=new Money(10,”USD”);Money m20USD=new Money(20,”USD”); Money result = m10USD.add(m20USD);if (!(result.amount() == 30 &&

result.currency() == “USD”))System.out.println(“add failed!”);

} }// Class Money Test ends

Page 9: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Using Assert method

import junit.framework.*;

public class MoneyTest extends TestCase{public void testAdd { Money m10USD=new Money(10,”USD”);

Money m20USD=new Money(20,”USD”);

Money result = m10USD.add(m20USD);

Assert.assertEquals(result,(new Money(30, “USD"));

}

}

Page 10: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

More Assert Methods static void assertTrue(boolean test)

static void assertTrue(String message, boolean test)

Asserts that the value is true assertNull(Object object)

assertNull(String message, Object object) Asserts that the object is null

assertEquals(expected, actual)assertEquals(String message, expected, actual)– This method is heavily overloaded: expected and

actual must be both objects or both of the same primitive type

http://junit.sourceforge.net/javadoc/junit/framework/Assert.html

Page 11: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Over-riding Equals in our example

public boolean equals(Object anObject) { if (anObject instanceof Money) {

Money aMoney= (Money)anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false;

}

Page 12: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Testing equals (TestCase)import junit.framework.*;public class TestMoney extends TestCase {

public TestMoney (String name) {super (name);

}public void testEquals() {

Money m10USD=new Money(10,”USD”); Money m20USD=new Money(20,”USD”); Money result = m10USD.add(m20USD);

assertTrue(result. equals(new Money(30, “USD"));

}//other test methods of the form testGet, testSet, etc}

Page 13: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Test Fixture What if you have two or more tests

that operate on the same or similar sets of objects?

Tests need to run against background of a known set of objects.

This set of objects is called a test fixture.

Page 14: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Test Fixture (Cont.) Steps of using Test Fixture

– Create a subclass of TestCase– Create a constructor which accepts a string as a

parameter and passes it to the superclass. – Add an instance variable for each part of fixture– Override setUp() to initialize the variables– Override tearDown() to release any permanent r

esources you allocated in setUp.

Page 15: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

junit

Foo

TestCase

exercise1..*

TestRunner

run1..*FooTest

test1

test2

setUp()tearDown()

Page 16: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Test Fixture Examplepublic class MoneyTest extends TestCase {     private Money m10USD;     private Money m20USD;   

public MoneyTest (String s) { super(s); } protected void setUp() {        

m10USD=new Money(10,”USD”);

m20USD=new Money(20,”USD”);

}

protected void teardDown() throws Exception {

}

// other testX methods here

}

Page 17: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Test Suite A TestSuite is a collection of test cases.

Two ways of invoking the tests

The simple way is

public static Test suite() {

return new TestSuite(TestStudent.class);

}

Page 18: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Test Suite (Cont.) public static Test suite() { TestSuite suite= new TestSuite();

suite.addTest(new MoneyTest("money equals") { protected void runTest() { testEquals(); } } ); suite.addTest( new MoneyTest("simple add") { protected void runTest() { testAdd(); } } ); return suite; }

Page 19: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

junit

TestSuite

MyClass

TestCase

exercise1..*

TestRunner

run1..*MyClassTest

test1

test2

test case

TestSuite

test suite

Page 20: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

TestRunner

http://junit.sourceforge.net/javadoc/junit/awtui/TestRunner.html

http://junit.sourceforge.net/javadoc/junit/swingui/TestRunner.html

http://junit.sourceforge.net/javadoc/junit/textui/TestRunner.html

Page 21: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

JUnit Rules and Conventions

Subclass TestCase Test methods

– public void testXXX() [throws …]– Any number of assertions per method

Implement main to run from command-line, but not necessary

Optionally add setUp / tearDown methods. Add a suite method if you want to create a t

est suite

Page 22: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Terminology A test case tests a particular method

of you class. (Eg testEquals, testGet, testSet in our example)

A test suite is a collection of test cases A test runner is software that runs

tests and reports results

Page 23: JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

Questions?

vinubalaji at gmail dot com