Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Lab

...

06

...

-

...

Simple

...

Java

...

Classes

...

and

...

JUnit

...

Testing

1.

...

Writing

...

Simple

...

Java

...

Classes

...

(45

...

min

...

)

...

First

...

steps:

...

  1. If

...

  1. you

...

  1. have

...

  1. not

...

  1. done

...

  1. it

...

  1. already,

...

  1. download

...

  1. the

...

  1. latest

...

  1. DrJava

...

  1. to

...

  1. your

...

  1. Desktop.

...

  1. [Use

...

  1. this

...

  1. link!|http://drjava.org.}

...

  1. Create

...

  1. a

...

  1. lab07

...

  1. directory

...

  1. within

...

  1. your

...

  1. Comp211

...

  1. directory.

...

  1. Set

...

  1. your

...

  1. DrJava

...

  1. "Language

...

  1. Level"

...

  1. to

...

  1. "Elementary"

...

  1. Create

...

  1. a

...

  1. class

...

  1. to

...

  1. calculate

...

  1. the areas of rectangles in DrJava by following the steps below. We will begin the process of writing a class that represents a rectangle given its width and height. We will write the class in several small steps. At each step, we will compile the code to ensure that everything is syntactically correct. By compiling the code at each small step, we hope to avoid seeing a large number of error messages that can be rather intimidating.
  2. * In the Definitions pane (upper right pane), type the following:
    Code Block
    
    class Rectangle {
      double width;
      double height;
    }
    

...

  1. Note

...

  1. the

...

  1. placement

...

  1. of

...

  1. the

...

  1. curly

...

  1. braces:

...

  1. the

...

  1. opening

...

  1. brace

...

  1. is

...

  1. on

...

  1. the

...

  1. same

...

  1. line

...

  1. as

...

  1. the

...

  1. class

...

  1. name,

...

  1. while

...

  1. the

...

  1. closing

...

  1. brace

...

  1. lines

...

  1. up

...

  1. with

...

  1. the

...

  1. beginning

...

  1. of

...

  1. the

...

  1. class

...

  1. definition

...

  1. on

...

  1. a

...

  1. new

...

  1. line.

...

  1. This

...

  1. is

...

  1. the

...

  1. de-facto

...

  1. Java

...

  1. coding

...

  1. style.

...

  1. This

...

  1. class

...

  1. is

...

  1. not

...

  1. very

...

  1. interesting

...

  1. because

...

  1. we

...

  1. cannot

...

  1. do

...

  1. much

...

  1. with

...

  1. it.

...

  1. However,

...

  1. it

...

  1. is

...

  1. syntactically

...

  1. correct

...

  1. and

...

  1. can

...


  1. be

...

  1. compiled.

...

  1. *

...

  1. Save

...

  1. the

...

  1. file

...

  1. in

...

  1. your

...

  1. Comp211

...

  1. /lab07

...

  1. }}folder

...

  1. under

...

  1. the

...

  1. default

...

  1. name

