sfwr eng 3s03: software testing

69
SFWR ENG 3S03: Software Testing Dr. R. Khedri Outline Preliminaries Your First Unit Tests Writing Tests in JUnit (Slide 1 of 69) SFWR ENG 3S03: Software Testing Dr. Ridha Khedri Department of Computing and Software, McMaster University Canada L8S 4L7, Hamilton, Ontario Acknowledgments: Material based on [HT03] Dr. R. Khedri SFWR ENG 3S03: Software Testing

Upload: others

Post on 23-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

(Slide 1 of 69)

SFWR ENG 3S03: Software Testing

Dr. Ridha Khedri

Department of Computing and Software, McMaster UniversityCanada L8S 4L7, Hamilton, Ontario

Acknowledgments: Material based on [HT03]

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 2: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

(Slide 2 of 69)

Unit Testing in Java with JUnitåOutline

1 Outline

2 PreliminariesExcuses For Not Testing Units

3 Your First Unit TestsAssertions as a testing techniqueTesting a Simple MethodHow to Run a JUnit Test

4 Writing Tests in JUnitStructuring Unit TestsJUnit AssertsJUnit FrameworkJUnit Test CompositionUsing Data Files

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 3: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Excuses For NotTesting Units

Your First UnitTests

Writing Tests inJUnit

(Slide 3 of 69)

Unit Testing in Java with JUnitåPreliminaries

A unit test is a piece of code written by adeveloper/tester that exercises a very small, specificarea of functionality of the code being tested

Usually a unit test exercises some particular method ina particular context

Add a large value to a sorted list, then confirm thatthis value appears at the end of the list

Delete a pattern of characters from a string and thenconfirm that they are gone

Unit testing techniques are needed for programmersand testers

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 4: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Excuses For NotTesting Units

Your First UnitTests

Writing Tests inJUnit

(Slide 4 of 69)

Unit Testing in Java with JUnitåPreliminaries

Fundamentally, you want to answer the question: “Isthe code fulfilling my intent?”

By building up confidence that the individual unitswork as expected, we can then proceed to assembleand test working systems

We still need other forms of testing, and perhaps muchmore formal testing depending on your environment

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 5: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Excuses For NotTesting Units

Your First UnitTests

Writing Tests inJUnit

(Slide 5 of 69)

Unit Testing in Java with JUnitåPreliminaries

One nice side-effect of unit testing is that it helps youcommunicate the code’s intended use

A unit test behaves as executable documentation,showing how you expect the code to behave under thevarious conditions you have considered

Team members can look at the tests for examples ofhow to use your code

If someone comes across a test case that you haven’tconsidered, they will be alerted quickly to that fact

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 6: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Excuses For NotTesting Units

Your First UnitTests

Writing Tests inJUnit

(Slide 6 of 69)

Unit Testing in Java with JUnitåPreliminaries

Unit testing is basically an easy practice to adopt

However, there are some guidelines and common stepsthat you can follow to make it easier and more effective

1 Decide how to test the method in questionWith at least a rough idea of how to proceed, youproceed to write the test code itself (If you are testingyour own code)

2 You run the test itself, and probably all the other testsin that (part of) the system –if that can be donerelatively quickly–

You want to get into the habit of looking at the testresults and telling at a glance whether it all worked

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 7: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Excuses For NotTesting Units

Your First UnitTests

Writing Tests inJUnit

(Slide 7 of 69)

Unit Testing in Java with JUnitåPreliminaries

åExcuses For Not Testing Units

If I a programmer,

It’s not my job to test my code

I am being paid to write code, not to write tests

It takes too long to run the tests

But it compiles!

I feel guilty about putting testers and QA staff out ofwork

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 8: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Excuses For NotTesting Units

Your First UnitTests

Writing Tests inJUnit

(Slide 8 of 69)

Unit Testing in Java with JUnitåPreliminaries

åExcuses For Not Testing Units

If I am tester,

I don’t really know how the code is supposed to behaveso I cannot test it

It takes too much time to write the tests

It takes too long to run the tests

My company won’t let me run unit tests on the livesystem (NOT THE CASE)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 9: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 9 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

Recall:

a unit test is articulated into a piece of code

it is to exercise another piece of code, and determineswhether the other piece of code is behaving asexpected or not

How do you do that, exactly?

Using Java Libraries: You can use an assertion, asimple method call that verifies that something is true

Write your own assert method

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 10: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 10 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åAssertions as a testing technique

Sometimes, we want to make sure that a programsatisfies a certain assumption at a given point

The code for an assumption defines an assertion

The assert statement allows us to specify an assertionabout the program’s behaviour

The assumption is written as a Boolean expression

The Boolean expression is evaluated during programexecutionIf the expression evaluated to false, then

an error message is generatedthe execution is aborted

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 11: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 11 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åAssertions as a testing technique

Using java -ea FloatingPointArea3

1 // Us ing a s s e r t i o n s to v e r i f y u s e r i n pu t and c a l c u l a t e d v a l u e s .impor t j a v a . u t i l . Scanner ;

