Index

Diagram


Test Schematic
A simple model (Rectangle) and a test that tests the model.

We use the word "model" for domain classes because they model things in the real world.


Model


Model Structure
Here is the ivars and constructor of our model (Rectangle).
public class Rectangle {
	private int width;
	private int height;

	public Rectangle(int width, int height) {
		width = width;
		this.height = height;
	}

Action
Here is an "action" that the model performs (computes it's area).
public int computeArea()  {
	return this.width * this.height;
}



Minimal Test


  • Construct
  • Action
  • Test
private void test1()  {
	
1
Rectangle rec = new Rectangle(10, 5);
2
int a = rec.computeArea();
3
if (a == 50) prn("PASSED"); else prn("FAILED"); prn("Rectangle: " + rec); prn("Area: " + rec.computeArea()); }



Bugs


Bug #1
A bit of a tricky bug.

Does the minimal test catch it: YES BUG CAUGHT
public Rectangle(int width, int height) {
	width = width;
	this.height = height;
}

Bug #2
Another bug that easily creeps in via copy-pasting.

Does the minimal test catch it: YES BUG CAUGHT
public Rectangle(int width, int height) {
	this.width = width;
	this.height = width;
}

Bug #3
Another bug that easily creeps in. We've forgotten the statement to set height.

Does the minimal test catch it: YES BUG CAUGHT
public Rectangle(int width, int height) {
	this.width = width;
}

Bug #4
Another bug that happens easily. We made a small typo ('+' for '*').

Does the minimal test catch it: YES BUG CAUGHT
public int computeArea() {
	return this.width + this.height;
}



Moral of the Story


The point of this exercise is not to identify different bugs (we would run out of time in that persuit).

The point is that a test (even a minimal test) is our best friend for bug hunting.

Once you have the minimal test passing, meaning your model is in pretty good shape, you can add more tests.

----------------------------------------------------------------------------------------------------steps1B

50% Done (Tested) vs 100% Done (Not-Tested)


Which is better?


In many cases, believe or not, 50% done (tested) is better than the 100% done (not tested).

The reason is that for the 100% case, if a "show-stopper" bug lurks, then the code will be unusable.