T
ESTING WITH
J
UNIT IN
E
CLIPSE
S lide s pr epa re d by : F ar za na R ahm anJ
UNIT IN
E
CLIPSE
1 S lide s pr epa re d by : F ar za na R ahm anI
NTRODUCTION
The class that you will want to test is created first so that
Eclipse will be able to find that class under test when you build the test case class. The test cases are built with the needed imports and extensions for JUnit to run. Once the test case class is built the actual test cases are then coded in by the programmer. S lide s pr epa re d by : F ar z ana R ahm an programmer.
Reminder of JUnit Naming Conventions:
Test Case Class: Named [classname]Test.java, where
classname is the name of the class that is being tested.
Test Case Method: Named test[methodname], where
methodname is the name of the method that is tested.
2 S lide s pr epa re d by : F ar z ana R ahm an
A
DDING
J
UNIT
.
JAR
Right click on the project and select Properties. In the left
column select Java Build Path. To the right select the
Libraries tab. Click the “Add External JARs” Button to
add the Jnit.jar. You can download Junit.jar from the following link
Its an open source and public package that is free for usage
S lide s pr epa re d by : F ar z ana R ahm an
Its an open source and public package that is free for usage
in JAVA.
Next some screenshots are provided so that you can
understand how to create Junit test cases.
We are using a sample program to be tested and follow the
steps as described. S lide s pr epa re d by : F ar z ana R ahm an
This is our familiar eclipse environment. We will start the tutorial here by opening up a project we wish to write a test suite for. Also start by selecting a file in your resources window that you wish to create a test for
S lide s pr epa re d by : F ar z ana R ahm an 4 S lide s pr epa re d by : F ar z ana R ahm an
The next step is to add our new test case. Since we have selected a file in our resource browser we can just go to file new JUnit Test Case. This will help us by filling in some information for us. It will also create a file for us the is named identically to our class except that "Test" is appended to the name of the class.
S lide s pr epa re d by : F ar z ana R ahm an S lide s pr epa re d by : F ar z ana R ahm an
•We are now presented with a New Junit Test Case window.
•The source folder is where we are going to place our new test file, while the package is the package it will be created in.
•We can rename the test if we like and choose a different superclass.
S lide s pr epa re d by : F ar z ana R ahm an
•For our purposes all of the information provided for us is correct.
•You will notice 4 check boxes. If the methods that you are trying to test are not static, you will probably want to specify a setUp method for test case.
6 S lide s pr epa re d by : F ar z ana R ahm an
•This will be where you can instantiate your object before running the tests. After we are done with this you may click the next button.
•In this next window we are allowed to specify which methods we wish to test.
•By selecting methods in this window eclipse will create a method in our test file named the same exact thing as the methods we select but with "Test" added in front of them.
S lide s pr epa re d by : F ar z ana R ahm an
"Test" added in front of them.
•If you want you can select "Create tasks for generated test methods" this will place //TODO markers in the code so you know where to add code by searching for //TODO.
S lide s pr epa re d by : F ar z ana R ahm an
At this point we now have our new test file opened in our editor. you look in the resource browser you will see a new file with "Test" at the end. This is the source file for our tests for that particular class.
S lide s pr epa re d by : F ar z ana R ahm an 8 S lide s pr epa re d by : F ar z ana R ahm an
The JUnit framework provides us with several ways to judge if a test has succeeded or failed.
Our TestCase object that we have extends Assert which provides the following functionality for testing.
•assertFalse: Use this if you know the function will always return false (fails if it receives true)
•assertEquals: This provides a series of overloads that allows
S lide s pr epa re d by : F ar za na R ahm an
•assertEquals: This provides a series of overloads that allows you to test if an actual value matches the expected one.
•assertTrue: Use this if you know the function will always return true (fails if it receives false)
•assertNotNull: If your method return null in the event of
S lide s pr epa re d by : F ar za na R ahm an
•
assertNotSame: If your method is supposed to return an
element from a list you can use this to check if the
element returned is the one from the actual list
•
assertNull: If your method return null in the event of
failure use this to check to see if it fails
•
fail: Will fail the test, use this in conjunction with
conditionals
S lide s pr epa re d by : F ar z ana R ahm anconditionals
•
failNotEquals: Essentially the same as assertEquals but
will fail the test if they aren’t equal instead of causing an
error
•
failNotSame: Essentially the same as assertNotSame
except instead of causing an error it will cause a failure.
10S lide s pr epa re d by : F ar z ana R ahm an
S lide s pr epa re d by : F ar za na R ahm an S lide s pr epa re d by : F ar za na R ahm an
We now want to run the test that we just created to do this we have several options. We can either create a test suite from the file new other menu or we can select the test we want to run and then goto run, run as, JUnit test.
S lide s pr epa re d by : F ar za na R ahm an 12 S lide s pr epa re d by : F ar za na R ahm an
You will notice that eclipse has opened a JUnit window on the left side of your browser. This will display the results of your testing. If you have complex tests which call other tests you can see the trace of the failure at the bottom. You are also presented with which tests failed under the failures tab.
S lide s pr epa re d by : F ar za na R ahm an S lide s pr epa re d by : F ar za na R ahm an
S lide s pr epa re d by : F ar z ana R ahm an 14 S lide s pr epa re d by : F ar z ana R ahm an