Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit Testing with JUnit Dan Fleck Fall 2007 (For both CS211 and 421… two birds… one lecture! :-)

Similar presentations


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

3 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

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

5 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)

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

7 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)

8 Junit Tutorials 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

9 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

10 Coverage Analysis with Netbeans ZInstall Unit Test Code Coverage Viewer module Zhttp://codecoverage.netbeans.org/ ZWrite a Unit Test ZRun test and view highlighted code

11 A simple test to dynamically cover your code: 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); }

12 How to write test cases ZSee sample system test case ZSee sample unit test case

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

14 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


Download ppt "Unit Testing with JUnit Dan Fleck Fall 2007 (For both CS211 and 421… two birds… one lecture! :-)"

Similar presentations


Ads by Google