Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Unit testing with JUnit

24,043 views

Published on

Sample lecture presented at University of Calgary.

Published in: Technology, Business
  • My son was struggling with GCSE revision and being a typical boy his desire to stay inside and study was not too high on the agenda! His maths has always been pretty average, and he has always had very little confidence in his mathematical abilities. Jeevan's programme is excellent. The break down of all mathematical problems has completely changed the way my son approaches his work and he is no longer doubting his ability. I have no doubt that this programme is the best there is and would have complete confidence in recommending it to anyone studying GCSE maths. On a side note, Jeevan is a perfect gentleman. He is very approachable, kind and generous with his advice. An efficient young man who truly cares about what he is offering. If you purchase this programme, you will certainly not be disappointed. ▲▲▲ http://t.cn/AirraVnG
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Although I'm a KS3 student, I decided to give Jeevan's guide a try because I know GCSE and KS3 are fairly similar. Looking back now, it was one of the best decisions I made. The guide has helped me jump from a level 6A to a level 8C in maths, and I'm only in year 8! Achieving a level 8 in maths is regarded as a gifted individual. Thank you for this terrific guide and I will recommend it to anyone taking a maths exam!  http://t.cn/AirraVnG
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Your GCSE Maths program has helped me immensely in maths. I am much more confident with this subject and I'm striving for better grades. I really appreciate the time you took in making this program because it has boosted many students self-confidence with their exams. ◆◆◆ http://ishbv.com/jeevan91/pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • It is very helpful to me could you please send to nicolaitesla369@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • This PPT is very helpful to me could you please send to dp.reddy.80@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Unit testing with JUnit

  1. 1. Unit testing with JUnit Tom Zimmermann
  2. 2. Testing Testing is the activity of finding out whether a piece of code (a method, class, or program) produces the intended behavior. taken from “Objects First with Java”
  3. 3. Edsger Dijkstra Program testing can be used to show the presence of bugs, but never to show their absence!
  4. 4. I don’t make mistakes!
  5. 5. I don’t make mistakes!
  6. 6. Test phases Unit testing on individual units of source code (=smallest testable part). Integration testing on groups of individual software modules. System testing on a complete, integrated system (evaluate compliance with requirements)
  7. 7. JUnit Kent Beck Erich Gamma
  8. 8. JUnit taken from “JUnit: A Cook’s Tour”
  9. 9. Tests in JUnit Tests are realized as public void testX() methods. A test typically calls a few methods, then checks if the state matches the expectation. If not, it fails. Example: Call empty() on the shopping cart and check the state with isEmpty() // Tests the emptying of the cart. public void testEmpty() { _bookCart.empty(); assertTrue(_bookCart.isEmpty()); }
  10. 10. JUnit Checklist Initialize Write test cases Write a test suite Execute test suite
  11. 11. Example: Shopping cart Add product 1
  12. 12. Example: Shopping cart Add product 1 Remove product 2
  13. 13. Example: Shopping cart Set of products 1 Add product 1 Remove product 2
  14. 14. Example: Shopping cart Set of products 1 Number of products 2 Add product 1 Remove product 2
  15. 15. Example: Shopping cart Set of products 1 Number of products 2 Balance 3 Add product 1 Remove product 2
  16. 16. Part 1: Initialize Constructor + Set up and tear down of fixture. import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class ShoppingCartTest extends TestCase { private ShoppingCart _bookCart; // Creates a new test case public ShoppingCartTest(String name) { super(name); }
  17. 17. Test fixture // Creates test environment (fixture). // Called before every testX() method. protected void setUp() { _bookCart = new ShoppingCart(); Product book = new Product(quot;Harry Potterquot;, 23.95); _bookCart.addItem(book); } // Releases test environment (fixture). // Called after every testX() method. protected void tearDown() { _bookCart = null; }
  18. 18. Part 2: Write test cases Help methods inherited from TestCase: – triggers a failure named msg fail(msg) – triggers a failure, when condition b is false assertTrue(msg, b) – triggers a failure, when v1 ≠ v2 assertEquals(msg, v1, v2) – triggers a failure, when |v1−v2|> ε assertEquals(msg, v1, v2, ε) – triggers a failure, when object is not null assertNull(msg, object) – triggers a failure, when object is null assertNonNull(msg, object) The parameter msg is optional.
  19. 19. Test: Add product // Tests adding a product to the cart. public void testProductAdd() { Product book = new Product(quot;Refactoringquot;, 53.95); _bookCart.addItem(book); assertTrue(_bookCart.contains(book)); double expected = 23.95 + book.getPrice(); double current = _bookCart.getBalance(); assertEquals(expected, current, 0.0); int expectedCount = 2; int currentCount = _bookCart.getItemCount(); assertEquals(expectedCount, currentCount); }
  20. 20. Test: Add product // Tests adding a product to the cart. public void testProductAdd() { Product book = new Product(quot;Refactoringquot;, 53.95); _bookCart.addItem(book); assertTrue(_bookCart.contains(book)); double expected = 23.95 + book.getPrice(); double current = _bookCart.getBalance(); assertEquals(expected, current, 0.0); int expectedCount = 2; int currentCount = _bookCart.getItemCount(); assertEquals(expectedCount, currentCount); }
  21. 21. Test: Remove product // Tests removing a product from the cart. public void testProductRemove() throws NotFoundException { Product book = new Product(quot;Harry Potterquot;, 23.95); _bookCart.removeItem(book); assertTrue(!_bookCart.contains(book)); double expected = 23.95 - book.getPrice(); double current = _bookCart.getBalance(); assertEquals(expected, current, 0.0); int expectedCount = 0; int currentCount = _bookCart.getItemCount(); assertEquals(expectedCount, currentCount); }
  22. 22. Test: Remove product // Tests removing a product from the cart. public void testProductRemove() throws NotFoundException { Product book = new Product(quot;Harry Potterquot;, 23.95); _bookCart.removeItem(book); assertTrue(!_bookCart.contains(book)); double expected = 23.95 - book.getPrice(); double current = _bookCart.getBalance(); assertEquals(expected, current, 0.0); int expectedCount = 0; int currentCount = _bookCart.getItemCount(); assertEquals(expectedCount, currentCount); }
  23. 23. Test: Exception handling // Tests removing an unknown product from the cart. public void testProductNotFound() { try { Product book = new Product(quot;Bonesquot;, 4.95); _bookCart.removeItem(book); fail(quot;Should raise a NotFoundExceptionquot;); } catch (NotFoundException nfe) { // Should always take this path } }
  24. 24. Test: Exception handling // Tests removing an unknown product from the cart. public void testProductNotFound() { try { Product book = new Product(quot;Bonesquot;, 4.95); _bookCart.removeItem(book); fail(quot;Should raise a NotFoundExceptionquot;); } catch (NotFoundException nfe) { // Should always take this path } }
  25. 25. Part 3: Write a test suite The method suite() assembles the test methods into a test suite – using reflection all methods named testX() will be part of the test suite. public static Test suite() { // Here: add all testX() methods to the suite (reflection). TestSuite suite = new TestSuite(ShoppingCartTest.class); // Alternative: add methods manually (prone to error) // TestSuite suite = new TestSuite(); // suite.addTest(new ShoppingCartTest(quot;testEmptyquot;)); // suite.addTest(new ShoppingCartTest(quot;testProductAddquot;)); // suite.addTest(...); return suite; }
  26. 26. Part 4: Execute test suite // Returns the name of the test case. public String toString() { return getName(); } // Main method: Call GUI public static void main(String args[]) { String[] tcName = { ShoppingCartTest.class.getName() }; junit.swingui.TestRunner.main(tcName); // junit.textui.TestRunner.main(tcName); } Execute the main() method to run the tests: $ java ShoppingCartTest
  27. 27. Demo
  28. 28. I know I should test, but everything?
  29. 29. Best practices Tests should be written before the code. Test everything that could reasonably break. If it can't break on its own, it's too simple to break (like most get and set methods). Run all your unit tests as often as possible.
  30. 30. Best practices (cont.) Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead. Martin Fowler
  31. 31. Bits of research
  32. 32. Delta debugging Yesterday, my program worked. Today, it does not. Zeller (ESEC/FSE 1999), Burger et al. (ETX 2003)
  33. 33. Delta debugging Yesterday, my The change in program worked. Line 42 makes Today, it does not. the program fail. Zeller (ESEC/FSE 1999), Burger et al. (ETX 2003)
  34. 34. Continuous testing Continuously run tests in the background rapid feedback about test failures Saff and Ernst (ISSRE 2003, ISSTA 2004)
  35. 35. Continuous testing Continuously run tests in the background rapid feedback about test failures Saff and Ernst (ISSRE 2003, ISSTA 2004)
  36. 36. Summary Testing Literature @ junit.org Unit testing JUnit Test Infected. Integration testing JUnit A Cook's Tour. System testing Self-study questions JUnit How can we test protected Test cases and private methods? Test suites What distinguishes a good Fixtures test suite from a bad one? Think of situations that are Best practices difficult to test.

×