Unit Testing Part 4: JUnit Testing - Introduction to Object-oriented Programming

ninetimesdissemblingΛογισμικό & κατασκευή λογ/κού

10 Νοε 2012 (πριν από 4 χρόνια και 9 μήνες)

182 εμφανίσεις

Unit Testing


Part 4: JUnit Testing

CSIS 3701: Advanced Object Oriented Programming

Automated Testing


Goal:
Automate

comprehensive testing


Run
all

test cases (regression testing)


Notify developer of incorrect results



JUnit


Basic tool for automating testing in Java


Built into NetBeans

JUnit Background


assert

package (from C)


http://www.junit.org/apidocs/junit/framework/Assert.html




fail(message)
:


Shuts down program and displays message to standard
output


assertEquals(message, value1, value2)
:


Causes
fail(message)

if the values not equivalent


Creating Tests


To create JUnit tests for a class:


Right
-
click class


Tools


Create JUnit tests

Running JUnit Tests


Right
-
click project


Test


Will display which methods passed/failed

JUnit Methods


Automatically creates one “testing” method for each method


Basic structure:


Create a “test” object


Define a desired result


Run method to get

actual result


Compare using

assertEquals



Modifying JUnit Methods


Will need to
modify

for testing


Set up desired

object state for

test


Define expected

result



Remove “fail”

condition

Adding JUnit Testing Methods


Usually have
multiple

tests for a method


Create one method for each


Add message at beginning to describe what is being
tested

Testing Modifier Methods


Use modifier to

change state



Use
inspector


(such as
toString
)

to get actual state



Add
assertEquals


to compare to

desired state

Testing Modifier Methods


Can also test

validation

on

illegal values



Note that expected

state
should not

change



Complex Testing Methods


Can run
many

tests as part of same method


Example: Testing

isFull

in

NameList



Loop to fill list,

with test each

iteration for

not

full



Final test

when

list
full

Testing Exceptions


Set up object for
exception



Put call inside
try



Call
fail

if do
not

exit the
try

block



catch

block does
nothing (test
succeeds)