CS110- Lecture 11 Mar 21, 2005 Agenda Questions on Project 2? JUnit Testing Practice Session toString() method 6/30/2016 CS110-Spring 2005, Lecture 11 1 Testing Unit Testing: To test each unit, or component, independently, before the units are integrated into the whole system. Integration and system testing: To integrate all the components of a system and to test the system as a whole. 6/30/2016 CS110-Spring 2005, Lecture 11 2 Unit Testing Unit testing is important because For the whole system to function properly, each of its components, or units, must function properly individually. Units are much smaller than the whole system, it is much easier to find the errors in each unit individually. Good unit testing will reduce the effort and cost required for integration significantly. 6/30/2016 CS110-Spring 2005, Lecture 11 3 Unit Testing In OOP the unit for unit testing is often a single class. Old style of testing: Carry out tests in main method. New style of testing: Write separate test classes for testing purposes only. 6/30/2016 CS110-Spring 2005, Lecture 11 4 JUnit Testing When dealing with large set of test cases it is usually more convenient to use a unit testing tool. JUnit is a flexible, easy to use, and open source unit testing tool for java programs. 6/30/2016 CS110-Spring 2005, Lecture 11 5 Template of a typical JUnit test program public class TestX extends TestCase // X is the class // to be tested { private X x = null; protected void setUp() throws Exception { x = new X(); setUp is called before every } testCase is executed protected void tearDown() throws Exception { x = null; tearDown is called after every } testCase is executed //All test methods are public/protected not private public void testCase1() { //test and compare results Name of test methods } starts with test public void testCase2() { //test and compare results } } 6/30/2016 CS110-Spring 2005, Lecture 11 6 Test class for Die.java public class TestDie extends TestCase //Die is the class //to be tested { private Die die = null; protected void setUp() throws Exception { die = new Die(); } protected void tearDown() throws Exception { die = null; } public void testGetFaceValue() { //test and compare results int expectedReturn = 1; int actualReturn = die.getFaceValue(); assertEquals(expectedReturn,actualReturn); } } 6/30/2016 CS110-Spring 2005, Lecture 11 7 JUnit features JUnit features include: Assertions for testing expected results Test fixtures (setUp and tearDown) for sharing common test data. Graphical and textual test runners 6/30/2016 CS110-Spring 2005, Lecture 11 8 Practice Session Point x : double y : double Class Name Data getX() : double Methods getY(): double setX (double x1) : void setY (double y1) : void 6/30/2016 CS110-Spring 2005, Lecture 11 9 Practice Session Write a Point class from the UML diagram in the previous slide. Write a test class TestPoint for Point class. 6/30/2016 CS110-Spring 2005, Lecture 11 10 toString() method toString() method takes no arguments. It is supposed to return all the data in an object coded into a String. Return type is always String. 6/30/2016 CS110-Spring 2005, Lecture 11 11 toString() method for Point Class public String toString() { return (“X = ” + x + “, Y = ” + y); } 6/30/2016 CS110-Spring 2005, Lecture 11 12 How toString() method is used public class PointClient { //Execution always starts from main public static void main(String[] args) { Point p = new Point(3,4); System.out.println(p.toString()); p.setX(6); System.out.println(p.toString()); } } 6/30/2016 CS110-Spring 2005, Lecture 11 13 Output X = 3, Y = 4 X = 6, Y = 4 6/30/2016 CS110-Spring 2005, Lecture 11 14 Special property of toString() If you do not include it in an invocation of System.out.println, it is invoked automatically. For example the bold part of the main method on slide 13 is equivalent to Point p = new Point(3,4); System.out.println(p); p.setX(6); System.out.println(p); 6/30/2016 CS110-Spring 2005, Lecture 11 15 toString() method It is always a good idea to define a suitable toString method for the classes with instance data. 6/30/2016 CS110-Spring 2005, Lecture 11 16