• No results found

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

N/A
N/A
Protected

Academic year: 2021

Share "Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

T

ESTING WITH

J

UNIT IN

E

CLIPSE

S lide s pr epa re d by : F ar za na R ahm an

J

UNIT IN

E

CLIPSE

1 S lide s pr epa re d by : F ar za na R ahm an

(2)

I

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

(3)

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

(4)

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

(5)

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

(6)

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

(7)

•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

(8)

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

(9)

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

(10)

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 an

conditionals

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.

10

S lide s pr epa re d by : F ar z ana R ahm an

(11)

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

(12)

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

(13)

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

(14)

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

References

Related documents

mandatory emission reductions under a cap-and-trade regime and generate credits when they reduce or sequester GHG emissions beyond a predetermined baseline. These credits can then

6.7.2 The association between being bullied and perceived health status and school related factors: ...259 6.7.3 The association between being bullied and other risk behaviours:

Blue water is a mineral water company dedicated to provide clean water ,healthy Water to the society.the main aim of the company is to provide quality product.. & consumer

yhsm-inucbr_001, pokemon light platinum elite four, pokemon light platinum gyms, pokemon light platinum legendary locations, light platinum elite four, pokemon light platinum

How to install the xPack Windows Build Tools binaries | The xPack Project 2.3 Installing OpenOCD for Source Debugging.. In order to do source level debugging in Eclipse, OpenOCD

Reinstating Ohio’s energy efficiency standards will drive cost-effective investment in home weatherization for low-income households — reducing financial stress on

To run tests created in Eclipse with ADT with command-line tools, you must first install additional files into the test project using the android tool's

Once the SDK Manager is running, return to the main Eclipse window and select the File -> Exit menu option to exit from the Eclipse environment.. This will leave the Android