J Unit


Published on

About J-Unit Test Cases

Published in: Technology
1 Comment
1 Like
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

J Unit

  1. 1. J-Unit 4.x Writing Test Cases
  2. 2. Why J-Unit ? <ul><li>For Test Driven Development. </li></ul><ul><li>Write the test first….. Then code to make your test pass </li></ul><ul><li>Official Definition: </li></ul><ul><li>JUnit is a unit testing framework for the Java programming language . JUnit has been important in the development of test-driven development , and is one of a family of unit testing frameworks collectively known as xUnit that originated with SUnit . </li></ul>
  3. 3. What is J-Unit ? <ul><li>JUnit features include: </li></ul><ul><li>Assertions for testing expected results </li></ul><ul><li>Test fixtures for sharing common test data </li></ul><ul><li>Test runners for running tests </li></ul>
  4. 4. @Test <ul><li>@Test   </li></ul><ul><li>public   void  addition() {    </li></ul><ul><li>     assertEquals(12, simpleMath.add(7, 5));    </li></ul><ul><li>}    </li></ul><ul><li>   Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”.  In addition, your class does not need to extend from “TestCase” class </li></ul>
  5. 5. @Before  <ul><li>@Before   </li></ul><ul><li>public   void  preTestRun() {    </li></ul><ul><li>     simpleMath =  new  SimpleMath();    </li></ul><ul><li>}    </li></ul><ul><li>   Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case </li></ul>
  6. 6. @After <ul><li>@After   </li></ul><ul><li>public   void  postTestRun() {    </li></ul><ul><li>     simpleMath =  null ;    </li></ul><ul><li>}    </li></ul>
  7. 7. @ BeforeClass and @AfterClass <ul><li>@BeforeClass   </li></ul><ul><li>public   static   void  runBeforeClass() {    </li></ul><ul><li>     // run for one time before all test cases    </li></ul><ul><li>}    </li></ul><ul><li>   </li></ul><ul><li>@AfterClass   </li></ul><ul><li>public   static   void  runAfterClass() {    </li></ul><ul><li>     // run for one time after all test cases    </li></ul><ul><li>}    </li></ul><ul><li>Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases. </li></ul>
  8. 8. Exception Handling <ul><li>@Test(expected = ArithmeticException. class )    </li></ul><ul><li>public   void  divisionWithException() {    </li></ul><ul><li>     // divide by zero    </li></ul><ul><li>     simpleMath.divide(1, 0);    </li></ul><ul><li>}    </li></ul><ul><li>Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown. </li></ul>
  9. 9. @Ignore <ul><li>@Ignore(&quot;Not Ready to Run&quot;)    </li></ul><ul><li>@Test   </li></ul><ul><li>public   void  multiplication() {    </li></ul><ul><li>     assertEquals(15, simpleMath.multiply(3, 5));    </li></ul><ul><li>}    </li></ul><ul><li>Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want. </li></ul>
  10. 10. Assertions
  11. 11. Timeout <ul><li>@Test(timeout = 1000)    </li></ul><ul><li>public   void  infinity() {    </li></ul><ul><li>     while  ( true )    </li></ul><ul><li>         ;    </li></ul><ul><li>}    </li></ul><ul><li>Define a timeout period in milliseconds with “timeout” parameter. The test fails when the timeout period exceeds. </li></ul>
  12. 12. Ant  Html Reports <ul><li>Ensure that Ant's optional. jar file is either in your CLASSPATH or exists in your $ANT_HOME/lib directory. </li></ul><ul><li>Add an ANT property for the directory containing the HTML reports: </li></ul><ul><li><property name=&quot;test.reports&quot; value=&quot;./reports&quot; /> </li></ul><ul><li>Define the Ant task for running JUnit and generating reports: </li></ul>
  13. 13. Ant  Html Reports Contd…. <ul><li><target name=&quot;test-html&quot;> </li></ul><ul><li><junit fork=&quot;yes&quot; printsummary=&quot;no“ haltonfailure=&quot;no&quot;> </li></ul><ul><li><batchtest fork=&quot;yes&quot; todir=&quot;${test.reports}&quot; > </li></ul><ul><li><fileset dir=&quot;${classes}&quot;> </li></ul><ul><li><include name=&quot;**/*Test.class&quot; /> </li></ul><ul><li></fileset> </li></ul><ul><li></batchtest> </li></ul><ul><li><formatter type=&quot;xml&quot; /> </li></ul><ul><li><classpath refid=&quot;test.classpath&quot; /> </li></ul><ul><li></junit> <junitreport todir=&quot;${test.reports}&quot;> </li></ul><ul><li><fileset dir=&quot;${test.reports}&quot;> </li></ul><ul><li><include name=&quot;TEST-*.xml&quot; /> </li></ul><ul><li></fileset> </li></ul><ul><li><report todir=&quot;${test.reports}&quot; /> </li></ul><ul><li></junitreport> </li></ul><ul><li></target> </li></ul>
  14. 14. Ant  Html Reports Contd…. <ul><li>Run the test: </li></ul><ul><li>ant test-html </li></ul>
  15. 15. The Concept of Mocking <ul><li>Test your code not third party dependencies </li></ul><ul><li>Create an Object that will replace a real object </li></ul>
  16. 16. Use EasyMock <ul><li>Download easyMock.jar and put it in your classpath </li></ul><ul><li>For mocking classes use EasyMock Extension </li></ul>
  17. 17. EasyMock….. <ul><li>Create a Mock Object </li></ul><ul><li>EasyMock.createMock(InterfaceName.class) </li></ul><ul><li>Set the Mock Object in the Class whose method you are testing </li></ul><ul><li>EasyMock. expect (your mock object.method which you are mocking) </li></ul><ul><li>Replay and Verify </li></ul>
  18. 18. Example…. <ul><li>package com.sample.test; </li></ul><ul><li>import static org.junit.Assert. assertEquals ; </li></ul><ul><li>import org.easymock.EasyMock; </li></ul><ul><li>import org.junit.After; </li></ul><ul><li>import org.junit.Before; </li></ul><ul><li>import org.junit.Test; </li></ul><ul><li>public class SampleTest { </li></ul><ul><li>SampleClassInterface sampleClassMock; </li></ul><ul><li>Addition addition; </li></ul>
  19. 19. Example Contd…… <ul><li>@Test </li></ul><ul><li>public void testLoad(){ </li></ul><ul><li>EasyMock. expect (sampleClassMock.addition(10, 20)).andReturn( new Integer(12)); </li></ul><ul><li>EasyMock. replay (sampleClassMock); </li></ul><ul><li>int result = addition.add(10, 20); </li></ul><ul><li>EasyMock. verify (sampleClassMock); </li></ul><ul><li>assertEquals ( new Integer(result), new Integer(12)); </li></ul><ul><li>} </li></ul>
  20. 20. Example Contd….. <ul><li>@Before </li></ul><ul><li>public void setup(){ </li></ul><ul><li>addition = new Addition(); </li></ul><ul><li>sampleClassMock = EasyMock. createMock (SampleClassInterface. class ); </li></ul><ul><li>addition.setSampleClassInterface(sampleClassMock); </li></ul><ul><li>System. out .println(&quot;Setup Method&quot;); </li></ul><ul><li>} </li></ul>
  21. 21. Example Contd…. <ul><li>@After </li></ul><ul><li>public void tearDown(){ </li></ul><ul><li>System. out .println(&quot;Tear Down Method&quot;); </li></ul><ul><li>} </li></ul>
  22. 22. Example Contd….. <ul><li>package com.sample.test; </li></ul><ul><li>public class Addition { </li></ul><ul><li>private SampleClassInterface sampleClass; </li></ul><ul><li>//junit </li></ul><ul><li>public Addition(SampleClass sampleClassInterface) { </li></ul><ul><li>super (); </li></ul><ul><li>this .sampleClass = sampleClassInterface; </li></ul><ul><li>} </li></ul>
  23. 23. Example Contd….. <ul><li>public Addition() { </li></ul><ul><li>super (); </li></ul><ul><li>} </li></ul><ul><li>public int add( int a, int b ){ </li></ul><ul><li>int result = sampleClass.addition(a,b); </li></ul><ul><li>return result; </li></ul><ul><li>} </li></ul><ul><li>public void setSampleClassInterface(SampleClassInterface sampleClassInterface) { </li></ul><ul><li>this .sampleClass = sampleClassInterface; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  24. 24. Why Coverage….?? <ul><li>Code coverage is a measure used in software testing . It describes the degree to which the source code of a program has been tested. It is a form of testing that inspects the code directly and is therefore a form of white box testing . </li></ul>
  25. 25. Why Coverage…..?? Contd… <ul><li>Think about how you can focus around the developer and how Code Coverage can be used with Unit Testing. </li></ul><ul><li>If you ask most developers if they do a good job testing their code, they would tell you yes. </li></ul><ul><li>However in another study (Boris Beizer in Johnson 1994), the average programmer thought they tested around 95 percent of the code they wrote, but on further inspection the amount of actual code was more like 30 percent. </li></ul>
  26. 26. Why Coverage Contd….. <ul><li>Developers are under more and more stress to deliver more code in less time. </li></ul><ul><li>Automated code Generation tools used…. </li></ul><ul><li>Big Question ??????? </li></ul><ul><li>Do You Know Your Code……??? </li></ul>
  27. 27. Why Coverage Contd…. <ul><li>This information can be very enlightening. Think about it... you exercise your application with Code Coverage turned on and the results come back as 35 percent coverage. What's the deal? Did you just run your application with the &quot;happy path&quot; only or did you truly exercise the application? If you did exercise the application, then it appears a great deal of code may not be needed. </li></ul>
  28. 28. Why Code Coverage…?? <ul><li>Another thing to keep in mind is the concept of condition’s in code. </li></ul><ul><li>For each condition another test has to be written to properly test the other side of the condition. </li></ul><ul><li>Again, if the testing is only focused on one path, then it is most likely &quot;happy path&quot; testing. </li></ul>
  29. 29. Managers Boon… <ul><li>Once you get the use of Code Coverage down, it can be used for trending. </li></ul><ul><li>This is very useful for program managers and companies as a whole. </li></ul>
  30. 30. Managers Boon Contd….. <ul><li>For example, say that starting on week one we had 40 percent total coverage with unit test, week two we had 50 percent of coverage with unit tests, and week three we had 30 percent coverage with unit tests… what gives? </li></ul><ul><li>Well from week three to four it may have been that the amount of code stayed about the same and more unit tests were created, and between weeks four and five a lot of code was added and very few unit tests were added. </li></ul>
  31. 31. Code Coverage Tool <ul><li>EclEmma is a free Java code coverage tool for Eclipse, available under the Eclipse Public License. Internally it is based on the great EMMA Java code coverage tool, trying to adopt EMMA's philosophy for the Eclipse workbench: </li></ul><ul><li>Fast develop/test cycle: Launches from within the workbench like JUnit test runs can directly be analyzed for code coverage. </li></ul>
  32. 32. Code Coverage Tool Contd…… <ul><li>Rich coverage analysis: Coverage results are immediately summarized and highlighted in the Java source code editors. </li></ul><ul><li>Non-invasive: EclEmma does not require modifying your projects or performing any other setup. </li></ul>
  33. 33. Code Coverage Tool Contd…… <ul><li>Update Site for Eclipse 3.3 and Before </li></ul><ul><li>Download the eclemma plugin….. </li></ul><ul><li>Copy from features and plugins to the features and plugins of your eclipse…… </li></ul><ul><li>Restart your eclipse through command line using eclipse –clean </li></ul>
  34. 34. Code Coverage Example….
  35. 35. Code Coverage
  36. 36. Generating Coverage Report……
  37. 37. Questions? <ul><li>Questions? </li></ul>
  38. 38. Thanks <ul><li>Happy Coding and Unit Testing. </li></ul>