Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 JUnit A Unit Testing Framework for Java

2 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

3 The Problem Few programmers write unit tests for their code Fewer tests → Less Productivity → Less Stable Code → More pressure on You

4 Testing Practices a little test, a little code, a little test, a little code Do not use a Print Statement, Write a Test instead

5 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

6 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

7 Installing JUnit Download the latest version of JUnit from http://sourceforge.net/project/showfiles.php? group_id=15278 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%\j unit.jar Add JUnit installation directory to the classpath: set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%

8 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.

9 A Sample Test Case We will Create a Java Class Create a Test Case Class Test using TestRunner

10 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; }

11 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"); } }

12 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

13 TestRunner SwingUI – Successful Test

14 TestRunner SwingUI – Failed Test

15 References http://www.junit.org http://junit.sourceforge.net/doc/testinfe cted/testing.htm http://junit.sourceforge.net/doc/testinfe cted/testing.htm http://junit.sourceforge.net/doc/cooksto ur/cookstour.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


Download ppt "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."

Similar presentations


Ads by Google