1 / 19

A Unit Testing Framework: JUnit

A Unit Testing Framework: JUnit. www.junit.org COMP 302 Software Engineering Koc University, Istanbul. The Money class to be tested. class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount;

xander
Download Presentation

A Unit Testing Framework: JUnit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Unit Testing Framework: JUnit www.junit.org COMP 302 Software Engineering Koc University, Istanbul

  2. The Money class to be tested 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()); } } JUnit

  3. A Simple TestCase public class MoneyTest extends TestCase { //… protected void runTest() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); Assert.assertTrue(expected.equals(result)); } } Junit API JUnit

  4. Scenarios • 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 JUnit

  5. A Simple TestCase public class MoneyTest extends TestCase { //… public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Assert.assertTrue(!m12CHF.equals(null)); Assert.assertEquals(m12CHF, m12CHF);Assert.assertEquals(m12CHF, new Money(12, "CHF"));Assert.assertTrue(!m12CHF.equals(m14CHF)); } } JUnit

  6. Overriding “equals()” method public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money)anObject; return (aMoney.currency().equals(currency())) && (amount() == aMoney.amount()); } return false; } JUnit

  7. Decomposing tests into methods public class MoneyTest extends TestCase { public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); Assert.assertTrue(expected.equals(result)); } public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Assert.assertTrue(!m12CHF.equals(null)); Assert.assertEquals(m12CHF, m12CHF); Assert.assertEquals(m12CHF, new Money(12, "CHF")); Assert.assertTrue(!m12CHF.equals(m14CHF)); } } JUnit

  8. Using “setUp” to share objects across test methods public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); } protected void tearDown() throws java.lang.Exception { ... } } JUnit

  9. Test methods using shared objects public void testEquals() { Assert.assertTrue(!f12CHF.equals(null)); Assert.assertEquals(f12CHF, f12CHF); Assert.assertEquals(f12CHF, new Money(12, "CHF")); Assert.assertTrue(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); Assert.assertTrue(expected.equals(result)); } JUnit

  10. Running a TestCase 1 public class MoneyTest extends TestCase { //Override runTest and define the method to be invoked protected void runTest() { ... } } // Running TestCase test = new MoneyTest(“adding test”); test.run(); JUnit

  11. Running a TestCase 2 public class MoneyTest extends TestCase { //… public void testSimpleAdd() { ... } //… public void testEquals() { ... } } // Running TestCase test = new MoneyTest("simple add and equals"){ public void runTest(){ testSimpleAdd(); testEquals(); } }; test.run(); JUnit

  12. Running a TestCase 3 public class MoneyTest extends TestCase { //… public void testSimpleAdd() { ... } //… public void testEquals() { ... } } // Running TestCase test = new MoneyTest(“testSimpleAdd”); test.run(); ... TestCase test = new MoneyTest(“testEquals”); test.run(); JUnit

  13. Running more than one tests:Test Suites public class MoneyTest extends TestCase { public void testSimpleAdd(){...} public void testEquals() {...} 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() { testSimpleAdd(); } } ); return suite; } } JUnit

  14. Test Suites 2 public class MoneyTest extends TestCase { public void testSimpleAdd(){...} public void testEquals() {...} public static Test suite(){ TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest("testEquals"));   suite.addTest(new MoneyTest("testSimpleAdd")); return suite; } } JUnit

  15. Running Test Suites MoneyTest test = new MoneyTest(“tests”); TestSuite suite = test.suite(); suite.run(); JUnit

  16. JUnit

  17. Testing Hints • During Development: When you need to add new functionality to the system, write the tests first. Then, you will be done developing when the test runs. • During Debugging: When someone discovers a defect in your code, first write a test that will succeed if the code is working. Then debug until the test succeeds. JUnit

  18. JUnit

  19. JUnit

More Related