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

Selecting the tests to run using IExamples interface

Using the IExamples interface is a good way to centralize all of your tests. By having the class you wish to use as an example class implement this interface, you signal to the tester that all tests are contained within an encapsulating tests(Tester t) method. The purpose of this is to make altering your testing scheme quick and easy. Because all of the tests you wish to run are called or written within this one container method, it is simple to comment out a line and thus exclude a test. Consider the following class:

import tester.*;

public class Book {
  String title;
  String author;
  int pages;
  boolean hardcover;
	
  public Book(){
    this.title = "No Book";
    this.author = "No One";
    this.pages = 0;
    this.hardcover = false;
  }
	
  public Book(String title, String author, int pages, boolean hardcover){
    this.title = title;
    this.author = author;
    this.pages = pages;
    this.hardcover = hardcover;
  }

  // compute the cost of this book	
  public int cost(){
    return pages * (1 / 2);
  }
	
  // produce a book like this one, but with the given title
  public Book changeTitle(String title){
    return new Book(title, this.author, this.pages, this.hardcover);
  }
	
  // test the method 'changeTitle'
  public void testTitleChange(Tester t){
    t.checkExpect(this.changeTitle("New Title"), 
    new Book("New Title", this.author, this.pages, this.hardcover));
  }
	
  // test the method 'cost'
  public void testCost(Tester t){
    t.checkExpect(this.cost(), this.pages * (1 / 2));
  }
}

Following the basic variant, you would declare this class as the example class either by using the @Example annotation, or by passing "Book" as a run time argument to the compiler. Then, the tester's Main class would run its main method, which would detect any @TestMethod annotations or any methods whose name begins with test. It would then execute these functions, and display a report.

Now let's change our class definition slightly:

public class Book implements IExamples{ //...
and we add our tests method:

// run all tests
public void tests(Tester t) {
  testCost(t);
  testTitleChange(t);
}
And now the Tester will run this tests method. It is now much simpler to see which tests are being run, and alter which tests are processed. Simply comment out a test, and it will be ignored. This makes it much easier to maintain your test code! Before, you would have had to find and comment out an entire test method; in larger classes or classes that contain numerous tests, that can be an annoying process. Keep in mind that now, any methods marked with the @TestMethod annotation are not processed as test methods; you must add these to this tests method as well. Likewise, any test not contained within tests is ignored.


You must be careful when switching to or from the use of this testing method. As described in the basic variant, the Main class typically runs any method that begins with the word test as a test. However, the moment you tack implements IExamples onto your class definition, those tests are ignored. The only method the tester will now run is the void tests(Tester t). The point of the IExamples interface is to localize testing, so if you wish to run those tests, you must call them in the tests method, passing each function the given tester.

On the other hand, if you use the IExamples interface, and later decide to switch styles, you should remember to comment out or remove your tests method. This is because the basic variant will detect the tests method as a test, because it starts with test (and so will invoke all test methods within the tests method again.) It is OK to leave the tests method in; just make sure that you are aware of this behavior, in case you don't mean to run the method.

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, it will ignore all @TestMethod annotations.


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