JUnit Testing

Introduction- JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run repeatable tests. Erich Gamma and Kent Beck initially develop it. It is an instance of xUnit architecture. As the name implies, it is used for Unit Testing of a small chunk of code. Developers who are following test-driven methodology must write and execute unit test first before any code. Once you are done with code, you should execute all tests, and it should pass. Every time any code is added, you need to re-execute all test cases and makes sure nothing is broken.

What is Unit Testing?
Unit Testing is used to verify a small chunk of code by creating a path, function or a method.
The term “unit” exist earlier than the object-oriented era. It is basically a natural abstraction of an object oriented system i.e. a Java class or object (its instantiated form).
Unit Testing and its importance can be understood by below-mentioned points:
1. Unit testing is used to identify defects early in software development cycle.
2. Unit Testing will compel to read our own code. i.e. a developer starts spending more time in reading than writing.
3. Defects in the design of code affect the development system. A successful code breeds the confidence of developer.

Types of unit testing :-
There are two ways to perform unit testing: 1) manual testing 2) automated testing.
1) Manual Testing :- If we execute the test cases manually without any tool support, it is known as manual testing. It is time consuming and less reliable.
2) Automated Testing :- If we execute the test cases by tool support, it is known as automated testing. It is fast and more reliable.

Why You Need JUnit Testing?

  • It finds bugs early in the code, which makes our code more reliable.
  • JUnit is useful for developers, who work in a test-driven environment.
  • Unit testing forces a developer to read code more than writing.
  • You develop more readable, reliable and bug-free code which builds confidence during development. 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.

Annotations for Junit testing:-
Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.
The Junit 4.x framework is annotation based, so let’s see the annotations that can be used while writing the test cases.
1. @Test annotation specifies that method is the test method.
2. @Test(timeout=1000) annotation specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).
3. @BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.
4. @Before annotation specifies that method will be invoked before each test.
5. @After annotation specifies that method will be invoked after each test.
6. @AfterClass annotation specifies that method will be invoked only once, after finishing all the tests.

Assert Class :-
Assert is a method used to determine status of a test case(Pass or Fail.
The assert methods are provided by the class org.junit.Assert which extends java.lang.Object class.
1. void assertEquals(boolean expected,boolean actual): checks that two primitives/objects are equal. It is overloaded.
2. void assertTrue(boolean condition): checks that a condition is true.
3. void assertFalse(boolean condition): checks that a condition is false.
4. void assertNull(Object obj): checks that object is null.
5. void assertNotNull(Object obj): checks that object is not null.

Features and advantages of JUnit5:-
JUnit has added many new features in JUnit4. You can understand it easily by comparing JUnit 3.x and JUnit 4.x. Below is quick comparison between JUnit4.x and JUnit 3.x –
All the old assert statements are same as before. Most of the things are easier in JUnit4 as..
1. With JUnit 4 you are more capable of identifying exception. You can define expected exception as a parameter while using @test annotation.
2. Parameterized test is introduced, which enables us to use parameters.
3. JUnit4 still can execute JUnit3 tests.
JUnit 4 can be used with java5 or higher version.
While using JUnit4, you are not required to extend JUnit.framework.TestCase. You can just create a simple java class.

Mr. Arshad Hussain, Assistant Professor, School of Computer Applications, Career Point University, Kota

Leave a Reply

Your email address will not be published. Required fields are marked *