Monday, September 2, 2013

JUnit Basic


JUnit

Junit is a testing framework for the Java language.
This is good in the development of test -driven development and provide the Unit testing.

Unit testing is the testing of single entity (class or method).

Unit testing can be done in two ways:
  1. Manual Testing
    - executing test cases manually.
    * time consuming and tedious. ( since the testing made by a human resources, it will slow )
    * big investment for test resources
    * less reliable
    * non-programmable: No programming can be done to write sophisticated tests which fetch hidden information.
  2. Automated Testing
- taking tool support and executing the test cases by using automation tool.
* significantly faster than human resources.
* less investment in human resources.
* more reliable: Automation tests perform precisely same operation each time they are run.
* Programmable: Testers can program sophisticated tests to bring out hidden information.

Features -

* Provides Annotation to identify the test methods.
* Provides Assertions for testing expected results.
* Provides Test runners for running tests.
* JUnit tests allow you to write code faster which increasing quality
* JUnit is elegantly simple. It is less complex & takes less time.
* JUnit tests can be run automatically and they check their own results.
* JUnit tests can be organized into test suites containing test cases.
* JUnit shows test progress in a bar – green : fine | red : fails.


Unit tests and unit testing

A unit test : is a piece of code written by a developer that executes a specific functionality in the code which is tested.
Test coverage : the percentage of code which is tested by unit tests

* Unit tests target small units of code,
e.g. a method or a class, (local tests)

( component and integration tests targeting to test the behavior of a component or the integration between a set of components or a complete application consisting of several components. )

Typically unit tests are created in own project / own source folder to avoid that the normal code and the test code is mixed.

Advantages of JUnit

* It’s used to test an existing class. Suppose, If we have Calculation class then, we need to write CalculationTest to test the Calculation class.

* Using JUnit we can save testing time.
In real time, in the web application development we implement JUnit test cases in the DAO classes. DAO classes can be tested with out server.

* With JUnit we can also test Struts / Spring applications but most of the time we test only DAO classes. Even some times service classes also tested using JUnit.

* Using JUnit, If we want to test the application (for web applications) then server is not required, so the testing becomes fast.