1 / 11

Unit Testing with JUnit

Unit Testing with JUnit. Dan Fleck Fall 2007 (For both CS211 and 421… two birds… one lecture! :-). What is Unit Testing?. A procedure to validate individual units of Source Code Example: A procedure, method or class

phouser
Download Presentation

Unit Testing with 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. Unit Testing with JUnit Dan Fleck Fall 2007 (For both CS211 and 421… two birds… one lecture! :-)

  2. What is Unit Testing? • A procedure to validate individual units of Source Code • Example: A procedure, method or class • Validating each individual piece reduces errors when integrating the pieces together later

  3. Automated Unit Tests with JUnit • Junit is a unit testing framework for Java • Allows you to write unit tests in Java using a simple interface • Automated testing enables running and rerunning tests very easily and quickly

  4. An example unit test public void testCellChangePropagates() { Spreadsheet sheet = new Spreadsheet(); sheet.put("A1", "5"); sheet.put("A2", "=A1"); sheet.put("A1", "10"); assertEquals("10",sheet.get("A2")); }

  5. Junit with Netbeans • New File • Choose file type: Junit • Choose Test for Existing Class • Junit test with all stubs created for that class • Fill in the individual tests • Run Tests (Netbeans options)

  6. Coverage Analysis • Determining which lines of code your tests have exercised and which they have not • Allows you to detect if your unit tests (or system tests) are adequately covering all possibilities or not • This is just one way to test

  7. Coverage Analysis with Netbeans • Install Unit Test Code Coverage Viewer module • Write a Unit Test • Run test and view highlighted code

  8. A simple test to dynamically cover your code: public void testMain() { SuDoku sFrame = new SuDoku(); while (sFrame.isDisplayable()) { try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } Assert.assertEquals(true,true); }

  9. How to write test cases • See sample system test case • See sample unit test case

  10. Class Exercise - Lets try it out! • Using the SystemRequirementsSpecificationExample.doc • Each team pick a use case • Document the overall system or unit tests you would write to test • Do the same for a non-functional requirement (sec 5)

  11. Summary • Unit tests can help test the details of your program • Automated unit tests provide constant visibility and easy retesting • Test coverage supplies valuable information when running both unit tests and system tests

More Related