JUnit Tutorial for Beginners: Learn in 3 Days
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.
JUnit Tutorial Syllabus
Here is what you will learn
|👉 Lesson 1||Download and Install JUnit — How to Download and Install JUnit in Eclipse|
|👉 Lesson 2||JUnit Test Framework — JUnit Test Cases @Before @BeforeClass Annotation|
|👉 Lesson 3||JUnit Annotations Tutorial — Learn With Example|
|👉 Lesson 4||JUnit Assert & AssertEquals — Learn With Example|
|👉 Lesson 5||Create JUnit Test Suite with Example — @RunWith @SuiteClasses|
|👉 Lesson 6||JUnit @Ignore Test Annotation — What is JUnit Ignore & Examples|
|👉 Lesson 7||JUnit Expected Exception Test — Example using @test(expected)|
|👉 Lesson 8||JUnit ErrorCollector — What is, Example and Benefits|
|👉 Lesson 9||JUnit Parameterized Test — Learn using @Parameters Example|
|👉 Lesson 10||TestNG Vs JUnit — What’s the Difference?|
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.