惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
T
ThreatConnect
J
Java Code Geeks
博客园 - 司徒正美
A
Arctic Wolf
T
True Tiger Recordings
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
The Register - Security
The Register - Security
S
Securelist
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
I
Intezer
P
Privacy & Cybersecurity Law Blog
Scott Helme
Scott Helme
K
Kaspersky official blog
博客园 - 聂微东
Last Week in AI
Last Week in AI
V
V2EX
小众软件
小众软件
F
Fox-IT International blog
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
F
Future of Privacy Forum
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
T
Threatpost
I
InfoQ
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Tor Project blog
G
GRAHAM CLULEY
D
DataBreaches.Net

DEV Community

HTB — MonitorsFour | Writeup Fr 97. Embeddings and Vector Search: Semantic Search That Works Deep Dive: Building "Gravity Paint" - A Tactile Physics Instrument with React, Matter.js, and p5.js LeetCode Solution: 5. Longest Palindromic Substring kovax-react 0.8: Tailwind v4 preset, FormField adapters, ColorModeScript, and Storybook I built an AI résumé tool that refuses to lie about your experience The hat Azure Entra ID User & Role Management — Step-by-Step Practical Guide With A Simple Excercise The AI-Native Company: How a Single Founder Can Build Global Organizations Powered by AWS and an Ecosystem of Artificial Intelligences Building a Lightweight Remote MCP Knowledge Base on Cloudflare Workers Why I built Trinavo for the MENA merchants Western platforms ignore The N+1 Query That Killed Our Database, And How I Fixed It Docstrings vs Markdown Docs: What Should Developers Actually Write? Training Data Provenance: The Manifest Diff That Explains the Hash Add SVGIcons MCP to Claude Code and Find SVG Icons from Your Terminal 3 CLI Tools You Can Buy with Crypto — No KYC, No Subscriptions COSS Weekly: OpenClaw competitor NanoClaw Raises $12M, Dust Raises $40M, Sonar Acquires Gitar, and more How to know if you actually need mobile proxies (without buying any) Building Cursor for Community: A Buildathon Built on Time Pressure How we built a PII masking layer for LLM APIs — local detection, reversible tokens, one line to integrate Why MLFQ Was Way Ahead of Its Time Add Runtime Limits to Claude Agent Workflows I Built a Prompt Injection Detector with 98% Recall on Unseen Attacks. Here's Why Data Beat Architecture. 8 Vite Config Options Every Developer Should Know (Vite 8) Feature Flags That Forgot to Leave Why Trust Infrastructure Is Becoming the Hidden Layer of Donation Platforms XyPriss: Rethinking Core Performance and Zero-Trust Architecture in Modern Backends Designing Configuration for Scalable Treasure Hunts SSH Login Delays: The 10-Second Wait That Drives Us Crazy Building Production Multi-Agent Workflows in n8n: What 50 Deployments Taught Us A 3-layer memory system that gives Claude Code persistent context across sessions. Trishul SNMP Suite 2.0.1: Better MIBs, Traps, and SNMP Labs How I built a production AI SaaS as a solo developer Auto-labelling 1.2M robotics frames with VLMs: a failover story India’s Laws Were Not Built for AI — And Courts Are Filling the Gap skill-insp: A Skill That Scores Other Skills Clprolf Minimalist Messaging in the Age of AI What's actually in a good .cursorrules file? I built 10 of them — here's what I learned Building Strong Python Basics – Loops, Functions and Logic How to Choose the Right Tech Stack for Your Project I built a free multi-tab JSON editor — here's what I learned HTTP Headers Every Developer Should Know (2026) Building Cross-Platform Digital Products: Challenges and Best Practices Data Privacy in the Age of AI: How Product Teams Can Build Trust with Users What Would WordPress Look Like If It Were Designed Today? Why Backup Success Does Not Mean Database Recoverability Local AI Office Assistant That Never Sends Your Documents to the Cloud Building TaskForge: Translating Enterprise Chaos into an Open-Source Scheduler Tesla P40 in a Homelab: 24GB of Inference on a Budget Llama 4: Meta's Latest — Scout, Maverick, and the MoE Revolution George Hotz called AI code 'slop.' He's half right. Como Construir um Fluxo de Trabalho Baseado em Engenharia de Prompt e Automação We Audited Our Agent Tool-Call Traces. Half Our Eval Data Was Garbage. The Hidden Cost of Downtime: How SRE Error Budgets Protect National Economic Infrastructure Getting started with openHUMANS can be an exciting venture for developers looking to create innovative applications in the realm of human-ce Stack Overflow: A Powerful Community for Developers and Learners From Language Models to Humanoid Minds ✨ Road to Senior #2: How Computers Think in Numbers Why LLM debugging fails on fragmented repository context How to Deploy a LangGraph Agent on AWS Bedrock AgentCore An outreach kit for solo founders whose drafts can't hallucinate Open Satchel is live Amy Kwalwasser and the Growing Importance of Quantum Risk Modeling I Built ShellReq - A Native API Client for VS Code & Terminal If Microsoft and Uber can't afford AI coding, what chance do the rest of us have? MADCAP: Building a Multi-Agent Debate CLI That Argues With Itself So You Don't Have To Why most AI fails at IDOR (and how AMAS fixes it with causal reasoning) How to Audit a Laravel Codebase You've Inherited LangGraph 워크플로우 템플릿 (v34) BugBench: a developer origin story and practical guide for VS Code / Kiro users A solution to messy token systems for Next.js A NestJS reference app that proves the nest-native stack under realistic backend pressure Observability for AI Systems: Monitoring Drift, Hallucinations, and Reliability in Production I Thought “Data Analyst” Was the Whole Game… Then I Entered the Data Avengers Office 👀 Create and configure network security groups How to analyze the cost of Kafka? How I Shipped 2,500+ Commits With AI Agents Using a 12-Phase Workflow [Boost] We built MDCMS, a Markdown-first CMS for teams using AI agents Zero Heap Allocations at 1.18 GB/s: Deep Dive into ForgeZero 4.0.x The Minimum Viable Test Suite for Working with Agents Why Perplexity Started Citing My Blog: 5 Changes That Actually Worked Sync Supabase via OAuth: No Connection String Needed I asked three AI models the same API question. Only one had it right. Implementing Saga Pattern With Lambda Durable Function Why does AI forget what you said (and how to fix it) I built a daily Wordle-style game for AI tools - Here's how Mapping Polish company structures: querying KRS direct via API Built tmpdrop — a tiny self-hosted ephemeral file drop Running Local LLM - 0$ Personal Agentic AI Assistant - Part 3 LLD Object-Oriented Design: Interfaces & Abstract Classes (Designing Contracts) The Smaller Ship: Vitalik, the Ethereum Foundation's Restructuring, and What It Leaves for Investors Looking for 4 people to build something weird with me Building a Local-Only RAG System with Ollama and TypeScript The False Positive Tax: a 1:1 TP:FP analysis of eslint-plugin-security What's new in Data Preprocessor 1.5.x — R codegen, Robust Scaler, and a deadlock post-mortem How I self-hosted my Flask app on an old laptop for almost free I built a free DSA interview prep site because I was tired of the existing options I built an AI agent that migrates Next.js Pages Router to App Router
ABAP Unit Testing with Test Doubles and Mocking Frameworks: A Senior Architects Guide to Isolating Dependencies in SAP S/4HANA
Oktay Ates · 2026-05-26 · via DEV Community

