Part 1 – What is Unit Testing

What is Unit Testing?
Unit testing simply verifies that individual units of code (mostly functions) work independently as expected. Usually, you write the test cases yourself to cover the code you wrote. Unit tests verify that the component you wrote works fine when we ran it independently.
A unit test is a piece of code written by a developer that executes a specific functionality in the code to be tested and asserts a certain behavior or state.

The percentage of code which is tested by unit tests is typically called test coverage.

A unit test targets a small unit of code, e.g., a method or a class. External dependencies should be removed from unit tests, e.g., by replacing the dependency with a test implementation or a (mock) object created by a test framework.

Unit tests are not suitable for testing complex user interface or component interaction. For this, you should develop integration tests.

Unit Testing Method
It is performed by using the White Box Testing method. White-box testing is a method of testing software that tests internal structures or workings of an application.

When is it performed?
Unit Testing is the first level of software testing and is performed prior to Integration Testing.

levels of testing

Who performs it?
It is normally performed by software developers themselves or their peers.

How to perform it?
Almost always, the process of unit-testing is built into an IDE (or through extensions) such that it executes the tests with every compile. A number of frameworks exist for assisting the creation of unit tests (and indeed mock objects), often named fooUnit (cf. jUnit, xUnit, nUnit). These frameworks provide a formalized way to create tests.

As a process, test-driven development (TDD) is often the motivation for unit testing (but unit testing does not require TDD) which supposes that the tests are a part of the spec definition, and therefore requires that they are written first, with code only written to “solve” these tests.

Source Code
Download the source code of JUnit tutorial from below git repository :
unit-testing-and-integration-testing-with-spring-boot

Let’s go to our next tutorial where we will discuss below points :

Part 2 – What is JUnit? How to use JUnit?

In this tutorial, we will understand below topics –
– What is Junit?
– How to use Junit?
– Where should the test be located?
– How to execute JUnit tests?
1. Using mvn command
2. Using Eclipse Run as > JUnit Test

 – Eclipse Optimize Imports to Include Static Imports
JUnit Tutorial