html5-img
1 / 20

Approach for Unit testing with the help of JUnit ...

Approach for Unit testing with the help of JUnit . Satish Mishra mishra@informatik.hu-berlin.de. This session. Testing concepts Unit testing Testing tools JUnit Practical use of tools Examples Writing unit test cases With the help of you all Discussions Always welcome.

ayasha
Télécharger la présentation

Approach for Unit testing with the help of JUnit ...

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Approach forUnit testing with the help of JUnit ... Satish Mishra mishra@informatik.hu-berlin.de

  2. This session • Testing concepts • Unit testing • Testing tools • JUnit • Practical use of tools • Examples • Writing unit test cases • With the help of you all • Discussions • Always welcome Unit testing with JUnit

  3. Status of assignments • Doubts ? • Progress ! ! • Results ?? Unit testing with JUnit

  4. Unit vs Functional tests • Unit tests:: • Unit tests are written from a programmer's perspective. They ensure that a particular method of a class successfully performs a set of specific tasks. Each test confirms that a method produces the expected output when given a known input. • Functional tests:: • Functional tests are written from a user's perspective. These tests confirm that the system does what users are expecting it to. Unit testing with JUnit

  5. Boundries between Unit and Functional tests • If a unit test crosses class boundaries, it might be a functional test. • If a unit test is becoming very complicated, it might be a functional test. • If a unit test is a valid test but it has to change continually to handle different user permutations, it might be a functional test. • If a unit test is harder to write than the code it is testing, it might be a functional test. Unit testing with JUnit

  6. Top reasons to write unit tests • Tests reduce bugs in new features • Tests reduce bugs in existing features • Tests are good documentation • Tests reduce the cost of change • Tests improve design • Tests allow refactoring • Tests constrain features • Testing forces you to slow down and think • Testing makes development faster • Tests reduce fear Unit testing with JUnit

  7. Example: 1. Hello World Program • Write test case for a ‘Hello World’ program which is written in java. Unit testing with JUnit

  8. Hello world Unit Test • Instance of HelloWorld.java should not be null • Output should be “Hello World“. Unit testing with JUnit

  9. Hello world java code class HelloWorld { /** * Print "Hello World" */ void sayHello() { System.out.println("Hello World"); } /** * Test */ public static void main( String[] args ) { HelloWorld world = new HelloWorld(); world.sayHello(); } } Unit testing with JUnit

  10. JUnit tests for HelloWorld • public class HelloWorldTest extends junit.framework.TestCase { Hello HelloWorld ; • public HelloWorldTest () { } // default constructor • protected void setUp() { // creates a (simple) test fixture Hello = new Helloworld(); } • protected void tearDown() { } // no resources to release Unit testing with JUnit

  11. JUnit tests for HelloWorld… • testSayHello() { assert( Hello!=null ); assertEquals("Hello World",Hello.sayHello()); } } // End from last slide Unit testing with JUnit

  12. Example:2 Write a java program for addition and multiplication of square Matrix. Detail: A + B = C (Simple addition) A x B = C [AB]ij = SIGMA [A]ik[B]kj where A and B are square Matrix and C is resultant Matrix Unit testing with JUnit

  13. Matrix Program Unit Tests:: We all will write together .. Unit testing with JUnit

  14. Matrix Program Java Code:: • Suppose i wrote this java program according to suggested test cases. • Now we will add the test case in my program • Try with JUnit Unit testing with JUnit

  15. Result • We will see with the help of tool When ?? !! Now !! Unit testing with JUnit

  16. Example 3 Employee Database • Write a java class for reading a Employee database which is in text file with specific format and then storing the text file data into the database. Program should use odbc for the connetion. Detail:: Text file contains the data in the sequence of Employee Id, Department Id,Name,Date of birth, Date of Joining, Salary, Position, Extension, Location Unit testing with JUnit

  17. Employee Database Program Unit Tests:: We all will write together .... Unit testing with JUnit

  18. Employee Database Program Java Code:: • Suppose java code we got from our colleague who is also working with us • Now we will add the test case in this program • Try with JUnit Unit testing with JUnit

  19. Result • We will see with the help of tool When ?? !! Now !! Unit testing with JUnit

  20. The End Thank You Unit testing with JUnit

More Related