Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,

Similar presentations


Presentation on theme: "Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,"— Presentation transcript:

1 Unit Testing with JUnit Dan Fleck Spring 2010

2

3 What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure, method or class ZValidating each individual piece reduces errors when integrating the pieces together later

4 Automated Unit Tests with JUnit ZJunit is a unit testing framework for Java ZAllows you to write unit tests in Java using a simple interface ZAutomated testing enables running and rerunning tests very easily and quickly

5 An example unit test @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")); }

6 Junit Assert ZDuring a test use Asserts to specify if the test passed or failed Zorg.junit.Assert – allows you to test if certain ideas hold by asserting results: http://junit.sourceforge.net/javadoc/ http://junit.sourceforge.net/javadoc/ ZassertEquals(expected, actual) ZassertEquals(message, expected, actual) ZassertEquals(expected, actual, delta) ZassertEquals(message, expected, actual, delta) ZassertFalse(condition) ZassertFalse(message, condition) ZAssert(Not)Null(object) ZAssert(Not)Null(message, object) ZAssert(Not)Same(expected, actual) ZAssert(Not)Same(message, expected, actual) ZassertTrue(condition) ZassertTrue(message, condition)

7 Junit Methods – Java annotations Z@BeforeClass // Run before all tests in class Z public static void setUpClass() throws Exception {} Z@AfterClass // Run after all tests in class Zpublic static void tearDownClass() throws Exception {} Z@Before // Run before each test in class Zpublic void setUp() {} Z@After // Run after each test in class Zpublic void tearDown() {} Z@Test Zpublic void testMain() { Zhttp://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/

8 Junit with Netbeans 1.New File 2.Choose file type: Junit 3.Choose Test for Existing Class 4.Junit test with all stubs created for that class 5.Fill in the individual tests 6.Run Tests (Netbeans options) Note: If this option doesnt exist use New File->Other->Junit->Test for existing class

9 Junit Documentation/Tutorials Zhttp://junit.sourceforge.net/http://junit.sourceforge.net/ Zhttp://code.google.com/p/t2framework/wiki/J UnitQuickTutorialhttp://code.google.com/p/t2framework/wiki/J UnitQuickTutorial Zhttp://junit.sourceforge.net/doc/testinfected/te sting.htm (older)http://junit.sourceforge.net/doc/testinfected/te sting.htm

10 Lines of code coverage analysis ZDetermining which lines of code your tests have exercised and which they have not ZAllows you to detect if your unit tests (or system tests) are adequately covering all possibilities or not ZThis is just one way to test

11 Lines of code coverage analysis with Netbeans Install Unit Test Code Coverage Viewer module (See next slide for instructions) Write a Unit Test Run test and view highlighted code

12 Installing the Code Coverage Plugin ZGo into Netbeans -> Tools -> Plugins Search for "Code Coverage" and install it. ZIf you don't see it in the list of available plugins, go to the "Settings" tab and check "Netbeans Beta, reload the catalog, and then try again. ZIf you don't see Netbeans Beta in the list on Settings, add the following source for plugins: Netbeans Beta http://updates.netbeans.org/netbeans/updates/6.8 /uc/final/beta/catalog.xml.gz

13 How do you you combine coverage with traditional system test scripts? Traditional system testing uses scripts -1. Enter salary -2. Enter number of dependents -3. Click Calculate Taxes button -… To combine this with coverage, launch the GUI while capturing coverage statistics Run the test case to determine coverage

14 How to write test cases ZSee sample system test case Zhttp://www.cs.gmu.edu/~kdobolyi/cs421/SAM PLESystemTestCase.doc ZSee sample unit test case Zhttp://www.cs.gmu.edu/~kdobolyi/cs421/Mai n.javahttp://www.cs.gmu.edu/~kdobolyi/cs421/Mai n.java Zhttp://www.cs.gmu.edu/~kdobolyi/cs421/Trian gle.javahttp://www.cs.gmu.edu/~kdobolyi/cs421/Trian gle.java Zhttp://www.cs.gmu.edu/~kdobolyi/cs421/Trian gle.java

15 Junit Example ZTry to write unit tests for /javacode/JUnitRobotExample

16 Class Exercise - Lets try it out! ZUsing the SystemRequirementsSpecificationExample.doc ZEach team pick a use case ZDocument the overall system or unit tests you would write to test ZDo the same for a non- functional requirement (sec 5)

17 Summary ZUnit tests can help test the details of your program ZAutomated unit tests provide constant visibility and easy retesting ZTest coverage supplies valuable information when running both unit tests and system tests

18 Unit test to use with system test case public void testMain() { SuDoku sFrame = new SuDoku(); // Launch the GUI // While GUI is showing while (sFrame.isDisplayable()) { try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } Assert.assertEquals(true,true); }


Download ppt "Unit Testing with JUnit Dan Fleck Spring 2010. What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample: A procedure,"

Similar presentations


Ads by Google