0% found this document useful (0 votes)
3 views4 pages

Unit Testing

The document compares Unit Testing and Integration Testing, highlighting their purposes, focuses, scopes, dependencies, speeds, error detection, environments, and tools used. Unit Testing tests individual components in isolation, while Integration Testing verifies interactions between multiple components. It also introduces JUnit as a Java testing framework and Mockito for creating mock objects to facilitate unit testing.

Uploaded by

mohammadnazila4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Unit Testing

The document compares Unit Testing and Integration Testing, highlighting their purposes, focuses, scopes, dependencies, speeds, error detection, environments, and tools used. Unit Testing tests individual components in isolation, while Integration Testing verifies interactions between multiple components. It also introduces JUnit as a Java testing framework and Mockito for creating mock objects to facilitate unit testing.

Uploaded by

mohammadnazila4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit Testing vs Integration Testing

Ecommerce Project

Module ---Order Module1---Payment Gateway

Placing order ----Order+Payment Gateway===has to happen then only


my process will be completed ----Integration Testing

Unit Testing ---Order module(unit)----alone if you are testing unit testing

Aspect Unit Testing Integration Testing

To test individual
To test interactions between
Purpose components or functions
multiple components/modules.
in isolation.

Ensures that a single unit Ensures that combined units


Focus (class/function) works work together
correctly. [Link](add…

Narrow — focuses on one Broader — involves multiple


Scope
small piece of code. modules or systems.

Uses mocks/stubs/fakes to Uses real dependencies or


Dependenci
isolate the unit under minimal mocks to check real
es
test. integration.

Slower, as it may involve DB,


Speed Very fast (milliseconds).
APIs, file I/O, etc.

Detects issues in module


Error Detects logic errors in
interactions, data flow, or API
Detection individual functions.
mismatches.

Requires configured
Environmen
Local, minimal setup. environments (e.g., database,
t
network, services).

Performed
Developers. Developers and testers (QA).
By

Postman, REST Assured, JUnit


JUnit, pytest, NUnit, xUnit,
Tools with Spring Boot, pytest +
TestNG, Mockito.
requests, etc.
Sample for UnitTest and IntegrationTest

Recommended Test
Type

Use Case

Validating pure logic (calculations, conditions) Unit Testing

Verifying data flow between classes/modules Integration Testing

Ensuring external systems (DB, API, file)


Integration Testing
connect properly

Ensuring refactoring doesn’t break internal


Unit Testing
logic

Both (unit first, then


CI/CD pipelines before deployment
integration)

What is JUnit?

JUnit is a Java testing framework used to write and run unit tests.
Current versions:

 JUnit 5 (Jupiter) → latest and recommended

 JUnit 4 → older but still widely used

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

 Platform → runs tests.

 Jupiter → supports writing tests (new annotations).

 Vintage → backward compatibility with JUnit 3/4.

Unit Testing - Introduction to Mockito

What is Mockito?

Mockito is a mocking framework for Java used in unit testing.


It allows you to create mock objects (fake versions of dependencies) so
you can test a class in isolation — without connecting to databases,
APIs, or files.
Payment Gateway ---testing will they the original API?

Database ---localhost assumption /fake username do the database

Why Use Mockito?

In real-world projects, classes often depend on others:

 A UserService calls a UserRepository (which talks to a DB).

 A PaymentService calls a PaymentGateway (which talks to an


external API).

You don’t want to hit a real DB or API during a unit test — it’s slow and
unreliable.
Instead, you mock those dependencies.

What is a Stub in Unit Testing?

A Stub is a dummy or fake implementation of a method or class that


returns predefined data. Add—logic ///fake implementation a+b
It’s mainly used to simulate the behavior of real components when
testing a specific module in isolation.

In short:

Stub = Controlled fake that returns fixed responses to method


calls.

Fake ---100 deposit ====balance 100 balance ==0

10 to 15 ==balance ==laks== 100 will be ouput

Why Use a Stub?

When you perform unit testing, your class might depend on other
classes or external systems (like databases, APIs, or services).
To test your class in isolation, you replace those dependencies with
stubs.

Integration ===AccountService

Example Scenario

Let’s say you’re testing a class UserService, which depends on


UserRepository to fetch data from a database.
You don’t want to connect to a real database during a unit test — so you
create a stub for UserRepository.

When to Use Stubs

When the real dependency:

 Is not yet implemented

 Is slow or costly to use (e.g., database, API)

 Returns unpredictable results

 Requires specific data setup

Entity class

Repository ---

Service ---implementation

Controller ---service

Entity

DAOInterface

DAOImplementation

Main

You might also like