Skip to the content.

Unit Testing and Encapsulation

Back to Homepage
Week 2


Types of Testing

Unit Tests


/*
Class invariant: Value is always between 0.0 and 100.0 (Guarenteed, promise)
  Write tests to verify this is always true
*/

class Percentage {
  public double Value {
    get{return value;};
    set(double newValue) {
      if (newValue < 0.0 || newValue > 100) {
        throw newInvalidArgumentException();
      }
      value = newValue;
    }
  } 
}

Class invariants should be true throughout the life of the object

constructor
|
|
| Method Calls
|
|
destructor / Garbage collector