3 p u b l i c c l a s s F l o a t i n gPo i n tA r e a3 {p u b l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) {

5 Scanner keyboard = new Scanner ( System . i n ) ;

7 // Read r e c t a n g l e d imens i on sSystem . out . p r i n t ( ” Ente r the r e c t a n g l e l e n g t h [ dec ima l number ] : ” ) ;

9 doub l e l e n g t h = keyboard . nextDoub le ( ) ;keyboard . n e x tL i n e ( ) ;

11 System . out . p r i n t ( ” Ente r the r e c t a n g l e width [ dec ima l number ] : ” ) ;doub l e width = keyboard . nextDoub le ( ) ;

13// Va l i d a t e u s e r i n pu t

15 a s s e r t l e n g t h > 0 .0 : ”The l e n g t h o f the r e c t a n g l e must be >0 .0 ” ; // (1 )

a s s e r t width > 0 .0 : ”The width o f the r e c t a n g l e must be > 0 .0 ” ;// (2 )

17doub l e a r ea = l e n g t h ∗ width ; // C a l c u l a t e a r ea o f the r e c t a n g l e

19// P r i n t the c o r r e c t answer

21 System . out . p r i n t f (”A r e c t a n g l e o f l e n g t h %.2 f cm . and width %.2 f cm . has ” +

23 ” a r ea %.2 f sq . cm.%n” ,l eng th , width , a r ea ) ;

25 }}

Program Output

Enter the rectangle length [decimal number]: -4

Enter the rectangle width [decimal number]: 1.4

Exception in thread "main" java.lang.AssertionError: The length of the rectangle must be > 0.0

at FloatingPointArea3.main(FloatingPointArea3.java:15)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 12: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 12 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åAssertions as a testing technique

Using java FloatingPointArea3

// Us ing a s s e r t i o n s to v e r i f y u s e r i n pu t and c a l c u l a t e d v a l u e s .2 impor t j a v a . u t i l . Scanner ;

p u b l i c c l a s s F l o a t i n gPo i n tA r e a3 {4 p u b l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) {

Scanner keyboard = new Scanner ( System . i n ) ;6

// Read r e c t a n g l e d imens i on s8 System . out . p r i n t ( ” Ente r the r e c t a n g l e l e n g t h [ dec ima l number ] : ” ) ;

doub l e l e n g t h = keyboard . nextDoub le ( ) ;10 keyboard . n e x tL i n e ( ) ;

System . out . p r i n t ( ” Ente r the r e c t a n g l e width [ dec ima l number ] : ” ) ;12 doub l e width = keyboard . nextDoub le ( ) ;

14 // Va l i d a t e u s e r i n pu ta s s e r t l e n g t h > 0 .0 : ”The l e n g t h o f the r e c t a n g l e must be >

0 .0 ” ; // (1 )16 a s s e r t width > 0 .0 : ”The width o f the r e c t a n g l e must be > 0 .0 ” ;

// (2 )

18 doub l e a r ea = l e n g t h ∗ width ; // C a l c u l a t e a r ea o f the r e c t a n g l e

20 // P r i n t the c o r r e c t answerSystem . out . p r i n t f (

22 ”A r e c t a n g l e o f l e n g t h %.2 f cm . and width %.2 f cm . has ” +” a rea %.2 f sq . cm.%n” ,

24 l eng th , width , a r ea ) ;}

26 }

Program Output

Enter the rectangle length [decimal number]: -4

Enter the rectangle width [decimal number]: 1.4

A rectangle of length -4.00 cm. and width 1.40 cm. has area -5.60 sq. cm.

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 13: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 13 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

Assertions provide a useful testing technique that canhelp us detect errors early

Assertions can be turned on when running the programfor test purposes, and turned off when the program isshipped to the user

The assertions can be turned on again by means of the”-ea” flag

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 14: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 14 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

FEST Assertions 2.0 is a Java library that provides afluent interface for writing assertions

Its main goal is to improve test code readability andmake maintenance of tests easier

FEST Assertions requires Java SE 6.0 or later and canbe used with either JUnit or TestNG

Currently, it provides assertions for the dat types:Object, String, Collection, Map, Primitives (boolean,int, char, etc.), Arrays of Object, Arrays of primitives,BufferedImage, Throwable, File, BigDecimal

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 15: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 15 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

You write your own assert function/method

You use an assertion method that verifies thatsomething is true

Example: Write a method assertTrue checks that thegiven boolean condition is true, and fails the currenttest if it is not

It might be implemented like the following

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 16: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 16 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

//2 // a method a s s e r tT r u e

//4 //

// Created by Ridha Khedr i on 2015´03´19.6 //

//8

p u b l i c vo i d a s s e r tT r u e ( boo l ean c o n d i t i o n ) {10 i f ( ! c o n d i t i o n ) {

abo r t ( ) ;12 }}

1i n t a = 2 ;

3 xx xxx xx x xxx x ;x x x xx xxx xxxx x ;

