Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result....
Transcript of Test Driven Development · Test Bar indicating failure. Itemised tests and their individual result....
![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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/1.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/27.jpg)
![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](https://reader035.fdocuments.in/reader035/viewer/2022071000/5fbc1fc59283e13b2c05bd2b/html5/thumbnails/28.jpg)
Department of Computing and Mathematics http://www.wit.ie/