Junit
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Junit

on

  • 2,183 views

JUnit is a framework for Java, so the very first requirement is to have JDK installed in your machine. ...

JUnit is a framework for Java, so the very first requirement is to have JDK installed in your machine.
An open source framework which is used for writing & running tests
Shows test progress in a bar that is green if test is going fine and it turns red when a test fails

Statistics

Views

Total Views
2,183
Views on SlideShare
2,178
Embed Views
5

Actions

Likes
1
Downloads
53
Comments
1

1 Embed 5

http://www.linkedin.com 5

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Junit Presentation Transcript

  • 1. Ravi Kant Soni Senior Software Engineer i-Admin , Bangalore i-Admin,Bangalore 1
  • 2. WHAT IS TESTING Testing is the process of checking the functionality of the application whether it is working as per requirements 2 i-Admin,Bangalore
  • 3. WHAT IS JUNIT DeFacto framework for developing unit test in java  Currently version 4.x Developed by Erich Gamma & Kent Beck Released under IBM, and hosted on sourcefog Promotes the idea of "first testing then coding“ 3 i-Admin,Bangalore
  • 4. FEATURES OF JUNIT An open source framework which is used for writing & running tests Shows test progress in a bar that is green if test is going fine and it turns red when a test fails Provides Annotation & Assertions 4 i-Admin,Bangalore
  • 5. WHAT IS UNIT TEST CASES? Unit testing is the testing of single entity (class or method) A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected There must be at least two test cases for each requirement: one positive test and one negative test 5 i-Admin,Bangalore
  • 6. JUNIT ENVIRONMENT SETUP JUnit is a framework for Java, so the very first requirement is to have JDK installed in your machine. Download latest version of JUnit jar file from http://www.junit.org. 6 i-Admin,Bangalore
  • 7. SET ECLIPSE ENVIRONMENT 7 i-Admin,Bangalore
  • 8. JUNIT 4 8 i-Admin,Bangalore Automatically recognizes test methods preceded by @Test annotation No need for main() to run the tests Test methods must be public, void, with no parameters
  • 9. EXAMPLE import org.junit.Test; // for @Test import static org.junit.Assert.assertEquals; // assertEquals() public class TestJunit { @Test public void testAdd() { assertEquals(2, 2 + 1); } } 9 i-Admin,Bangalore
  • 10. JUNIT TEST OUTPUT 10 i-Admin,Bangalore
  • 11. JUNIT TEST OUTPUT 11 i-Admin,Bangalore
  • 12. JUNIT TEST OUTPUT 12 i-Admin,Bangalore Runs Total number of tests run Failures Tests that failed. For example assertions that failed. Errors Tests that generated unhandled (unexpected) exceptions. Time elapsed (ms)
  • 13. JUNIT CLASSES JUnit classes are important classes which is used in writing and testing Junits Assert which contain a set of assert methods TestCase which contain a test case defines the fixture to run multiple tests 13 i-Admin,Bangalore
  • 14. ASSERT org.junit.Assert.* static methods:  void assertEquals(expected, actual)  Works with object, int, long, byte, string, etc  Object: it invokes object.equals(object) for equality  assertEquals (expected, actual, εpsilon)  float and double  void assertFalse/ True(boolean condition)  void assertNull/ NotNull(Object object)  void fail() 14 i-Admin,Bangalore
  • 15. ASSERT EXAMPLE i-Admin,Bangalore 15
  • 16. ANNOTATION Annotations are like meta-tags  @Test  @Before  @After  @BeforeClass  @AfterClass  @Ignore 16 i-Admin,Bangalore
  • 17. ANNOTATION EXAMPLE i-Admin,Bangalore 17
  • 18. PARAMETERIZED TEST  Allows you to run the same test with different data  @RunWith(Parameterized.class)  public static method that returns a Collection of data  Collection must be an Array of the various parameters used for the test  public constructor that uses the parameters 18 i-Admin,Bangalore
  • 19. PARAMETERIZED EXAMPLE i-Admin,Bangalore 19
  • 20. SUITES  Specify an execution order  Add @Suite to an empty class @RunWith(Suite.class) @Suite.SuiteClasses({SomeTest.class}) public class AllTests { } 20 i-Admin,Bangalore
  • 21. SUITES EXAMPLE i-Admin,Bangalore 21
  • 22. TESTING EXCEPTIONS i-Admin,Bangalore 22
  • 23. GOOD UNIT TEST  Any static utility method must have test  Make exception tests  Business models with equals and hashcode  Test that “something is true” but also that “not-something is false”  Give your tests meaningful names 23 i-Admin,Bangalore
  • 24. MORE RESOURCE  Official site: www.junit.org  JUnit cook book http://junit.sourceforge.net/doc/cookbook /cookbook.htm (one recipe long!)  JUnit Javadoc: http://junit.org/junit/javadoc/4.5/ 24 i-Admin,Bangalore
  • 25. i-Admin,Bangalore 25