5 a s s e r tT r u e ( a == 2) ;xxxx xx xx xxx xx ;

7 xxxx xx xx xxx xx ;xxxx xx xx xxx xx ;

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 17: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 17 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

//2 // a method a s s e r tT r u e

//4 //

// Created by Ridha Khedr i on 2015´03´19.6 //

//8

p u b l i c vo i d a s s e r tT r u e ( boo l ean c o n d i t i o n ) {10 i f ( ! c o n d i t i o n ) {

abo r t ( ) ;12 }}

14//

16 // we cou ld w r i t e a method tha t t a k e s two i n t e g e r pa ramete r s//

18p u b l i c vo i d a s s e r t E q u a l s ( i n t a , i n t b ) {

20 a s s e r tT r u e ( a == b) ;}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 18: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 18 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

Example

int Largest.largest(int[] list);

Given an array of numbers such as [7, 8, 9], thismethod should return 9

What other tests can you think of?

r7, 8, 9s ÝÑ 9r8, 9, 7s ÝÑ 9r7, 9, 8, 9s ÝÑ 9r1s ÝÑ 1r´9,´8,´7s ÝÑ ´ 7

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 19: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 19 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

1p u b l i c c l a s s L a r g e s t {

3 //// Return the l a r g e s t e l ement i n a l i s t .

5 //// @param l i s t A l i s t o f i n t e g e r s

7 // @re tu rn The l a r g e s t number i n the g i v en l i s t//

9 //// Created by Ridha Khedr i on 2015´03´19.

11 ////

13

15 p u b l i c s t a t i c i n t l a r g e s t ( i n t [ ] l i s t ) {i n t index , max=I n t e g e r .MAX VALUE;

17 f o r ( i nd e x = 0 ; i ndex < l i s t . l eng th ´1; i nd ex++) {i f ( l i s t [ i nd e x ] > max) { max = l i s t [ i n d ex ] ;

19 }}

21 r e t u r n max ;}

23}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 20: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 20 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

2 impor t j u n i t . f ramework .∗ ;p u b l i c c l a s s Te s tLa r g e s t e x t end s TestCase {

4 p u b l i c Te s tLa r g e s t ( S t r i n g name) {supe r ( name) ;

6 }

8p u b l i c vo i d t e s t S imp l e ( ) {

10 a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {7 ,8 ,9}) ) ;}

12 }

Program Output

There was 1 failure:

1) testSimple(TestLargest)junit.framework.AssertionFailedError:

expected:<9> but was:<2147483647>

at TestLargest.testSimple(TestLargest.java:11)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 21: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 21 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

2 p u b l i c c l a s s L a r g e s t {

4 p u b l i c s t a t i c i n t l a r g e s t ( i n t [ ] l i s t ) {i n t index , max = I n t e g e r .MIN VALUE ;

6 f o r ( i nd e x = 0 ; i nd ex < l i s t . l eng th ´1; i nd ex++) {i f ( l i s t [ i nd e x ] > max) { max = l i s t [ i n d ex ] ;

8 }}

10 r e t u r n max ;}

12}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 22: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 22 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

1impor t j u n i t . f ramework .∗ ;

3 p u b l i c c l a s s Te s tLa r g e s t e x t end s TestCase {

5 p u b l i c Te s tLa r g e s t ( S t r i n g name) {supe r ( name) ;

7 }

9 p u b l i c vo i d t e s t S imp l e ( ) {a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {7 ,8 ,9}) ) ;

11 }

13 p u b l i c vo i d t e s tO r d e r ( ) {a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {9 ,8 ,7}) ) ;

15 }}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 23: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 23 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

2 p u b l i c vo i d t e s tO r d e r ( ) {a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {9 ,8 ,7}) ) ;

4 a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {7 ,9 ,8}) ) ;}

6

8 OR MORE TESTS . . . . .

10 p u b l i c vo i d t e s tO r d e r ( ) {a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {9 ,8 ,7}) ) ;

12 a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {7 ,9 ,8}) ) ;a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {7 ,8 ,9}) ) ;

14 }

Program Output

There was 1 failure:

1) testOrder(TestLargest)junit.framework.AssertionFailedError:

expected:<9> but was:<8>

at TestLargest.testOrder(TestLargest.java:10)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 24: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 24 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

2 p u b l i c c l a s s L a r g e s t {

4 p u b l i c s t a t i c i n t l a r g e s t ( i n t [ ] l i s t ) {i n t index , max = I n t e g e r .MIN VALUE ;

6 f o r ( i nd e x = 0 ; i nd ex < l i s t . l eng th ´1; i nd ex++) {i f ( l i s t [ i nd e x ] > max) { max = l i s t [ i n d ex ] ;

8 }}

10 r e t u r n max ;}

12}