If yoy have been writing ABAP unit tests for a while, you have probably hit the same wall I did years ago: your tests pass in isolation but become brittle the moment they touch a database table, a remote function call, or a business object manager. The root cause is almost always the same—uncontrolled dependencies. Learning how to isolate those dependencies using ABAP test doubles and mocking frameworks is one of the most impactful skills a senior SAP developer can develop. This guide walks you through that skill in depth, with real-world patterns you can apply immediately.

Before we dive in, if you haven't read my earlier articles on ABAP unit testing in SAP S/4HANA and writing reliable, maintainable tests for real SAP systems, I’d recommend starting there. This article builds on those foundations and takes you to the next level: true dependency isolation.

Why Dependency Isolation Matters More Than You Think

Let me paint a picture you will recognize. You write a clean ABAP class—ZCL_ORDER_PROCESSOR—and it works perfectly. You even write a unit test. But your test calls the real BAPI_SALESORDER_CREATEFROMDAT2, which creates actual sales orders in your development client. Your test suite runs for 40 seconds. A colleague runs it on a different system and it fails because the customer number doesn't exist there.

That's not a unit test. That's an integration test wearing a unit test costume.

True unit tests must be:

  • Fast — Milliseconds, not seconds

  • Isolated — No database, no RFC, no file system

  • Deterministic — Same result every time, on every system

  • Self-contained — No setup data required outside the test class

