junit a unit testing framework for java. the objective introduce junit as a tool for unit testing...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/1.jpg)
JUnit
A Unit Testing Framework for Java
![Page 2: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/3.jpg)
The Problem
Few programmers write unit tests for their code
Fewer tests → Less Productivity → Less Stable Code → More pressure on You
![Page 4: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/4.jpg)
Testing Practices
a little test, a little code, a little test, a little code
Do not use a Print Statement, Write a Test instead
![Page 5: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/7.jpg)
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%\junit.jar Add JUnit installation directory to the
classpath: set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%
![Page 8: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/8.jpg)
Testing the Installation Use either the textual or graphical test
runner to run the sample tests For the textual TestRunner, type:
java junit.textui.TestRunner junit.samples.AllTests For the graphical TestRunner, type:
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.
![Page 9: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/9.jpg)
A Sample Test Case
We will Create a Java Class Create a Test Case Class Test using TestRunner
![Page 10: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/10.jpg)
Sample Java Class - Studentpackage 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; }}
![Page 11: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/11.jpg)
JUnit Test Class - TestStudentpackage 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"); } }
![Page 12: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/13.jpg)
TestRunner SwingUI – Successful Test
![Page 14: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/14.jpg)
TestRunner SwingUI – Failed Test
![Page 15: 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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c01c1a28abf838cd017b/html5/thumbnails/15.jpg)
References http://www.junit.org http://junit.sourceforge.net/doc/tes
tinfected/testing.htm http://junit.sourceforge.net/doc/coo
kstour/cookstour.htm Gamma, E., et al. Design Patterns:
Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995