What is Junit?
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.
Here is what you will learn
|Tutorial||How to Download and Installation JUnit|
|Tutorial||JUnit Test framework|
|Tutorial||JUnit Annotations & API|
|Tutorial||Create JUnit Test Suite|
|Tutorial||JUnit Ignore Test|
|Tutorial||JUnit Exception Test|
|Tutorial||JUnit Parameterized Test|
|Tutorial||JUnit Vs TestNG|
What is Unit Testing?
Before discussing JUnit testing in detail, it is imperative to understand 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:
- Unit testing is used to identify defects early in software development cycle.
- Unit testing will compel to read our own code. i.e. a developer starts spending more time in reading than writing.
- Defects in the design of code affect the development system. A successful code breeds the confidence of developer.
Learn more about unit testing here
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.
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..
- With JUnit 4 you are more capable of identifying exception. You can define expected exception as a parameter while using @test annotation.
- Parameterized test is introduced, which enables us to use parameters.
- 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.
- You need to use annotations in spite of special method name as before.
- Instead of using setup method, you need to use @before annotation.
- Instead of using teardown method, put @after annotation.
- Instead of using testxxxx before method name, use @test annotation.