Achieving this requires controlling your dependencies—and that means test doubles.

The Four Types of Test Doubles You Need to Know

The terminology here comes from Gerard Meszaros; classic work on xUnit patterns, and it maps cleanly to ABAP:

1. Dummy Objects

Passed around but never actually used. Often used to fill parameter lists when the value doesn’t matter for the test case at hand.

2. Stubs

Return canned answers to calls made during the test. They don't verify anything—they just provide predefined responses. Use these when your class under test reads data from a dependency.

3. Mocks

Pre-programmed with expectations about which calls they should receive. They verify behavior—meaning they fail your test if an expected method was not called or was called with wrong parameters.

4. Fakes

Working implementations that take shortcuts—an in-memory repository instead of a real database, for example. These are the most sophisticated and most useful in complex ABAP scenarios.

In practice, the ABAP Test Double Framework blurs these categories a bit, but understanding the conceptual distinction shapes how you design your tests.

Setting Up Your Architecture for Testability

You can't retrofit test doubles onto poorly structured code. The single most important prerequisite is dependency injection. If your class instantiates its own dependencies internally, you have no way to replace them in tests.

Here’s the pattern I use in every project:


"--- Interface Definition ---
INTERFACE zif_order_repository.
  METHODS:
    get_open_orders
      IMPORTING iv_customer TYPE kunnr
      RETURNING VALUE(rt_orders) TYPE ztorder_tab
      RAISING   zcx_order_not_found.
ENDINTERFACE.

"--- Production Implementation ---
CLASS zcl_order_repository DEFINITION PUBLIC CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES zif_order_repository.
ENDCLASS.

CLASS zcl_order_repository IMPLEMENTATION.
  METHOD zif_order_repository~get_open_orders.
    SELECT * FROM ztorders
      WHERE customer = @iv_customer
        AND status  = 'OPEN'
      INTO TABLE @rt_orders.
    IF rt_orders IS INITIAL.
      RAISE EXCEPTION TYPE zcx_order_not_found.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

"--- Consumer Class (Dependency Injected) ---
CLASS zcl_order_processor DEFINITION PUBLIC CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS:
      constructor
        IMPORTING io_repository TYPE REF TO zif_order_repository,
      process_customer_orders
        IMPORTING iv_customer TYPE kunnr
        RETURNING VALUE(rv_count) TYPE i
        RAISING   zcx_order_not_found.
  PRIVATE SECTION.
    DATA mo_repository TYPE REF TO zif_order_repository.
ENDCLASS.

CLASS zcl_order_processor IMPLEMENTATION.
  METHOD constructor.
    mo_repository = io_repository.
  ENDMETHOD.

  METHOD process_customer_orders.
    DATA(lt_orders) = mo_repository->get_open_orders( iv_customer ).
    rv_count = lines( lt_orders ).
    " ... additional processing logic ...
  ENDMETHOD.
ENDCLASS.

Enter fullscreen mode Exit fullscreen mode

Notice that ZCL_ORDER_PROCESSOR depends on the interface ZIF_ORDER_REPOSITORY, not the concrete class. This is the foundation that makes every test double strategy possible. This principle aligns directly with the clean code and refactoring principles I have covered previously—clean architecture and testability go hand in hand.

Using the ABAP Test Double Framework (TDF)

SAP’s built-in Test Double Framework, available since ABAP 7.50, lets you create mock objects for any interface without writing a separate test class. It’s powerful, but it requires you to understand its configuration model.

Step 1: Create the Test Double


CLASS ltc_order_processor_test DEFINITION FINAL FOR TESTING
  DURATION SHORT RISK LEVEL HARMLESS.

  PRIVATE SECTION.
    DATA:
      mo_cut        TYPE REF TO zcl_order_processor,
      mo_repo_mock  TYPE REF TO zif_order_repository.

    METHODS:
      setup,
      test_returns_correct_order_count FOR TESTING,
      test_raises_exception_when_no_orders FOR TESTING.