1p u b l i c c l a s s L a r g e s t {

3p u b l i c s t a t i c i n t l a r g e s t ( i n t [ ] l i s t ) {

5 i n t index , max = I n t e g e r .MIN VALUE ;f o r ( i nde x = 0 ; i nd ex < l i s t . l e n g t h ; i nd e x++) {

7 i f ( l i s t [ i n d e x ] > max) { max = l i s t [ i n d e x ] ;}

9 }r e t u r n max ;

11 }

13 } Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 25: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 25 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åTesting a Simple Method

1

3impor t j u n i t . f ramework .∗ ;

5 p u b l i c c l a s s Te s tLa r g e s t e x t end s TestCase {p u b l i c Te s tLa r g e s t ( S t r i n g name) {

7 supe r ( name) ;}

9

11 p u b l i c vo i d t e s t S imp l e ( ) {a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {7 ,8 ,9}) ) ;

13 }

15 p u b l i c vo i d te s tDups ( ) {a s s e r t E q u a l s (9 , L a r g e s t . l a r g e s t ( new i n t [ ] {9 ,7 ,9 ,8}) ) ;

17 }

19 p u b l i c vo i d tes tOne ( ) {a s s e r t E q u a l s (1 , L a r g e s t . l a r g e s t ( new i n t [ ] {1}) ) ;

21 }

23 p u b l i c vo i d t e s tN e g a t i v e ( ) {i n t [ ] n e g L i s t = new i n t [ ] {´9, ´8, ´7}; a s s e r t E q u a l s (´7,

L a r g e s t . l a r g e s t ( n e gL i s t ) ) ;25 }

27 p u b l i c vo i d testEmpty ( ) {t r y {

29 La r g e s t . l a r g e s t ( new i n t [ ] {}) ;f a i l ( ” Should have thrown an e x c e p t i o n ” ) ;

31 } ca tch ( Runt imeExcept ion e ) {a s s e r tT r u e ( t r u e ) ;

33 }}

35}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 26: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 26 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åHow to Run a JUnit Test

If JUnit is integrated into your IDE, then running a testmay be as easy as pressing a button and selecting froma list of available test classes

Otherwise, you can always execute a TestRunnermanually

There are several flavours of test runners

To run a GUI version that lets you pick and chooseclasses (and which remembers them from session tosession), run the following class:

java junit.swingui.TestRunner

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 27: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Assertions as a testingtechnique

Testing a SimpleMethod

How to Run a JUnitTest

Writing Tests inJUnit

(Slide 27 of 69)

Unit Testing in Java with JUnitåYour First Unit Tests

åHow to Run a JUnit Test

You will probably be able to run thejunit.swingui.TestRunner class from your IDE

If not, run it from the command line using the jre orjava command (as in previous slide)

To run a test using the textual UI, use:

java junit.textui.TestRunner classname ...

For instance, to run the unit tests discussed in previousslides:

javac Largest.java TestLargest.javajava junit.textui.TestRunner TestLargest

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 28: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 28 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

We have looked at writing tests somewhat informally

Now it is time to take a deeper look at the differencebetween test code and production code

We look at all the various forms of JUnit’s assert, thestructure and composition of JUnit tests, etc.

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 29: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 29 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åStructuring Unit Tests

When writing test code, there are some namingconventions you need to follow

If you have a method named createAccount that youwant to test, then your first test method might benamed testCreateAccount

The method testCreateAccount will call createAccountwith the necessary parameters and verify thatcreateAccount works as specified

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 30: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 30 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åStructuring Unit Tests

You can have many test methods that exercisecreateAccount

Customers or end-users will never see it or use it

The production code must therefore not know anythingabout the test code

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 31: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 31 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åStructuring Unit Tests

�� ��TestAccount.java

testCreateAccount()testCreateAcctDef()testCreateAcctDup()...

Internal Only –Test Code–�� ��Account.java

CreateAccount()

Delivered –Prod. Code–

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 32: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 32 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åStructuring Unit Tests

The test code must be written to do a few things:

Setup all conditions needed for testing (create anyrequired objects, allocate any needed resources, etc.)

Call the method to be tested

Verify that the method to be tested functioned asexpected

Clean up after itself

You write test code and compile it in the normal way(as you would any other bit of source code)

It might happen to use some additional libraries, butotherwise it is just regular code

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 33: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 33 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åStructuring Unit Tests

When it is time to execute the code, remember thatyou never actually run the production code directly

You won’t be running it the way a user would

You run the test code, which in turn exercises theproduction code under very carefully controlledconditions

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 34: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 34 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts

There are (or we can write) some helper methods thatassist us in determining whether a method under test isperforming correctly or not

We call all these methods asserts

They let you assert that some condition is true

We will take a look at each one of the assert methodsthat JUnit provides

All of the following methods will record failures orerrors, and report these through the JUnit classes

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 35: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 35 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts

For the text version, that means an error message willbe printed to the console

The GUI version will show a red bar and supportingdetails to indicate a failure

When a failure or error occurs, execution of the currenttest method is aborted

Other tests within the same test class will still be run

Asserts are the fundamental building block for unittests

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 36: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 36 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertEquals

assertEquals

assertEquals([String message], expected, actual)

