Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example."— Presentation transcript:

1 JUnit Introduction and Advanced Features

2 Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example

3 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.phphttp://sourceforge.net/index.php –http://junit.sourceforge.net/http://junit.sourceforge.net/ –http://www.junit.org/http://www.junit.org/  Used by Java programmer

4 JUnit Framework http://junit.sourceforge.net/javadoc/index.html

5 junit How to Test with JUnit? Student TestCase exercise 1..* TestRunner run 1..* TestStudent test 1 test 2 …

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

7 Currency (cont) public int getAmount() { return fAmount; } public String getCurrency() { return fCurrency; } public Money add(Money m) { return new Money(amount()+m.amount(), currency()); } }// Class Money Ends

8 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

9 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")); }

10 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/fra mework/Assert.html

11 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; }

12 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 }

13 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.

14 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 resources you allocated in setUp.

15 junit Foo TestCase exercise 1..* TestRunner run 1..* FooTest test 1 test 2 … setUp () tearDown ()

16 Test Fixture Example public 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 }

17 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); }

18 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; }

19 junit TestSuite MyClass TestCase exercise 1..* TestRunner run 1..* MyClassTest test 1 test 2 … test case TestSuite test suite

20 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

21 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 test suite

22 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

23 Questions? vinubalaji at gmail dot com


Download ppt "JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example."

Similar presentations


Ads by Google