1 / 12

APCS-AB: Java

APCS-AB: Java. Unit Testing Information today from “Unit Testing in BlueJ” http://www.bluej.org/tutorial/testing-tutorial.pdf October 28, 2005. Unit Testing. The individual testing of separate units of a software system In Scheme, we tested each function we wrote

albert
Télécharger la présentation

APCS-AB: Java

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. APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” http://www.bluej.org/tutorial/testing-tutorial.pdf October 28, 2005

  2. 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. To use Testing • Need to set a BlueJ preference to Show testing tools • Then you get new buttons on the interface

  4. 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. Test Class • The Test Class Menu gives the programmer several options

  6. 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. Recording the test • Make an object • Call methods, entering parameters as needed • When you get a result dialog, you can make an assertion

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

  9. 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. 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. 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. Lab: Projects From book • 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 • 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

More Related