This is the most-often used form of assert

“expected” is a value you hope to see (typicallyhard-coded)

“actual” is a value actually produced by the code undertest

message is an optional message that will be reported inthe event of a failure

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 37: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 37 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertEquals

Any kind of object may be tested for equality

In particular, you can compare the contents of stringsusing this method

Different method signatures are also provided for allthe native types (boolean, int, short, etc.) and Object

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 38: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 38 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertEquals

Be aware that the equals method for native arrays,does not compare the contents of the arrays (just thearray reference itself)

If you are using an assert to compare floating pointnumbers (floats or doubles in Java), you need to specifyone additional piece of information, the toleranceThis specifies just how close to ?equals? you need theresult to be

For most business applications, 4 or 5 decimal places isprobably enough

For scientific apps, you may need much greaterprecision

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 39: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 39 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertEquals

assertEquals

assertEquals([String message], expected,actual, tolerance)

Example

assertEquals(”Should be 3 1/3”, 3.33, 10.0/3.0, 0.01);

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 40: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 40 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertNull

assertNull

assertNull([String message], java.lang.Object object)

assertNotNull

assertNotNull([String message], java.lang.Object object)

Asserts that the given object is null (or not null),failing otherwise

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 41: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 41 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertSame

assertSame

assertSame([String message], expected, actual)

assertNotSame

assertNotSame([String message], expected, actual)

The first asserts that expected and actual refer to thesame object, and fails the test if they do not

The second asserts that expected and actual do notrefer to the same object, and fails the test if they arethe same object

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 42: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 42 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à assertTrue

assertTrue

assertTrue([String message], boolean condition)

Asserts that the given boolean condition is true,otherwise the test fails

assertFalse

assertFalse([String message], boolean condition)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 43: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 43 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à fail

fail

fail([String message])

Fails the test immediately, with the optional message

Often used to mark sections of code that should not bereached (for instance, after an exception is expected)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 44: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 44 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Asserts à Using asserts

You usually have multiple asserts in a given testmethod

When an assert fails, that test method will be aborted

å the remaining assertions in that method will not beexecuted this time

à You have to fix the failing test before you can proceed(Programmer)

à And you fix the next failing test. And the next. And soon. (Programmer)

Programmer: Under no circumstances should youcontinue to add features when there are failing tests!

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 45: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 45 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Framework

So far, we have just looked at the assert methods

We need a little bit more of a structure around theasserts (a framework)

Follows is a very simple piece of test code thatillustrates the minimum framework we need to getstarted

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 46: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 46 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Framework

impor t j u n i t . f ramework .∗ ;2

p u b l i c c l a s s Tes tS imp le ex t end sTestCase {

4 p u b l i c Tes tS imp le ( S t r i n g name) {supe r ( name) ;

6 }

8 p u b l i c vo i d tes tAdd ( ) {a s s e r t E q u a l s (2 , 1+1) ;

10 }}

The import statement on line 1brings in the necessary JUnitclasses

On line 3, we have the classdefynition (each class thatcontains tests must extendTestCase as shown)

The base class TestCaseprovides most of theunit-testing functionality thatwe need (including all of theassert methods)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 47: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 47 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Framework

1 impor t j u n i t . f ramework .∗ ;

3 p u b l i c c l a s s Tes tS imp le ex t end sTestCase {

p u b l i c Tes tS imp le ( S t r i n g name) {5 supe r ( name) ;

}7

p u b l i c vo i d tes tAdd ( ) {9 a s s e r t E q u a l s (2 , 1+1) ;

}11 }

The base class requires aconstructor that takes a String

Finally, the test class containsa method named testAdd online 9 (It is the one we wantedto use in the example)

All methods with names thatbegin with “test” will be runautomatically by JUnit

We can also specify particularmethods to run by defining asuite method (To be discussed)

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 48: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 48 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Framework

1 impor t j u n i t . f ramework .∗ ;

3 p u b l i c c l a s s Tes tS imp le ex t end sTestCase {

p u b l i c Tes tS imp le ( S t r i n g name) {5 supe r ( name) ;

}7

p u b l i c vo i d t e s tAdds ( ) {9 a s s e r t E q u a l s (2 , 1+1) ;

a s s e r t E q u a l s (4 , 2+2) ;11 a s s e r t E q u a l s (´8, ´12+4) ;

}13 }

In the previous example, weshowed ONE test, using ONEassert, in ONE test method

Inside a test method, we canplace any number of asserts

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 49: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 49 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition

Summary:

A test class contains test methods

Each method contains one or more assert statements

A test class can also invoke other test classes:individual classes, packages, or even the whole system

We can better structure our testing by creating testsuites

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 50: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 50 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition

Any test class can contain a static method named suite

public static Test suite();

We can provide a suite() method to return anycollection of tests we want

Without a suite() method, JUnit runs all of the“test. . . ” methods automatically

We might also want to add particular tests by hand

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 51: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 51 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition1 impor t j u n i t . f ramework .∗ ;

