Quick Index
Overview


We've previously added methods to the Rectangle class and verified that it compiles okay.

Now we want to verify that Rectangle objects behave (run) as expected.

It is suggested you type along with this example rather than copy-paste for muscle memory.

Let's quickly review our previous work with Rectangle... We will utilize that work here.

Review


Recall in the previous challenge that we added these methods to the Rectangle class:


We will now test the functionality of all of these methods.

  • Go to the working directory that contains "Rectangle.java" (or create a new working directory and copy "Rectangle.java" into it)
  • Create new file named "RectangleTests.java" in the working directory
  • Type or copy the code shown here into the new file

The Rectangle code can be found in this previous example...
public class RectangleTests {

	public void testSetSmall() {

	}

	public void testSetLarge() {

	}

	//=======================================

	public static void main(String[] args) {
		RectangleTests test = new RectangleTests();
		test.testSetSmall();
		test.testSetLarge();
	}

}

Expected Behavior - 'setSmall'


Recall the Rectangle's "setSmall" method. For a given Rectangle object the method does this:


Coding The 'testSetSmall()' Method


Now we want to test the results of sending the message "setSmall". A simple way to do that is to print the rectangle state/values. Here is a quick synopsis on basic printing in Java...

  • Construct Rectangle and assign to variable rec
  • Send message setSmall to var rec
  • Show expected width
  • Show actual width by sending message getWidth to var rec

We send the message "getHeight" similarly
	/**
	 * Test that the method setSmall sets the Rectangle
	 * to the expected width and height of 5 and 1.
	*/
	public void testSetSmall()  {

		Rectangle rec;

		
1
rec = new Rectangle();
2
rec.setSmall(); System.out.println("-- testSetSmall --"); System.out.println("
3
Expected Width: "
+ 5); System.out.println("Expected Height: " + 1); System.out.println("Actual Width: " +
4
rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); }

The test passes if the expected and actual values match.

If they don't pass, that just means we conduct a bug hunt (i.e., finding and fixing the program bug).
-- testSetSmall --
Expected Width: 5
Expected Height: 1
Actual Width: 5
Actual Height: 1

Expected Behavior - 'setLarge'


Recall the Rectangle's "setLarge" method. For a given Rectangle object the method does this:


Coding The 'testSetLarge()' Method


The "testSetLarge" method code follows the same pattern as the "testSetSmall" code.

  • Construct Rectangle and assign to variable rec
  • Send message setLarge to var rec
  • Show expected width
  • Show actual width by sending message getWidth to var rec
	/**
	 * Test that the method setSmall sets the Rectangle
	 * to the expected width and height of 100 and 50.
	*/
	public void testSetLarge()  {

		Rectangle rec;

		
1
rec = new Rectangle();
2
rec.setLarge(); System.out.println("-- testSetLarge --"); System.out.println("
3
Expected Width: "
+ 100); System.out.println("Expected Height: " + 50); System.out.println("Width: " +
4
rec.getWidth()); System.out.println("Height: " + rec.getHeight()); }

The test passes if the expected and actual values match.
-- testSetLarge --
Expected Width: 100
Expected Height: 50
Width: 100
Height: 50

Try It 💡


📹 Video

Try the code here

Completed Code


The final test code is shown here.

It can also be downloaded
public class RectangleTests {

	/** Test that the method setSmall sets the Rectangle
	to the expected width and height of 5 and 1.*/
	public void testSetSmall() {

		Rectangle rec;

		rec = new Rectangle();

		rec.setSmall();

		System.out.println("-- testSetSmall --");
		System.out.println("Expected Width: " + 5);
		System.out.println("Expected Height: " + 1);
		System.out.println("Actual Width: " + rec.getWidth());
		System.out.println("Actual Height: " + rec.getHeight());
	}

	/** Test that the method setSmall sets the Rectangle
	to the expected width and height of 100 and 50.*/
	public void testSetLarge() {

		Rectangle rec;

		rec = new Rectangle();

		rec.setLarge();

		System.out.println("-- testSetLarge --");
		System.out.println("Expected Width: " + 100);
		System.out.println("Expected Height: " + 50);
		System.out.println("Width: " + rec.getWidth());
		System.out.println("Height: " + rec.getHeight());
	}

	//=======================================

	public static void main(String[] args) {
		RectangleTests test = new RectangleTests();
		test.testSetSmall();
		test.testSetLarge();
	}

}