Friday 10 January 2014

JUnit Overview




JUnit is a unit testing framework for the Java Programming Language. It is important in the test driven development, and is one of a family of unit testing frameworks collectively known as xUnit.

JUnit promotes the idea of "first testing then coding", which emphasis on setting up the test data for a piece of code which can be tested first and then can be implemented . This approach is like "test a little, code a little, test a little, code a little..." which increases programmer productivity and stability of program code that reduces programmer stress and the time spent on debugging.
JUnit is an open source framework and for the purpose of writing and running test cases for java programs. In the case of web applications JUnit is used to test the application with out server. This framework builds a relationship between development and testing process.


JUnit is an open source framework which is used for writing & running tests.

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 and provide immediate feedback. There's no need to manually comb through a report of test results.

JUnit tests can be organized into test suites containing test cases and even other test suites.

Junit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.


A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected. To achieve those desired results quickly, test framework is required .JUnit is perfect unit test framework for java programming language.

A formal written unit test case is characterized by a known input and by an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a postcondition.

There must be at least two unit test cases for each requirement: one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.



If we want to test any application, In standalone, we need to call the respective method’s main() method but there are so many problems in testing the flow using main() method.

In a web application, to test the flow we need to deploy it in the server so if there is a change then again we need to restart the server. Here also facing problems while testing the application.

Writing main method checks is convenient because all Java IDEs provide the ability to compile and run the class in the current buffer, and certainly have their place for exercising an object’s capability. There are, however, some issues with this approach that make it ineffective as a comprehensive test framework:
There is no explicit concept of a test passing or failing. Typically, the program outputs messages simply with System.out.println; the user has to look at this and decide if it is correct.
main has access to protected and private members and methods. While you may want to test the inner workings of a class may be desired, many tests are really about testing an object’s interface to the outside world.
There is no mechanism to collect results in a structured fashion.
There is no replicability. After each test run, a person has to examine and interpret the results.
The JUnit framework addresses these issues, and more


JUnit addresses the above issues, 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.

No comments:

Post a Comment