Unit Testing with JUnit Dan Fleck Fall 2007

  • Slides: 9
Download presentation
Unit Testing with JUnit Dan Fleck Fall 2007 (For both CS 211 and 421…

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

What is Unit Testing? ZA procedure to validate individual units of Source Code ZExample:

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

Automated Unit Tests with JUnit ZJunit is a unit testing framework for Java ZAllows

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

An example unit test public void test. Cell. Change. Propagates() { Spreadsheet = new

An example unit test public void test. Cell. Change. Propagates() { Spreadsheet = new Spreadsheet(); sheet. put("A 1", "5"); sheet. put("A 2", "=A 1"); sheet. put("A 1", "10"); assert. Equals("10", sheet. get("A 2")); }

Junit with Netbeans 1. 2. 3. 4. New File Choose file type: Junit Choose

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

Coverage Analysis ZDetermining which lines of code your tests have exercised and which they

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

Coverage Analysis with Netbeans ZInstall Unit Test Code Coverage Viewer module ZWrite a Unit

Coverage Analysis with Netbeans ZInstall Unit Test Code Coverage Viewer module ZWrite a Unit Test ZRun test and view highlighted code

A simple test to dynamically cover your code: public void test. Main() { Su.

A simple test to dynamically cover your code: public void test. Main() { Su. Doku s. Frame = new Su. Doku(); while (s. Frame. is. Displayable()) { try { Thread. sleep(100); } catch (Exception e) { e. print. Stack. Trace(); } } Assert. assert. Equals(true, true); }

Summary ZUnit tests can help test the details of your program ZAutomated unit tests

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