3 p u b l i c c l a s s TestClassTwo ex t end s TestCase {p u b l i c TestClassTwo ( S t r i n g method ) {

5 supe r (method ) ;}

7// This one t a k e s a few hour s . . .

9p u b l i c vo i d tes tLongRunner ( ) {

11 TSP t sp = new TSP( ) ; // Load wi th d e f a u l t c i t i e sa s s e r t E q u a l s (2300 , t s p . s h o r t e s tPa t h (50) ) ; // top 50

13 }

15 p u b l i c vo i d t e s t S h o r tT e s t ( ) {TSP t sp = new TSP( ) ; // Load wi th d e f a u l t c i t i e s

17 a s s e r t E q u a l s (140 , t s p . s h o r t e s tPa t h (5 ) ) ; // top 5}

19p u b l i c vo i d t e s tAno th e rSho r tTe s t ( ) {

21 TSP t sp = new TSP( ) ; // Load wi th d e f a u l t c i t i e sa s s e r t E q u a l s (586 , t s p . s h o r t e s tPa t h (10) ) ; // top 10

23 }

25 p u b l i c s t a t i c Test s u i t e ( ) {Tes tSu i t e s u i t e = new Te s tSu i t e ( ) ;

27 // Only i n c l u d e s h o r tt e s t s s u i t e . addTest (

29 new TestClassTwo ( ” t e s t S h o r tT e s t ” )) ;

31 s u i t e . addTest (new TestClassTwo ( ” t e s tAno th e rSho r tTe s t ” )

33 ) ;r e t u r n s u i t e ;

35 }}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 52: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 52 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition

Basic test Skeleton: Provides a mechanism to run test

// package . . .2 impor t j u n i t . f ramework .∗ ;

// Change a l l o c c u r r e n c e s4 // as a p p r o p r i a t e

6 p u b l i c c l a s s Te s tSke l e t on o f ” Ske l e t on ” below ex t end s TestCase {/∗∗

8 ∗ Per´method t e s t s e t up∗/

10 p u b l i c vo i d setUp ( ) {}

12/∗∗

14 ∗ Per´method t e s t t e a r down∗/

16 p u b l i c vo i d tearDown ( ) {}

18/∗∗

20 ∗ Add t e s t s he r e :∗ p u b l i c vo i d testName ( ) . . .

22 ∗/p u b l i c Te s tSke l e t on ( S t r i n g name) {

24 supe r ( name) ;}

26/∗∗

28 ∗ De f au l t s u i t e method∗/

30 p u b l i c s t a t i c Test s u i t e ( ) {r e t u r n new Te s tSu i t e ( Te s tSke l e t on . c l a s s ) ;

32 }// THE REST OF THE SKELETON i s on NEXT SLIDE

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 53: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 53 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition

Basic test Skeleton. . . CONTINUED . . .

1 // Test Ske l e t on CONTINUED from p r e v i o u s s l i d e

3 /∗∗ Note ´́ ”main” w i l l o n l y be run when invoked∗ i n d i v i d u a l l y from the command l i n e

5 ∗ ( not v i a A n t s JUn i t Task , e t c . )∗/

7 p u b l i c s t a t i c vo i d main ( S t r i n g [ ] a r g s ) {Tes tSu i t e s u i t e = new Te s tSu i t e ( ) ;

9 i f ( a r g s . l e n g t h != 0) {// Run s p e c i f i c t e s t s as i n d i c a t e d from the

11 // command l i n ef o r ( i n t i =0; i< a r g s . l e n g t h ; i++) {

13 s u i t e . addTest ( new Tes tSke l e t on ( a r g s [ i ] ) ) ;}

15 } e l s e {// Dynamica l l y d i s c o v e r a l l o f them , or use

17 // use r´d e f i n e d s u i t es u i t e . addTest ( Te s tSke l e t on . s u i t e ( ) ) ;

19 }j u n i t . t e x t u i . TestRunner . run ( s u i t e ) ;

21 }}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 54: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 54 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition

The String parameter to the constructor lets aTestCase return a reference to a named test method

We can use it to get references to the twoshort-running tests to populate our test suite

You might want to have a higher-level test that iscomposed of test classes

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 55: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 55 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition

impor t j u n i t . f ramework .∗ ;2

p u b l i c c l a s s Tes tC la s sCompos i t e e x t end sTestCase {

4p u b l i c Tes tC la s sCompos i t e ( S t r i n g method ) {

6 supe r (method ) ;}

8s t a t i c p u b l i c Test s u i t e ( ) {

10 Te s tSu i t e s u i t e = new Te s tSu i t e ( ) ;// Grab e v e r y t h i n g :

12 s u i t e . addTes tSu i t e ( TestC lassOne . c l a s s ) ;// Use the s u i t e method :

14 s u i t e . addTest ( TestClassTwo . s u i t e ( ) ) ;r e t u r n s u i t e ;

16 }}

If you runTestClassComposite, werun the test methods:

