15
JUnit A Unit Testing Framework for Java

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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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