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

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

Embed Size (px)

Citation preview

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

week8 1

APCS-AB: Java

Unit TestingInformation today from “Unit Testing in BlueJ”

http://www.bluej.org/tutorial/testing-tutorial.pdf

October 28, 2005

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

week8 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

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

week8 3

To use Testing

• Need to set a BlueJ preference to Show testing tools

• Then you get new buttons on the interface

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

week8 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

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

week8 5

Test Class

• The Test Class Menu gives the programmer several options

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

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

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

week8 7

Recording the test

• Make an object

• Call methods, entering parameters as needed

• When you get a result dialog, you can make an assertion

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

week8 8

Running Tests

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

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

week8 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

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

week8 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

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

week8 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

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

week8 12

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