Here is a subset of our test code.
Can we identify any redudant code.
We sure can. The call "System.out.println" is repeated many-many times.
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); System.out.println("\n-- testSetDimensions --"); System.out.println("Set dimensions to 8 and 7"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); System.out.println("\n-- testGrowByWithOneParam --"); System.out.println("Sent growBy with 5 (after setting dims to 100)"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); System.out.println("\n-- testGrowByWithTwoParams --"); System.out.println("Sent growBy with (3, 2) after setting dims to 100"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); }
public void prn(Object o) {
System.out.println(o);
}
In this case we can simply search & replace the verbose 'System.out.println' with the concise 'prn'. We've lightened up the code a bit.
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); prn("\n-- testSetDimensions --"); prn("Set dimensions to 8 and 7"); prn("Actual Width: " + rec.getWidth()); prn("Actual Height: " + rec.getHeight()); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); prn("\n-- testGrowByWithOneParam --"); prn("Sent growBy with 5 (after setting dimensions to 100)"); prn("Actual Width: " + rec.getWidth()); prn("Actual Height: " + rec.getHeight()); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); prn("\n-- testGrowByWithTwoParams --"); prn("Sent growBy with 3 and 2 (after setting dimensions to 100)"); prn("Actual Width: " + rec.getWidth()); prn("Actual Height: " + rec.getHeight()); }
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); prn("\n-- testSetDimensions --"); prn("Set dimensions to 8 and 7"); prn("Actual Width: " + rec.getWidth()); prn("Actual Height: " + rec.getHeight()); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); prn("\n-- testGrowByWithOneParam --"); prn("Sent growBy with 5 (after setting dimensions to 100)"); prn("Actual Width: " + rec.getWidth()); prn("Actual Height: " + rec.getHeight()); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); prn("\n-- testGrowByWithTwoParams --"); prn("Sent growBy with 3 and 2 (after setting dimensions to 100)"); prn("Actual Width: " + rec.getWidth()); prn("Actual Height: " + rec.getHeight()); }
public void summarize(Object testee, String testTitle, String note) { //testee is the object being tested prn("\nCompleted Test: " + testTitle); prn(note); prn("Tested Object: " + testee); }
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); summarize(rec, "testSetDimensions", "Set dimensions to 8 and 7"); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); summarize(rec, "testGrowByWithOneParam", "Sent growBy with 5 (after setting dims to 100)"); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); summarize(rec, "testGrowByWithTwoParams", "Sent growBy with 3 and 2 (after setting dims to 100)"); }
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); System.out.println("\n-- testSetDimensions --"); System.out.println("Set dimensions to 8 and 7"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); System.out.println("\n-- testGrowByWithOneParam --"); System.out.println("Sent growBy with 5 (after setting dims to 100)"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); System.out.println("\n-- testGrowByWithTwoParams --"); System.out.println("Sent growBy with (3, 2) after setting dims to 100"); System.out.println("Actual Width: " + rec.getWidth()); System.out.println("Actual Height: " + rec.getHeight()); }
public void testSetDimensions() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(8, 7); summarize(rec, "testSetDimensions", "Set dimensions to 8 and 7"); } public void testGrowByWithOneParam() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(5); summarize(rec, "testGrowByWithOneParam", "Sent growBy with 5 (after setting dims to 100)"); } public void testGrowByWithTwoParams() { Rectangle rec; rec = new Rectangle(); rec.setDimensions(100, 100); rec.growBy(3, 2); summarize(rec, "testGrowByWithTwoParams", "Sent growBy with 3 and 2 (after setting dims to 100)"); }
Rectangle rec; rec = new Rectangle();
beforeTest(rec); //test code summarize(rec, "testSetDimensions", "Set dimensions to 8 and 7");