testAddition() fromTestClassOnetestSubtraction() fromTestClassOnetestShortTest() fromTestClassTwo

testAnotherShortTest()from TestClassTwo

We can keep going withthis scheme, by writinganother class includeTestClassComposite

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 56: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 56 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à Per-test Setup and Tear-down

Each test should run independently of every other test

This allows you to run any individual test at any time,in any order

For this purpose, we may need to reset some parts ofthe testing environment in between tests, or clean upafter a test has run

JUnit’s TestCase base class provides two methods thatyou can override to set up and then tear down thetest’s environment

protected void setUp(); (Called before each testXXXX )protected void tearDown(); (Called after each testXXXX )

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 57: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 57 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à Per-test Setup and Tear-down

Example of Execution Order of Setup Code

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 58: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 58 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à Per-test Setup and Tear-down

Example: Overriding of setUp and tearDown1 p u b l i c c l a s s TestDB ex t end s TestCase {

p r i v a t e Connect ion dbConn ;3

p r o t e c t e d vo i d setUp ( ) {5 dbConn = new Connect ion ( ” o r a c l e ” , 1521 , ” Fred ” , ” f ooba r ” ) ;

dbConn . connect ( ) ;7 }

9 p r o t e c t e d vo i d tearDown ( ) {dbConn . d i s c o nn e c t ( ) ; dbConn = n u l l ;

11 }

13 p u b l i c vo i d t e s tAccoun tAcce s s ( ) {// Uses dbConn

15 xxx xxx xxxxxx xxx xxxxxxxxx ;xx xxx xxx xxxx x xx xxxx ;

17 }

19 p u b l i c vo i d t e s tEmp loyeeAcce s s ( ) {// Uses dbConn

21 xxx xxx xxxxxx xxx xxxxxxxxx ;xxxx x x xx xxx xx xxxx ;

23 }}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 59: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 59 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à Per-suite Setup and Tear-down

Normally per-test setup is all we need

But in some circumstances, we may need to setsomething up or clean up after the entire test suite hasrun

In this case, we need per-suite setup and tear-down

Per-suite setup is a bit more complicated

Provide a suite of the required testsWrap it in a TestSetup object

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 60: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 60 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à Per-suite Setup and Tear-down

Example: Per-suite Setup and Tear-downimpor t j u n i t . f ramework .∗ ; impor t j u n i t . e x t e n s i o n s .∗ ;

2 p u b l i c c l a s s TestClassTwo ex t end s TestCase {p r i v a t e s t a t i c TSP t sp ;

4p u b l i c TestClassTwo ( S t r i n g method ) {

6 supe r (method ) ;}

8// This one t a k e s a few hour s . . .

10 p u b l i c vo i d tes tLongRunner ( ) {a s s e r t E q u a l s (2300 , t s p . s h o r t e s tPa t h (50) ) ;

12 }

14 p u b l i c vo i d t e s t S h o r tT e s t ( ) {a s s e r t E q u a l s (140 , t s p . s h o r t e s tPa t h (5 ) ) ;

16 }

18 p u b l i c vo i d t e s tAno th e rSho r tTe s t ( ) {a s s e r t E q u a l s (586 , t s p . s h o r t e s tPa t h (10) ) ;

20 }// CONTINUED ON NEXT SLIDE

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 61: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 61 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à Per-suite Setup and Tear-down

// Cont inued from PREVIOUS SLIDE2

p u b l i c s t a t i c Test s u i t e ( ) {4 Te s tSu i t e s u i t e = new Te s tSu i t e ( ) ;

// Only i n c l u d e s h o r t t e s t s6 s u i t e . addTest ( new TestClassTwo ( ” t e s t S h o r tT e s t ” ) ) ;

s u i t e . addTest ( new TestClassTwo ( ” t e s tAno th e rSho r tTe s t ” ) ) ;8 TestSetup wrapper = new TestSetup ( s u i t e ) {

10 p r o t e c t e d vo i d setUp ( ) {oneTimeSetUp ( ) ;

12 }

14 p r o t e c t e d vo i d tearDown ( ) {oneTimeTearDown ( ) ;

16 } ;}

18 r e t u r n wrapper ;}

20p u b l i c s t a t i c vo i d oneTimeSetUp ( ) {

22 // one´t ime i n i t i a l i z a t i o n code goes he r e . . .t s p = new TSP( ) ;

24 t sp . l o a d C i t i e s ( ” Eas t e rnSeaboa rd ” ) ;}

26p u b l i c s t a t i c vo i d oneTimeTearDown ( ) {

28 // one´t ime c l eanup code goes he r e . . .t s p . r e l e a s e C i t i e s ( ) ;

30 }}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 62: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 62 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à JUnit and Exceptions

There are two kinds of exceptions that we might beinterested in:

Expected exceptions resulting from a test

Unexpected exceptions from something that is gonewrong

In a test, we want the method under test to throw anexception

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 63: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 63 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åJUnit Test Composition à JUnit and Exceptions

Consider a method named sortMyList() that is supposed tothrow an exception if passed a null list

