Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 999389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:20:42+00:00 2026-05-16T07:20:42+00:00

I’ve used the self-shunt unit testing pattern a few times over the years. As

  • 0

I’ve used the self-shunt unit testing pattern a few times over the years. As I was explaining it to someone recently they argued that it violated the SRP. The argument is that the test class can now be changed for one of two reasons: when the test changes, or when the method signature on the interface that the test is implementing changes. After thinking about it for a while, it seems like this is a correct assessment, but I wanted to get other peoples’ opinions. Thoughts?

Reference:
http://www.objectmentor.com/resources/articles/SelfShunPtrn.pdf

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:20:42+00:00Added an answer on May 16, 2026 at 7:20 am

    My take on this is that the test class technically violates the SRP, but it doesn’t violate the spirit of SRP. The alternative to self-shunting is to have a mock class separate from the test class.

    With the separate mock class you might think that it’s all self contained and satisfies the SRP, however the semantic coupling to the mock class’s attributes is still there. So, really, we didn’t achieve any meaningful separation.

    Taking the example out of the PDF:

    public class ScannerTest extends TestCase implements Display
    {
      public ScannerTest (String name) {
        super (name);
      }
      public void testScan () {
        // pass self as a display
        Scanner scanner = new Scanner (this);
        // scan calls displayItem on its display
        scanner.scan ();
        assertEquals (new Item (“Cornflakes”), lastItem);
      }
      // impl. of Display.displayItem ()
      void displayItem (Item item) {
        lastItem = item;
      }
      private Item lastItem;
    }
    

    Now we make a Mock:

    public class DisplayMock implements Display
    {
      // impl. of Display.displayItem ()
      void displayItem (Item item) {
        lastItem = item;
      }
    
      public Item getItem() {
         return lastItem;
      }
      private Item lastItem;
    }
    
    public class ScannerTest extends TestCase
    {
      public ScannerTest (String name) {
        super (name);
      }
      public void testScan () {
        // pass self as a display
        DisplayMock dispMock = new DisplayMock();
        Scanner scanner = new Scanner (dispMock );
        // scan calls displayItem on its display
        scanner.scan ();
        assertEquals (new Item (“Cornflakes”), dispMock.GetItem());
      }
    }
    

    In practical terms (IMHO) the higher coupling of TestClass to DisplayMock is a greater evil than the violation of the SRP for TestClass. Besides, with the use mocking frameworks, this problem goes away completely.

    EDIT I have just encountered a brief mention of self-shunt pattern in Robert C. Martin’s excellent book Agile Principles, Patterns, and Practices in C#. Here is the snippet out of the book:

    We can accomplish this by using an abstract interface for the database. One implementation of this abstract interface uses the real database. Another implementation is test code written to simulate the behavior of the database and to check that the database calls are being made correctly. Figure 29-5 shows the structure. The PayrollTest module tests the PayrollModule by making calls to it and also implements the Database interface so that it can trap the calls that Payroll makes to the database. This allows PayrollTest to ensure that Payroll is behaving properly. It also allows PayrollTest to simulate many kinds of database failures and problems that are otherwise difficult to create. This is a testing pattern known as SELF-SHUNT, also sometimes known as mocking or spoofing.

    Figure 29-5. PayrollTest SELF-SHUNTs database

    So, the guy who coined the SRP (which is talked about in great detail in the same book) has no qualms using self-shunt pattern. In light of that, I’d say you are pretty safe from the OOP (Objected Orientated Police) when using this pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have text I am displaying in SIlverlight that is coming from a CMS
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.