test driven development · test bar indicating failure. itemised tests and their individual result....

28
Test Driven Development Produced by: Dr. Siobhán Drohan Mairead Meagher Department of Computing and Mathematics http://www.wit.ie/ Using JUnit to Test DVD.java

Upload: others

Post on 16-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Test Driven Development

Produced

by:

Dr. Siobhán Drohan Mairead Meagher

Department of Computing and Mathematics http://www.wit.ie/

Using JUnit to Test DVD.java

Page 2: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Let’s first write a complete test class and then we will discuss the theory

behind it.

Testing DVD.java

Page 3: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

DVD.java

public class DVD

{

private String title;

public DVD(String title){

if (title.length() <= 20){

this.title = title;

}

else{

this.title = title.substring(0,20);

}

}

public DVD(){

}

public void setTitle(String title){ if (title.length() <= 20){ this.title = title; } } public String getTitle(){ return title; } public String toString(){ return "DVD Title is: " + title; } }

Page 4: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

In Eclipse, you should create a new “test” folder to hold your test classes

Page 5: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Within this “test” folder, create a new JUnit Test Case

Page 6: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

You may be asked to add JUnit 4 to your build path Click OK.

Page 7: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Revised Folder Structure

Page 8: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

The generated DVDTest.java

Page 9: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Running DVDTest.java

Page 10: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

JUnit Results Pane (1)

Page 11: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

JUnit Results Pane (2)

Number of tests run. Also the number that have errors (e.g. compile) and the number that failed.

Test Bar indicating failure.

Itemised tests and their individual result.

When a failed test is selected in the above pane, the reason for failure appears here. Can be double clicked on for more information.

Page 12: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing getTitle() public String getTitle() { return title; }

Page 13: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing Constructors (1)

Page 14: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing Constructors (2)

Page 15: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing Constructors (3)

Our tests are successful, BUT have we tested every possible path in the constructors?

Page 16: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing Constructors (4)

What do you think of this approach? Better? Will this test be successful?

Page 17: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing Constructors (5)

Can you see what went

wrong?

Page 18: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing Constructors (6)

Fixed!

Fixed!

Page 19: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing setTitle method (1)

Page 20: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing setTitle method (2)

Page 21: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing toString method (1)

Page 22: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Does this test look right? Will it run successfully?

Page 23: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing toString method (3)

Page 24: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

We are referencing the wrong dvd objects in the

last two method calls.

Page 25: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Testing toString method (5)

We are now referencing the

correct dvd objects, so our test is now

successful.

Page 26: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

We have now successfully automated the testing of the entire DVD class public class DVD

{

private String title;

public DVD(String title){

if (title.length() <= 20){

this.title = title;

}

else{

this.title = title.substring(0,20);

}

}

public DVD(){

}

public void setTitle(String title){ if (title.length() <= 20){ this.title = title; } } public String getTitle(){ return title; } public String toString(){ return "DVD Title is: " + title; } }

Page 27: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears
Page 28: Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result. When a failed test is selected in the above pane, the reason for failure appears

Department of Computing and Mathematics http://www.wit.ie/