lecture10.ppt

advertisement
CS110- Lecture 10
Mar 9, 2005

Agenda



Practice Session (classes and loops)
JUnit Testing
Practice Session (testing)
6/30/2016
CS110-Spring 2005, Lecture 10
1
Practice Session (Extra Credit)

Design and implement a class called
Circle that contains instance data that
represents the circle’s radius. Define
the Circle constructor to accept and
initialize the radius, and include getter
and setter methods for the radius.
Include method area that calculates
and return the area and another
method perimeter that calculates and
return the perimeter.
6/30/2016
CS110-Spring 2005, Lecture 10
2
Practice Session (Extra Credit)


Design and implement a class called
Person that contains instance data that
represents the person’s name and age.
Define the Person constructor to
accept and initialize the name and age,
and include getter and setter methods
for the name and age.
Create a driver class called
PersonClient whose main method
instantiates and update several Person
objects.
6/30/2016
CS110-Spring 2005, Lecture 10
3
Practice Session (Extra Credit)


Design and implement a class called
Book that contains instance data that
represents the book’s title, author,
publisher, edition and isbn number.
Define the Book constructor to accept
and initialize the instance data, and
include getter and setter methods for all
instance data.
Create a driver class called BookShelf
whose main method instantiates and
update several Book objects.
6/30/2016
CS110-Spring 2005, Lecture 10
4
Practice Session (Page 277)





5.17: Write a for loop to print the odd numbers from
1 to 99.
5.18: Write a for loop to print the multiples of 3 from
300 down to 3.
5.19: Write a code fragment that reads 10 integer
values from the user and prints the highest value
entered.
5.20: Write a code fragment that determines and
prints the number of times the character ‘a’ appears
in a String object called name.
5.21: Write a code fragment that prints the
characters stored in a String object called str
backward.
6/30/2016
CS110-Spring 2005, Lecture 10
5
Practice Session (Page 277)


5.22: Write a code fragment that prints
every other character in a String object
called word starting from the first
character.
5.23: Write a method called
powersOfTwo that prints the first 10
powers of 2 (starting with 2). The
method takes no parameter and doesn’t
return anything.
6/30/2016
CS110-Spring 2005, Lecture 10
6
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 10
7
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 10
8
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 10
9
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 10
10
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 10
11
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 10
12
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 10
13
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 10
14
Download