ENDCLASS.

CLASS ltc_order_processor_test IMPLEMENTATION.

  METHOD setup.
    " Create the test double using TDF
    mo_repo_mock = CAST zif_order_repository(
      cl_abap_testdouble=>create( 'ZIF_ORDER_REPOSITORY' )
    ).

    " Inject it into the class under test
    mo_cut = NEW zcl_order_processor( mo_repo_mock ).
  ENDMETHOD.

Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Stub Behavior


  METHOD test_returns_correct_order_count.
    " Arrange: define what the mock returns
    DATA(lt_fake_orders) = VALUE ztorder_tab(
      ( order_id = '1000000001' customer = '0000001000' status = 'OPEN' )
      ( order_id = '1000000002' customer = '0000001000' status = 'OPEN' )
      ( order_id = '1000000003' customer = '0000001000' status = 'OPEN' )
    ).

    cl_abap_testdouble=>configure_call( mo_repo_mock
      )->returning( lt_fake_orders
      )->when_invoked( ).

    mo_repo_mock->get_open_orders( '0000001000' ).

    " Act
    DATA(lv_count) = mo_cut->process_customer_orders( '0000001000' ).

    " Assert
    cl_abap_unit_assert=>assert_equals(
      act = lv_count
      exp = 3
      msg = 'Expected 3 open orders for customer 1000'
    ).
  ENDMETHOD.

Enter fullscreen mode Exit fullscreen mode

Step 3: Verify Exception Handling


  METHOD test_raises_exception_when_no_orders.
    " Configure mock to raise the exception
    cl_abap_testdouble=>configure_call( mo_repo_mock
      )->raising( NEW zcx_order_not_found( ) 
      )->when_invoked( ).

    mo_repo_mock->get_open_orders( '9999999999' ).

    " Act & Assert
    TRY.
      mo_cut->process_customer_orders( '9999999999' ).
      cl_abap_unit_assert=>fail(
        msg = 'Expected ZCX_ORDER_NOT_FOUND to be raised'
      ).
    CATCH zcx_order_not_found.
      " Test passes  exception was correctly propagated
    ENDTRY.
  ENDMETHOD.

ENDCLASS.

Enter fullscreen mode Exit fullscreen mode

This is clean, readable, and—critically—requires zero database access. The test runs in milliseconds and produces the exact same result on every system, every time.

Building a Fake Repository for Complex Scenarios

Sometimes the TDF's stub configuration becomes verbose when you have many method calls with different parameter combinations. In those cases, I prefer writing a fake implementation—a lightweight in-memory version of the real dependency.


"--- Fake Repository (defined inside the test include) ---
CLASS ltc_fake_order_repository DEFINITION FINAL.
  PUBLIC SECTION.
    INTERFACES zif_order_repository.
    DATA mt_orders TYPE ztorder_tab.  " Test data injected directly
ENDCLASS.

CLASS ltc_fake_order_repository IMPLEMENTATION.
  METHOD zif_order_repository~get_open_orders.
    rt_orders = FILTER #( mt_orders
      USING KEY primary_key
      WHERE customer = iv_customer
        AND status   = 'OPEN' ).
    IF rt_orders IS INITIAL.
      RAISE EXCEPTION TYPE zcx_order_not_found.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

Enter fullscreen mode Exit fullscreen mode

Using the fake in a test becomes extremely readable:


  METHOD test_with_fake_repository.
    " Arrange
    DATA(lo_fake_repo) = NEW ltc_fake_order_repository( ).
    lo_fake_repo->mt_orders = VALUE #(
      ( order_id = '2000000001' customer = '0000002000' status = 'OPEN' )
      ( order_id = '2000000002' customer = '0000002000' status = 'OPEN' )
      ( order_id = '2000000003' customer = '0000002000' status = 'BLOCKED' ) " Should not be counted
    ).

    DATA(lo_cut) = NEW zcl_order_processor( lo_fake_repo ).

    " Act
    DATA(lv_count) = lo_cut->process_customer_orders( '0000002000' ).

    " Assert  only OPEN orders should be counted
    cl_abap_unit_assert=>assert_equals(
      act = lv_count
      exp = 2
      msg = 'Only OPEN orders should be processed'
    ).
  ENDMETHOD.

