junit.framework.TestCase

Download Report

Transcript junit.framework.TestCase

JUnit
A Unit Testing Framework for Java
The Objective


Introduce JUnit as a tool for Unit
Testing
Provide information on how to:



Install it
Build a test class
Run the test program using the Test
Runner tool
The Problem


Few programmers write unit tests for
their code
Fewer tests → Less Productivity → Less
Stable Code → More pressure on You
Testing Practices


a little test, a little code, a little test, a
little code
Do not use a Print Statement, Write a
Test instead
Introduction to JUnit




A simple framework to write repeatable
tests
Automates the tests
Creates tests that retain their value over
time
It is a program testing a program
Advantages of JUnit





Programmers become more productive
Increases the Quality of the developed code
Easily run regression tests, i.e. run your tests
again and again
You could write the tests before writing the
code
Can be composed into a hierarchy of test
suites
Installing JUnit


Download the latest version of JUnit from
http://sourceforge.net/project/showfiles.php?
group_id=15278
To install on windows:



Unzip the junit.zip distribution file to a directory
referred to as %JUNIT_HOME%.
Add JUnit to the classpath: set
CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\ju
nit.jar
Add JUnit installation directory to the classpath:
set
CLASSPATH=%CLASSPATH%;%JUNIT_HOME%
Testing the Installation


Use either the textual or graphical test runner to
run the sample tests
For the textual TestRunner, type:


For the graphical TestRunner, type:



java junit.textui.TestRunner junit.samples.AllTests
java junit.swingui.TestRunner junit.samples.AllTests
All the tests should pass with an "OK" (textual) or a
green bar (graphical)
If the tests do not pass, verify the CLASSPATH.
A Sample Test Case



We will Create a Java Class
Create a Test Case Class
Test using TestRunner
Sample Java Class - Student
package toban.Junit;
public class Student {
private String name;
private int studentID;
public Student(String first, String last, int id){
this.name = last;
this.studentID = id;
}
public String getName(){
return this.name;
}
}
public int getID(){
return this.studentID;
}
JUnit Test Class - TestStudent
package toban.Junit;
import junit.framework.TestCase;
public class TestStudent extends TestCase{
private Student student_1;
protected void setUp(){
student_1 = new Student("Peter", "Parker", 10500015);
}
public void testID(){
assertEquals(student_1.getID(),10500015);
}
}
public void testNames(){
assertSame(student_1.getName(), "Parker");
}
Running the Test

Enter the TestRunner command:




C:\Junit Demo\bin>java junit.swingui.TestRunner
toban.Junit.TestStudent
The UI will pop up
If the test is successful, it will be green
If the test failed, it will be red
TestRunner SwingUI –
Successful Test
TestRunner SwingUI – Failed
Test
References




http://www.junit.org
http://junit.sourceforge.net/doc/testinfe
cted/testing.htm
http://junit.sourceforge.net/doc/cooksto
ur/cookstour.htm
Gamma, E., et al. Design Patterns:
Elements of Reusable Object-Oriented
Software, Addison-Wesley, 1995