Upload
phebe-rodgers
View
261
Download
0
Embed Size (px)
Citation preview
JUnit
Eclipse, Java and introduction to Junit
Topics Covered Using Eclipse IDE Example Java Programs Junit Introduction
What is Eclipse?Eclipse is an open source community whose projects are focused on providing an extensible development platform and application frameworks for building software.
Download eclipse fromhttp://www.eclipse.org/downloads/index.php
Article on using junit with Eclipsehttp://www.onjava.com/pub/a/onjava/2004/02/04/juie.html
JavaJava Tutorials http://java.sun.com/docs/books/
tutorial/getStarted/index.html
Thinking in Java 3rd Edition by Bruce Eckel
http://www.mindview.net/Books/TIJ/Or get a printed version
A simple java Program
//A Very Simple Example - Hello.java
public class Hello {
public static void main(String[] args) { System.out.println(“Hello world");
}
}
Example Compilation
#bash# javac Hello.java
#bash# java Hello
Hello world
Compiling the Eclipse Way
Step 1: Create a New Project. Click on File -> New Project Select Java Project Enter the name for the project
Step 2: Create a Class File Click on File -> New Class Enter the name for the Class (Hello)
Compiling the Eclipse Way (Contd.)
Step 3: Copy or create the java program (Hello.java) and Save.
Step 4: Run the application Select Run -> Run and click on Java
Application. Click on the New button and then Click
on Run.
Eclipse Output
What is Unit Test? A definition from IEEE
– Testing of individual hardware or software or group of related units
Usually, a unit test is code that tests a "unit": the unit could be a class, a component, a module, a function
Unit Test usually built and run at the development cycle.
Unit test is not functional, application testing Unit tests are not interactive
We need a framework A unit testing framework provides
A mechanism to organize and group multiple tests
A simple way to invoke tests Clear indication of which tests passed/failed A standard way to write tests and specify
expected results.
What is JUnit JUnit is a Unit Testing Tool for Java JUnit is a regression testing framework Programmed by Erich Gamma and Kent Be
ck Open Source
– Hosted on SourceForge– http://sourceforge.net/index.php – http://junit.sourceforge.net/ – http://www.junit.org/
JUnit’s Features Assertions for testing expected results Testing fixtures for sharing common
test data Test suites for easily organizing and
running tests Graphical and textual test runners
JUnit Framework
http://junit.sourceforge.net/javadoc/index.html
run()
<< interface >>Test
fName
setUp()runTest()tearDown()run()
TestCase
run()addTest()
TestSuite
*
TestResult
junit.framework
assertTrue()assertEquals()...
Assert
junit.textui.TestRunner junit.swingui.TestRunner
junit
How to Test with JUnit?
Student
TestCase
exercise1..*
TestRunner
run1..*TestStudent
test1
test2
…
Another Java Examplepublic class Student{
private String sname;private String sid;private int score;public Student(String num, String nam, int scr){
sid = num;sname = nam;score = scr;
}
Another Example(cont.)public String getNumber(){
return sid;}public String getName(){
return sname;}public int getScore(){
return score;}
Another Example(cont.)public void setNumber(String num){
sid = num;}public void setName(String name){
sname=name;}public void setScore(int scr){
score=scr;}} //End Class Student
Testing the Student Classpublic class TestStudent {
public static void main(String[] args) { Student student=new Student(“043”, “Justin”,100);
if (!(student.getName()!=null && student.getName().equals(“Justin”)))
System.out.println(“getName method failure”);}
}
Testing the Junit wayimport junit.framework.*;public class TestStudent1 {
public static void main(String[] args) { Student student = new Student(“043", "Justin", 100);Assert.assertEquals("getName method failure", "Justin", student.getName());}
}
Assert in Junit Class Assert provide a set of methods These methods are static They can throw an object with failure
messages. – Messages are only displayed when an
assert fails
Junit TestCasepublic class TestIt extends TestCase { public TestIt (String name) { super (name); } public void testGetMethod() {
Student student=new Student(“013", "Justin", 100);
assertEquals(“013", student.getNumber()); assertEquals("Justin", student.getName());assertEquals(100, student.getScore());
}}
Junit in Eclipse Click on File -> New -> Junit Testcase Give the name for the testcase and
create/copy the file in the editor. Click on Run -> Run. Select Junit
Testcase, New and click on Run.
Junit in Eclipse
Assert.assertEquals assertEquals can be overrided many ti
mes for different comparison, such as for String, int
For object comparison, you have to override equals in order to compare fields of two objects
Equals in our examplepublic boolean equals(Object anObject){
if (anObject instanceof Student){
Student aStudent=(Student) anObject;return
aStudent.getNumber().equals(getNumber())&& aStudent.getName()==getName()&&aStudent.getScore==getScore();}return false;
}
JUnit Rules and Conventions
Subclass TestCase Test methods
– public void testXXX() [throws …]
– Any number of assertions per method