Enter fullscreen mode Exit fullscreen mode

Fakes are particularly powerful when you need to test multiple method calls, stateful behavior, or complex filtering logic. They are slightly more work to write but dramatically more flexible. The alignment here with good exception handling design is important too—see my detailed series on class-based exception architecture to understand how to propagate errors cleanly from your fakes.

Handling CDS Views and Database Access in Tests

One of the trickiest scenarios in modern SAP development is testing code that reads from CDS views. If your class calls a CDS view directly via inline SELECT, you can't easily mock it without architectural changes.

The solution is to wrap CDS access in a repository interface—exactly the pattern shown above. Your production repository queries the CDS view; your test uses a fake or mock. This approach integrates naturally with the layered CDS architecture I’ve described in the CDS Views performance optimization series—the interface boundary between your CDS consumption layer and business logic layer is also your testability boundary.

If you absolutely must test with real CDS data, use ABAP's test seams (introduced in 7.40) as a last resort—but treat them as a code smell that signals a refactoring opportunity.

Common Mistakes and How to Avoid Them

Mistake 1: Testing the Mock, Not the Logic

A surprisingly common mistake is writing a test where the only thing being verified is that the mock returns what you told it to return. Ask yourself: am I testing the behavior of my class under test, or just the wiring of my test setup?

Mistake 2: Over-specification

Configuring your mock to verify that a method was called with exactly these parameters in exactly this order makes your tests fragile. Mock behavior, not implementation details. If the internal call sequence of a method isn't part of its public contract, don't assert on it.

Mistake 3: Sharing Mutable State Between Tests

Always create fresh test doubles in the SETUP method. If one test modifies the state of a fake repository and the next test inherits that state, you get mysterious, order-dependent failures. This is one of the hardest bugs to diagnose in test suites.

Mistake 4: Ignoring the Refactoring Cost

Retrofitting dependency injection into tightly coupled legacy code is hard. Be honest about the cost. I’ve seen teams spend more time fighting legacy code structure than writing actual tests. Prioritize testability in new code; tackle legacy code incrementally with the techniques in my clean code refactoring guide.

Structuring Your Test Classes for Maintainability

As your test suite grows, organization becomes critical. Here is the structure I recommend:

  • One test class per behavior scenario — Don't cram all tests into a single LTC_ class. Use separate local test classes for distinct behaviors (happy path, error cases, edge cases).

  • Descriptive method names — test_returns_zero_when_customer_has_no_open_orders is far more useful than test_01 when diagnosing failures at 2am.

  • Arrange-Act-Assert structure — Every test method should follow this pattern. It makes tests readable at a glance and forces you to think clearly about what you’re actually testing.

  • Short test methods — If a test method exceeds 20-25 lines, it's probably testing too many things. Split it.

Key Takeaways

After years of building and reviewing ABAP systems, here is what I can tell you with confidence:

  • Test doubles are not a testing luxury—they’re a design quality indicator. If you can't mock a dependency, your architecture has a coupling problem.

  • The ABAP Test Double Framework is production-ready and sufficient for most scenarios. Learn it deeply before reaching for more complex solutions.

  • Fake implementations are your best friend when testing complex stateful interactions. Don’t be afraid to write them—they’re part of your test suite, not production code overhead.

  • Dependency injection via constructor is the single most important pattern for ABAP testability. Apply it consistently in all new code.

  • Clean test code is just as important as clean production code. Treat your test classes with the same architectural discipline you apply to your business classes.

What's Next?

In the next article in this testing series, I will tackle one of the most requested topics from my readers: testing RAP business objects and ABAP RESTful application handlers—where the frameworks built-in test infrastructure opens up some genuinely exciting possibilities. Stay tuned.

In the meantime, pick one class in your current project that has no tests. Identify its external dependencies. Draw the interface boundaries. Then write your first test double. That first step is almost always the hardest—and the most rewarding.

Have questions about a specific mocking scenario you are struggling with? Drop a comment below—I read every one and do my best to respond with concrete advice.