...

  1. {{Rectangle

...

  1. .

...

  1. he

...

  1. file

...

  1. will

...

  1. be

...

  1. saved

...

  1. as

...

  1. a

...

  1. .dj0

...

  1. file,

...

  1. which

...

  1. is

...

  1. a

...

  1. DrJava

...

  1. Elementary

...

  1. level

...

  1. file.

...

  1. *

...

  1. Click

...

  1. the

...

  1. "Compile

...

  1. All"

...

  1. button

...

  1. in

...

  1. DrJava.

...

  1. The

...

  1. file

...

  1. should

...

  1. compile

...

  1. with

...

  1. no

...

  1. errors.

...

  1. *

...

  1. In

...

  1. the

...

  1. Interactions

...

  1. pane

...

  1. (bottom

...

  1. pane),

...

  1. type

...

  1. the

...

  1. following:

...

  1. Code Block

...

  1. 
    Rectangle r = new Rectangle(5, 10);
    

...

  1. This

...

  1. code

...

  1. defines

...

  1. a

...

  1. variable

...

  1. called

...

  1. r

...

  1. of

...

  1. type Rectangle and binds it to a new object belonging to class
    Rectangle.
  2. * There isn't much we can do with the Rectangel class yet, but we can print the string representations of Rectangle objets. In the Interactions pane, type r and hit Enter. This action will print the string representation of object bound to calc. In the DrJava Elementary language level, the string representation of the Rectangle object r which "Rectanlge(5.,10.)"

...

  1. .

...

  1. Conventional

...

  1. Java

...

  1. would

...

  1. return

...

  1. something

...

  1. much

...

  1. more

...

  1. cryptic.

...

  1. *

...

  1. Take

...

  1. a

...

  1. look

...

  1. at

...

  1. your

...

  1. Comp211/lab07

...

  1. directory

...

  1. and

...

  1. you

...

  1. will

...

  1. see

...

  1. several

...

  1. new

...

  1. files.

...

  1. The

...

  1. compiler

...

  1. automatically

...

  1. created

...

  1. a

...

  1. file

...

  1. called

...

  1. Rectangle.class

...

  1. containing

...

  1. the

...

  1. compiled

...

  1. code

...

  1. expressed

...

  1. as

...

  1. "Java

...

  1. bytecode"

...

  1. ready

...

  1. execution

...

  1. on

...

  1. the

...

  1. Java

...

  1. Virtual

...

  1. Machine

...

  1. (JVM).

...

  1. DrJava

...

  1. automatically

...

  1. generated

...

  1. the

...

  1. file

...

  1. Rectangle.java

...

  1. which

...

  1. we

...

  1. will

...

  1. ignore

...

  1. for

...

  1. now.

...

  1. Now we

...

  1. are

...

  1. ready

...

  1. to

...

  1. add

...

  1. to

...

  1. Rectangle

...

  1. a

...

  1. method

...

  1. to

...

  1. compute

...

  1. the

...

  1. area

...

  1. of

...

  1. a

...

  1. rectangle.

...

  1. Change

...

  1. the

...

  1. defintion

...

  1. of

...

  1. the

...

  1. Rectangle

...

  1. class

...

  1. to

...

  1. read:

...

  1. Code Block

...

  1. 
    class Rectangle { 
      double width;
      double height;
      double area() { return width * height; }  // This line is the only change
    }
    

...

  • Compile All:

...

  • the

...

  • Rectangle

...

  • file

...

  • is

...

  • automatically

...

  • saved.

...

  • To

...

  • create

...

  • a

...

  • new

...

  • instance

...

  • of

...

  • class

...

  • Rectangle

...

  • ,

...

  • type

...

  • the

...

  • following

...

  • in

...

  • the

...

  • Interactions

...

  • pane:

...

  • Code Block

...

  • 
    Rectangle r2 = new Rectangle(4,5);
    

...

  • To "ask"

...

  • the

...

  • Rectangle

...

  • object

...

  • bound

...

  • to

...

  • r2

...

  • to

...

  • compute

...

  • its

...

  • area,

...

  • type

...

  • Code Block

...

  • 
    r2.area();
    

...

  • in

...

  • the

...

  • Interactions

...

  • pane.

...

  • Experiment

...

  • with

...

  • using

...

  • the

...

  • object

...

  • calc

...

  • to

...

  • compute

...

  • the

...

  • area

...

  • of

...

  • a

...

  • few

...

  • rectangles

...

  • of

...

  • different

...

  • sizes.

...

  • How

...

  • do

...

  • you

...

  • instantiate

...

  • another

...

  • AreaCalc

...

  • and

...

  • bind

...

  • it

...

  • to

...

  • the

...

  • name

...

  • robot

...

  • ?

...

Computing

...

the

...

area

...

of

...

a

...

right

...

triangle

...

Suppose

...

we

...

want

...

to

...

define

...

a

...

class

...

representing

...

a

...

right

...

triangle

...

given

...

the

...

length

...

of

...

the

...

two

...

sides

...

forming

...

the

...

right

...

answer,

...

which

...

we

...

call

...

base

...

and

...

height

...

.

...

What

...

code

...

should

...

we

...

write?

...

Write

...

another

...

class

...

called

...

RightTriangle

...

with

...

fields

...

of

...

type

...

double

...

called

...

base

...

and

...

height

...

,

...

and

...

a

...

method

{
Code Block
}
double area()
{code}

to

...

compute

...

the

...

area

...

of

...

this

...

right

...

triangle

...

.

...

  • Now

...

  • suppose

...

  • we

...

  • are

...

  • asked

...

  • to

...

  • define

...

  • a

...

  • class

...

  • that

...

  • can

...

  • compute

...

  • the

...

  • area

...

  • of

...

  • a

...

  • circle.

...

  • How

...

  • would

...

  • you

...

  • do

...

  • it?

...


  • Write

...

  • a

...

  • class

...

  • called

...

  • Circle

...

  • that

...

  • has

...

  • one

...

  • double

...

  • field,

...

  • radius,

...

  • and

...

  • a

...

  • method

...

  • to

...

  • compute

...

  • the

...

  • area.

...

2.

...

Unit

...

Testing

...

with

...

JUnit

...

(35

...

min

...

)

...

Extreme

...

Programming

...

(XP

...

)

...

is

...

a

...

software

...

development

...

methodology

...

that

...

is

...

"very

...

hot"

...

these

...

days

...

and

...

is

...

currently

...

embraced

...

by

...

the

...

software

...

industry.

...

One

...

key

...

element

...

of

...

XP,

...

called

...

"unit

...

testing",

...

is

...

the

...

idea

...

of

...

writing

...

test

...

code

...

for

...

functions

...

(or

...

procedures

...

or

...

object

...

methods

...

)

...

before

...

the

...

functions

...

(or

...

procedures

...

or

...

object

...

methods

...

)

...

are

...

even

...

written.

...

(Don't

...

ask

...

me

...

how

...

to

...

write

...

test

...

code

...

for

...

the

...

test

...

code

...

themselves!

...

The

...

whole

...

thing

...

is

...

"kinda"

...

suspiciously

...

recursive

...

with

...

no

...

well-defined

...

base

...

cases.

...

)

...

The

...

test

...

code

...

becomes

...

in

...

effect

...

the

...

"documentation

...

and

...

specification"

...

of

...

the

...

behavior

...

of

...

the

...

functions

...

(or

...

procedures

...

or

...

object

...

methods

...

)

...

in

...

code

...

form!

...

Each

...

time

...

a

...

function

...

(or

...

procedure

...

or

...

object

...

method

...

)

...

is

...

modified,

...

it

...

must

...

pass

...

its

...

existing

...

test

...

code.

...

Each

...

time

...

a

...

test

...

code

...

for

...

a

...

function

...

(or

...

procedure

...

or

...

object

...

method

...

)

...

is

...

modified,

...

the

...

corresponding

...

function

...

(or

...

procedure

...

or

...

object

...

method

...

)

...

may

...

have

...

to

...

be

...

revised

...

to

...

satisfy

...

the

...

new

...

specification.

...

JUnit

...

(http:-www.junit.org

...

) (www.junit.org

...

)

...

is

...

an

...

open

...

source

...

framework

...

developed

...

to

...

support

...

the

...

above

...

concept

...

of

...

unit

...

testing

...

for

...

Java

...

programs.

...

This

...

tutorial

...

will

...

lead

...

you

...

through

...

a

...

simple

...

example

...

of

...

how

...

to

...

write

...

unit

...

test

...

code

...

in

...

developing

...

a

...

(simple

...

)

...

Java

...

program

...

with

...

only

...

one

...

class.

...

Step

...

0.

...

Run

...

DrJava

...

with

...

Elementary

...

Language

...

level.

...

We

...

will

...

do

...

all

...

development

...

in

...

DrJava

...

since

...

it

...

has

...

very

...

nicely

...

integrated

...

JUnit

...

with

...

its

...

development

...

environment.

...

Step

...

1.

...

Suppose

...

we

...

want

...

to

...

model

...

the

...

notion

...

of

...

a

...

smart

...

person

...

who

...

knows

...

his/her

...

birthday

...

and

...

can

...

compute

...

the

...

number

...

of

...

months

...

till

...

his/her

...

next

...

birthday

...

given

...

the

...

current

...

month.

...

For

...

our

...

purpose,

...

a

...

month

...

can

...

simply

...

be

...

represented

...

by

...

an

...

integer,

...

which

...

in

...

Java

...

is

...

called

...

int.

...

We

...

start

...

by

...

writing

...

"stub"

...

code

...

for

...

the

...

class

...

Person

...

that

...

is

...

to

...

represent

...

our

...

smart

...

person.

{
Code Block
}   
class Person {

    /**
    * Computes the number of months till the next birthday given the current month.
    */
    int nMonthTillBD(int currentMonth) {
        // to do
    }

}
{code}

Notice

...

in

...

the

...

above

...

there

...

is

...

really

...

no

...

concrete

...

code.

...

As

...

a

...

matter

...

of

...

fact,

...

the

...

above

...

would

...

not

...

compile.

...

Now

...

we

...

must

...

abandon

...

everything

...

and

...

start

...

writing

...

test

...

code

...

for

...

nMonthTillBD

...

(...

...

).

...

Step

...

2.

...

DrJava

...

is

...

very

...

nice

...

to

...

you

...

and

...

will

...

create

...

a

...

test

...

stub

...

class

...

for

...

you

...

if

...

you

...

know

...

what

...

to

...

click:

...

  • Go

...

  • to

...

  • the

...

  • File

...

  • menu

...

  • and

...

  • select

...

  • New

...

  • Junit

...

  • Test

...

  • Case...

...

  • Create

...

  • a

...

  • stub

...

  • test

...

  • case

...

  • called

...

  • Test_Person.

...

  • DrJava

...

  • will

...

  • create

...

  • a

...

  • stub

...

  • class

...

  • that

...

  • looks

...

  • like

...

  • the

...

  • following

...

  • Code Block

...

  • 
    /**
     * A JUnit test case class.
     * Every method starting with the word "test" will be called when running
     * the test with JUnit.
     */
    class Test_Person extends TestCase {
        /**
         * A test method.
         * (Replace "X" with a name describing the test.  You may write as
         * many "testSomething" methods in this class as you wish, and each
         * one will be called when running JUnit over this class.)
         */
        void testX() {
        }
    }
    

...

  • At

...

  • this

...

  • point,

...

  • do

...

  • not

...

  • worry

...

  • about

...

  • what

...

  • "extends

...

  • TestCase"

...

  • means

...

  • in

...

  • the

...

  • above.

...

  • DrJava

...

  • is

...

  • re-using

...

  • the

...

  • class

...

  • TestCase

...

  • from

...

  • the

...

  • JUnit

...

  • framework

...

  • in

...

  • some

...

  • clever

...

  • way

...

  • without

...

  • you

...

  • having

...

  • to

...

  • deal

...

  • with

...

  • all

...

  • the

...

  • details

...

  • of

...

  • how

...

  • to

...

  • use

...

  • an

...

  • existing

...

  • class

...

  • from

...

  • some

...

  • other

...

  • files.

...

  • Compile

...

  • this

...

  • test

...

  • class

...

  • only

...

  • (by

...

  • clicking

...

  • on

...

  • Tools/Compile

...

  • current

...

  • document

...

  • ),

...

  • and

...

  • save

...

  • the

...

  • class

...

  • in

...

  • the

...

  • same

...

  • directory

...

  • as

...

  • Person.

...

  • The

...

  • Test_Person

...

  • class

...

  • should

...

  • compile,

...

  • though

...

  • the

...

  • Person

...

  • class

...

  • does

...

  • not.

...

  • Change

...

  • the

...

  • name

...

  • of

...

  • the

...

  • stub

...

  • method

...

  • testX

...

  • (

...

  • )

...

  • in

...

  • the

...

  • above

...

  • to

...

  • test_nMonthTillBD

...

  • (

...

  • )and

...

  • add

...

  • code

...

  • to

...

  • make

...

  • it

...

  • look

...

  • like

...

  • the

...

  • code

...

  • below.

...

  • Code Block

...

  • 
    class Test_Person extends TestCase {
    
        void test_nMonthTillBD() {
    
            Person peter = new Person(9); // a person born in September.
    
            assertEquals("Calling nMonthTillBD(2).", 7, peter.nMonthTillBD(2));
            assertEquals("Calling nMonthTillBD(9).", 0, peter.nMonthTillBD(9));
            assertEquals("Calling nMonthTillBD(12).", 9, peter.nMonthTillBD(12));
        }
    } 
    

...

  • Note

...

  • that

...

  • in

...

  • the

...

  • code

...

  • for

...

  • test_nMonthTillBD

...

  • (

...

  • ),

...

  • we

...

  • have

...

  • decided

...

  • that

...

  • we

...

  • only

...

  • need

...

  • to

...

  • know

...

  • the

...

  • birth

...

  • month

...

  • of

...

  • a

...

  • person

...

  • in

...

  • order

...

  • to

...

  • compute

...

  • the

...

  • number

...

  • of

...

  • months

...

  • till

...

  • the

...

  • next

...

  • birth

...

  • day.

...

  • As

...

  • a

...

  • result,

...

  • we

...

  • instantiate

...

  • a

...

  • Person

...

  • object

...

  • by

...

  • passing

...

  • only

...

  • the

...

  • birth

...

  • month:

...

  • new

...

  • Person

...

  • (9

...

  • ).

...

Also

...

the

...

test

...

covers

...

three

...

cases:

...

  • the

...

  • current

...

  • month

...

  • is

...

  • less

...

  • than

...

  • the

...

  • birth

...

  • month

...

  • the

...

  • current

...

  • month

...

  • is

...

  • equal

...

  • to

...

  • the

...

  • birth

...

  • month

...

  • the

...

  • current

...

  • month

...

  • is

...

  • greater

...

  • than

...

  • the

...

  • birth

...

  • month.

...

The

{
Code Block
assertEquals

method comes from the class TestCase and takes in three parameters:

  • the first parameter is a String
  • the second parameter is the expected result
  • the third parameter is the actual result of the computation you are testing.

Most of the time, your test code will call on the assertEquals method to test for equality between the result of the computation you are testing and the expected result.

When you compile the above code in DrJava, it won't compile. You will have to go in and fix the code for Person to make the test code compile.

Step 3.

Fix the code for Person until Test_Person compiles! (See the code below.)

First add a int field called _bMonth and compile. What happens?

Now add the statement return 0; to the body of the method nMonthTillBD(...) and compile.

Code Block
}assertEquals{code} method comes from the class TestCase and takes in three parameters:
* the first parameter is a String
* the second parameter is the expected result
* the third parameter is the actual result of the computation you are testing. 

Most of the time, your test code will call on the assertEquals method to test for equality between the result of the computation you are testing and the expected result.

When you compile the above code in DrJava, it won't compile.  You will have to go in and fix the code for Person to make the test code compile.

*Step 3.*

Fix the code for Person  until Test_Person compiles!  \(See the code below.\)

First add a int field called _bMonth and compile.  What happens?

Now add the statement return 0; to the body of the method nMonthTillBD\(...\) and compile.
{code}
class Person {
    int _bMonth;

    /**
    * Computes the number of months till the next birthday.
    */
    int nMonthTillBD(int currentMonth) {
        return 0; // to do
    }
}
{code}

*Step 

Step 4.

...

After

...

you

...

have

...

cleaned

...

up

...

your

...

code

...

for

...

Person

...

as

...

shown

...

in

...

the

...

above,

...

you

...

should

...

be

...

able

...

to

...

compile

...

Test_Person.

...

With

...

Test_Person

...

open,

...

click

...

on

...

the

...

Test

...

button

...

in

...

DrJava

...

tool

...

bar.

...

What

...

do

...

you

...

see?

...

Something

...

has

...

failed!

...

The

...

formula

...

for

...

the

...

number

...

of

...

months

...

till

...

the

...

next

...

birth

...

day

...

seems

...

to

...

be

...

the

...

culprit.

...

We

...

will

...

pretend

...

ignorance

...

and

...

fix

...

the

...

"bug"

...

in

...

two

...

steps.

...

Step

...

5.1

...

Change

...

the

...

formula

...

to

{
Code Block
}
    int nMonthTillBD(int currentMonth) {
        return _bMonth - currentMonth; // to do
    }
{code}

Compile

...

all

...

and

...

test

...

again.

...

Still

...

we

...

have

...

errors.

...

Step

...

5.2

...

Change

...

the

...

formula

...

to

{
Code Block
}
    int nMonthTillBD(int currentMonth) {
        return (_bMonth - currentMonth + 12) % 12; // to do
    }
{code}

Compile

...

all

...

and

...

test

...

again.

...

Now

...

everything

...

should

...

pass!

...

You

...

may

...

now

...

remove

...

the

...

TO

...

DO

...

comment

...

from

...

the

...

code

...

of

...

nMonthTillBD.

...

In

...

XP

...

programming,

...

only

...

after

...

a

...

method

...

has

...

passed

...

its

...

unit

...

test

...

that

...

you

...

are

...

allowed

...

to

...

proceed

...

to

...

another

...

one.

...

3.

...

Additional

...

Reading:

...

Lecture

...

notes

...

on

...

Object-Oriented

...

Programming

...

using

...

Java

...

by

...

Dung

...

X.

...

Nguyen

...

and

...

Stephen

...

B.

...

Wong:

...

http://cnx.org/content/col10213/latest

...

Image Added
Here

...

is

...

another

...

link

...

for

...

a

...

more

...

detailed

...

discussion

...

of

...

JUnit

...

testing

...

uisng

...

DrJava:

...

http://cnx.rice.edu/content/m11707/latest/

...

Image Added
The

...

following

...

link

...

contains

...

a

...

more

...

elaborate

...

discussion

...

on

...

JUnit

...

testting

...

and

...

a

...

more

...

involved

...

example

...

of

...

testing:

...

http://junit.sourceforge.net/doc/testinfected/testing.htm

...

Image Added.

...

Access Permissions: (Please don't

...

edit

...

)

...

  • Set

...

  • ALLOWTOPICCHANGE

...

  • =

...

  • Main.TeachersComp211Group

...