View
243
Download
1
Embed Size (px)
Citation preview
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
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
junit
How to Test with JUnit?
Student
TestCase
exercise1..*
TestRunner
run1..*TestStudent
test1
test2
…
Currency Exampleclass Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; }
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
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
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"));
}
}
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
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;
}
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}
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.
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.
junit
Foo
TestCase
exercise1..*
TestRunner
run1..*FooTest
test1
test2
…
setUp()tearDown()
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
}
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);
}
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; }
junit
TestSuite
MyClass
TestCase
exercise1..*
TestRunner
run1..*MyClassTest
test1
test2
…
test case
TestSuite
test suite
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
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
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
Questions?
vinubalaji at gmail dot com