0% found this document useful (0 votes)
9 views16 pages

Junit: Unit Testing

JUnit is a test framework which uses annotations to identi fy methods which need to be tested. To write a test with junit you have to annotate a method with @or g.junit.Test then you can use a method provided by junit to check the expected result.

Uploaded by

donkeydonkey4455
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

Junit: Unit Testing

JUnit is a test framework which uses annotations to identi fy methods which need to be tested. To write a test with junit you have to annotate a method with @or g.junit.Test then you can use a method provided by junit to check the expected result.

Uploaded by

donkeydonkey4455
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link].

com

JUnit
What is unit tes ting? Unit testing is a piece of code written by developers like you which is just to make sure that the actual code in the project is worki ng the same way as expected. Remember this is not testing. Unit testing is the complete responsibility of a developer. A tester has nothing do with unity testing. Unit Testing JUnit is a test framework which uses annotations to identi fy methods which need to be tested. So To write a test with JUnit you have to annotate a method with @or [Link] Then you can use a method provided by JUnit to check the expected result. Simple !!! To put i t simply you can consider JUnit is another random API which can get you a JOB . To e xecute the JUnit program you can use [Link] class or just use the eclipse to execute. Lets start from the very basics i.e I nstallation of JUnit in eclipse Go to [Link] and do wnload the latest version. At the time of writing this awes ome document [Link] was the latest. At this point I was able to download that jar file from below link. https ://[Link]/KentBeck/junit/dow nloads https ://[Link]/downloads/KentBeck/junit/junit -[Link] Now we just have to impor t that jar in build path. Like I always suggested in my class it better if you create folder in your eclipse project and copy the junit****.jar file into that folder then imp ort it. I t is not advisable to choose Add External JAR.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Now JUnit framework is available for your project. Lets kick start b y writing a simple test case Since the class whi ch I am creating now is basically to test the functionality of s ome other class I will create a new Source folder in my project and separate all my Test related classes into this new Source folder .

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link] Right click on your project go to new then choose new source folder

Give the folder name as srctest s o that we can identify i t as test source fol der . Click Finish

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Right click on srctest and c hoose new then others then type junit in the new window.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Now choose JUnit Test Case and click next. I n the new window give the proper package name and name of the Tes t class. I choose to name it as CalculatorTest. I n Class under Tes t field click browse and choose the class you want to test.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Once you choose the proper class you wan t to test click next. Now you have option to choose the methods you want to test are . Choose the method or methods you would like to test.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Then click finish. Thi s will auto generate the test class and create a test method also.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

But nothing has been implemented in the Test method. Have you noticed the @Test annota tion in the auto generated code ? Now create the obj ect of Calculator c lass and call add(i nt n1, int n2) method inside assertEquals(long expected, long actual) method. Just write the codes; dont bother what assertEquals method is. I will explain it to you after someti me.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

Once you written the code as above right click on the cl ass file and click runs as then choose JUnit Tes t Case.

You will see the output as in the below screenshot. I n Failure Trace section you can see that expected:<50> but was : <40>

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link] The addi tion of 10 and 30 is of course 40 but we written the expected value as 50. Now let correct it by passing 20, 30 and execute it.

Now the test case is successful. assertEqual s method is a JUnit method which takes two parameters , one is the actual and another one is expected. I f both matches then the test case is successful.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link]

JUnit framework understands that a particular method is a Test method with the help of @Test annotation. There are many assertEquals overloaded methods which you experim ent it and learn more about it. To know what are the o ther annotation exists in JUnit please refer to below tabl e.

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

+91 80 23400383, + 91 96321 88899 + 1-515-708-3176 [Link] Lets say you written a performance critical application wher e a method should not take more than 100 0 milliseconds. I f you use the traditi onal mai n method approac h it will difficult implementation to conduct such an experiment. In this scenario you can simply use @Test(timeout=1000) annotation . Below table will give the exposure to the different assert statement s or methods available in JUnit.

Try writing the JUnit Test cases for all the methods you created in your different project s . This is the best way to learn JUnit a lot deeper. All the Best

Al l r i gh t r e s e r ve d b y F l a m b o y a n z 2 0 1 2 | F e l i gh t i s r e gi s te r e d tr a d e ma r k o f F l a m b o y a n z S o f t w a r e P VT L T D

You might also like