RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lect02c
test classes; reading docs


- Review/practice:
  - construct at 20x30 Rectangle whose NW corner is at 100,200.
  - declare a variable which can hold a Rectangle.
  - Initialize your variable with a 80x80 Rectangle at 0,0.
  - Ask that Rectangle what its X coord of NW corner is.
  - call the method  setLocation(int,int)
  - Again, ask that Rectangle what its X coord of NW corner is.
  - Note that we can make several different vars each holding
    different Rectangles.
    (Draw picture on board; compare to Inspect window.)

New:
- make a test class, class TestRectangle
  Note: in a test-method, you figure the answers *first*
    Write tests for: grow(int,int)
                     setLocation(int,int);    
                     translate(int,int)
  Tip: have *some* test cases involving zeroes and ones, whenever applicable.
  Mindset: you'll get paid $100 if you find an incorrect result!
           ...er, rather, you won't lose 20pts (or, your job).
- Look up documentation for java.awt.Rectangle.
- For several different methods, 
   (a) read docs; 
   (b) call interactively (*after* figuring out the expected answer);
   (c) write a test case.
   Try: grow(int,int)
        contains(int,int)
        add(int,int)
        union(Rectangle)
 1  class RectangleTester {
 2      
 3      public static void main( String[] args ) {
 4          java.awt.Rectangle r = new java.awt.Rectangle( 10, 15, 100, 200 );
 5          r.grow( 5, 5 );
 6          System.out.println( "r's width is now " + r.getWidth() + "; I expected 110." );
 7          System.out.println( "r's height is now " + r.getHeight() + "; I expected 210." );
 8          System.out.println( "r's NW corner is at "
 9                            + "(" + r.getX() + "," + r.getY() + ")"
10                            + "; I expected (5,10)." );
11          }
12      
13      }

To test a method

  1. Create an object
  2. call the method to be tested
  3. Compare the result to the expected value. For now, we'll print them.
    If it's a mutator method (which doesn't return a result), then you must call one or more getters, and make sure the object has changed as expected.
  4. Repeat as necessary.
    (Usually, at least two-three tests are necessary to test a single method.)

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Sep.12 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme