Skip to content
pubscout

http_mock_adapter

0.6.1, released 3 years ago by lomsa.com

A simple to use mocking package for Dio intended to be used in tests.

$ flutter pub add http_mock_adapter
Source repository

What it does

It provides various types and methods to declaratively mock request-response communication for the Dio HTTP client. It allows simulating server behavior and network requests in test environments.

AI-written summary of version 0.6.1, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You are writing tests for a Dart or Flutter project using the Dio package for networking.
  • You need to declaratively mock HTTP requests and responses.

Skip it when

  • You are not using Dio for networking (e.g., you use the default http package).
  • You require complex production interceptors that aren't related to testing.

Strengths

  • Simple to use for mocking Dio requests in tests
  • Supports declarative request-response mocking
  • Includes features like response delays and exact body checks

Weaknesses

  • Strictly tied to the Dio package
  • Limited to testing use cases

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
4

Add as a dev_dependency in pubspec.yaml and import in test files.

import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';

void main() async {
  final dio = Dio(BaseOptions());
  final dioAdapter = DioAdapter(dio: dio);

  const path = 'https://example.com';

  dioAdapter.onGet(
    path,
    (server) => server.reply(
      200,
      {'message': 'Success!'},
      delay: const Duration(seconds: 1),
    ),
  );

  final response = await dio.get(path);
  print(response.data);
}

Alternatives

Packages that do the same job and could replace http_mock_adapter.

  • mockito

    If you prefer general-purpose mocking of classes instead of a specialized HTTP/Dio adapter.

  • mocktail

    If you prefer a mocktail-style mocking library without explicit code generation.

Works with

Packages commonly used together with http_mock_adapter: ones it builds on, ones built on it, and its own family.