JavaLib
 
Tester
 
Setup
 
Main
Annotations
Select Tests
Select Reports
 
Sources
Downloads
 
TSRJ Home
TSRJ Local

Using Annotations to define test classes and methods

Java allows the programmer to use Annotations to provide additional information about its classes, methods, and fields, that the program may use at the run time. The tester library uses Annotations to allow the programmer to specify that a class may contain test methods and to specify that a method with an arbitrary name is to be run as a test method. This methodology is much more powerful than the previous manner of invoking the tester library.

By adding the @Example above any class declaration, you can tell the tester to use that class as an example class (i.e. notifying the tester to look for the test methods defined in that class). This means that you can declare any number of classes to be example classes. The tester will run all of the tests contained within each class that is annotated as an example class.

The tester will still run every method whose name begins with 'test' in every such class; however, this naming convention can be bothersome. Or, what if you already have a function that you would like to use as a test method, but it is referenced in numerous places in your code? In any class marked by the @Example annotation, the tester library will also run as a test every method marked with a @TestMethod annotation.

Note that methods annotated by @TestMethod must still take a Tester object as an argument.

Here is an example:

import tester.*;
@Example //declares this class to be an example class
public class Song {
  String title;
  String artist;
  int duration;
  int rating;
  boolean released;
	
  public Song(){
    this.title = "Nothing";
    this.artist = "No One";
    this.duration = 0;
    this.rating = 0;
    this.released = false;
  }
	
  public Song(String title, String artist, int dur, int rate, boolean released){
    this.title = title;
    this.artist = artist;
    this.duration = dur;
    this.rating = rate;
    this.released = released;
  }
	
  //change the title of this Song
  public void changeName(String title){
    this.title = title;
  }
	
  //calculate the cost of this song
  public int cost(){
    if(this.released){
      return 2 * this.rating;	
    }
    return 0;
  }
	
  //will be run as a test
  @TestMethod
  public void doSomeStuff(Tester t){
    t.checkExpect(true, "hahaha");
  }
	
  //will also be run as a test
  public void testCost(Tester t){
    if(this.released){
      t.checkExpect(this.cost(), 2 * this.rating, "TestCost");
    } else{
      t.checkExpect(this.cost(), 0, "TestCost");
    }
  }

  //also run as a test
  public void tests(Tester abc) {
    testCost(abc);
  }
}

Note that making one or more of your classes serve as an example class does not invalidate those classes in any way. Being an example class merely indicates that the class contains test data in addition to all of the material it is meant to contain as a part of your program.

Note that if you attempt to use the Java Annotations method of declaring tests, you must also be careful. If a class you have marked with @Example implements IExamples, (see the Select Tests link) it will ignore all @TestMethod annotations.


last updated on Fri Apr 1 14:26:43 EDT 2011generated with DrRacket