1 p u b l i c vo i d t e s t F o rE x c e p t i o n ( ) {t r y {

3 s o r tMyL i s t ( n u l l ) ;f a i l ( ” Should have thrown an e x c e p t i o n ” ) ;

5 } ca tch ( Runt imeExcept ion e ) {a s s e r tT r u e ( t r u e ) ;

7 }}

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 64: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 64 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åUsing Data Files

For sets of tests with large amounts of test data

Consider putting the test values and/or results in aseparate data file that the unit test reads in

It is a very complicated exercise

The next example illustrate this usage of data files

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 65: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 65 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åUsing Data Files

The data file has a very simple format

For example, each line contains a set of numbers

The first number is the expected answer

The numbers on the rest of the line are the argumentswith which to test

We allow a pound-sign (#) for comments, so that youcan put notes in the test file

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 66: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 66 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åUsing Data Files

#

# Simple tests:

#

9 789

9 987

9 989

#

# Negative number tests: #

-7 -7 -8 -9

-7 -8 -7 -8

-7 -9 -7 -8

#

# Mixture:

#

7 -9-7-8764

0 -109-74

#

# Boundary conditions:

#

11

00

2147483647 2147483647 -2147483648 -2147483648

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 67: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 67 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åUsing Data Filesimpor t j u n i t . f ramework .∗ ;

2 impor t j a v a . i o .∗ ;impor t j a v a . u t i l . A r r a y L i s t ;

4 impor t j a v a . u t i l . S t r i n gTok e n i z e r ;

6 p u b l i c c l a s s Te s t L a r g e s tDa t aF i l e e x t end s TestCase {p u b l i c T e s t L a r g e s tDa t aF i l e ( S t r i n g name) {

8 supe r ( name) ;}

10/∗ Run a l l the t e s t s i n t e s t d a t a . t x t ( does not t e s t

12 ∗ e x c e p t i o n ca se ) . We w i l l ge t an e r r o r i f any o f the∗ f i l e I /O goes wrong .

14 ∗/

16 p u b l i c vo i d t e s t F r omF i l e ( ) throws Excep t i on {S t r i n g l i n e ;

18 Bu f f e r edReade r r d r = new Bu f f e r edReade r (new F i l eR e ad e r (

” t e s t d a t a . t x t ” ) ) ;20 wh i l e ( ( l i n e = r d r . r e adL i n e ( ) ) != n u l l ) {

i f ( l i n e . s t a r t sW i t h ( ”#” ) ) { // I g no r e comments22 con t i nu e ;

}24

S t r i n gTok e n i z e r s t = new S t r i n gTok e n i z e r ( l i n e ) ;26 i f ( ! s t . hasMoreTokens ( ) ) {

con t i nu e ; // Blank l i n e28 }

30 // . . . . CONTINUED ON NEXT SLIDE

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 68: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 68 of 69)

Unit Testing in Java with JUnitåWriting Tests in JUnit

åUsing Data Files

// CONTINUED FROM PREVIOUS SLIDE2

4 // Get the expec t ed v a l u eS t r i n g v a l = s t . nextToken ( ) ;

6 i n t expec t ed = I n t e g e r . va lueOf ( v a l ) . i n tVa l u e ( ) ;// And the arguments to La r g e s t

8 A r r a y L i s t a r g ume n t l i s t = new A r r a y L i s t ( ) ;

10 wh i l e ( s t . hasMoreTokens ( ) ) {a r g ume n t l i s t . add ( I n t e g e r . va lueOf ( s t . nextToken ( ) ) ) ;

12 }// T r an s f e r o b j e c t l i s t i n t o n a t i v e a r r a y

14 i n t [ ] arguments = new i n t [ a r g ume n t l i s t . s i z e ( ) ] ;

16 f o r ( i n t i =0; i < a r g ume n t l i s t . s i z e ( ) ; i++) {arguments [ i ] =

( ( I n t e g e r ) a r g ume n t l i s t . ge t ( i ) ) . i n tVa l u e ( ) ;18 }

20 // And run the a s s e r ta s s e r t E q u a l s ( expected , L a r g e s t . l a r g e s t ( arguments ) ) ;

22 }}

24 }

Dr. R. Khedri SFWR ENG 3S03: Software Testing

Page 69: SFWR ENG 3S03: Software Testing

SFWR ENG 3S03:Software Testing

Dr. R. Khedri

Outline

Preliminaries

Your First UnitTests

Writing Tests inJUnit

Structuring Unit Tests

JUnit Asserts

assertEquals

assertNull

assertSame

assertTrue

fail

Using asserts

JUnit Framework

JUnit TestComposition

Per-test Setupand Tear-downPer-suite Setupand Tear-downJUnit andExceptionsUsing Data Files

(Slide 69 of 69)References I

Andy Hunt and Dave Thomas, Pragmatic unit testingin java with junit, The Pragmatic Programmers, LLC,2003.

Dr. R. Khedri SFWR ENG 3S03: Software Testing