Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.

Similar presentations


Presentation on theme: "Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005."— Presentation transcript:

1 week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” http://www.bluej.org/tutorial/testing-tutorial.pdf October 28, 2005

2 week82 Unit Testing The individual testing of separate units of a software system  In Scheme, we tested each function we wrote  In Java, we are going to test individual classes and methods BlueJ uses JUnit, a regression testing framework  Regression testing basically allows a programmer to rerun tests at anytime to make sure that recent changes/additions haven’t broken code  More information about JUnit at their website; www.junit.org

3 week83 To use Testing Need to set a BlueJ preference to Show testing tools Then you get new buttons on the interface

4 week84 Testing Now when you right-click on an object, you can choose to “Create Test Class” Doing so will create a new class that is linked to the class being tested

5 week85 Test Class The Test Class Menu gives the programmer several options

6 week86 Testing Methods When you choose “Create Test Method” you will be prompted to enter a name for the test  test methods always start with the prefix “test” and it will be automatically added if you don’t put it (so “testName” and “name” would both create a method called “testName” Then everything you do after clicking OK will be recorded as part of the test (until you click the “End” or “Cancel” buttons)

7 week87 Recording the test Make an object Call methods, entering parameters as needed When you get a result dialog, you can make an assertion

8 week88 Running Tests If we right-click the test class, we can choose to “Test All” or we can choose individual tests to run

9 week89 Reducing Overhead Instead of creating the object each time we are running a set of tests (especially if a given problem requires us to create multiple objects), we can create the objects once on the object bench and then choose “Object Bench to Test Fixture”  This will create the initial scenario for all test methods that you create

10 week810 Writing By Hand You may also choose to make test cases by hand - making the source code just like any other class  But you need to follow the JUnit guidelines Class extends junit.framework.TestCase One method setUp() One void method for each test, method names starting with “test” assertEquals method call to check values

11 week811 eXtreme Programming A way of approaching software design Says that you should create test cases before the implementation of a method  In BlueJ, that means either: Doing it by hand Make “stub” methods first (with dummy values being returned) then make the test cases interactively, making the assertions according to expectations of the finished program

12 week812 Lab: Projects From book 1.Design a class with one method that takes an integer value as a parameter and prints the sum of all even integers between 2 and the integer value, inclusive. Design and program appropriate test cases 2.Design and implement an application that reads an integer value representing a year from the user. The purpose pf the program is to determine if the year is a leap year (and therefore has 29 days in February) in the Gregorian calendar. A year is a leap year if it is divisible by 4, unless it is also divisible by 100 but not 400. For example, the year 2003 is not a leap year, but 2004 is. The year 1900 is not a leap year because it is divisible by 100, but the year 200 is a leap year because even though it is divisible by 100, it is also divisible by 400. Produce an error message for any input less than 1582 (the year the Gregorian calendar was adopted).  Design and program appropriate test cases. Hint: use all the examples given in the problem statement


Download ppt "Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005."

Similar presentations


Ads by Google