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

  • SEARCH
  • Home
  • 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 6775771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:56:47+00:00 2026-05-26T15:56:47+00:00

I’ve read most SO related questions ( here , here and there ). The

  • 0

I’ve read most SO related questions ( here, here and there).
The last question proposes four alternatives to make code which calls static methods unit-testable.
I want to ask about my particular case: We have a “business logic layer” or “rules” project which contains 45 static classes (no state, just static methods). Moreover, they are not easily testable by themselves: most of them access the database and file system. It’s not that bad, anyway: to access the database, they use the unique instance of some Mapper class (all Mappers are Singletons).
Whenever I try to unit test something, I run into this wall. The biggest problem is that this is very, very important code, and changes to it should be planned very carefully.
My question: How should I go about making this more unit testable? Should I write 45 interfaces and use dependency injection? Even so, how do I stub/mock Mappers?

PS: I’ve been reading Michael Feathers’ “Working with Legacy Code”, so direct references are welcome (other books too 🙂

Edit: Since some people said solutions might be platform-dependent, I’m working on .NET (C# and some VB.NET)

  • 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-26T15:56:48+00:00Added an answer on May 26, 2026 at 3:56 pm

    The current situation is probably that no one dares to change anything in the code because it might break in unexpected ways. Make sure that everyone understands that you are improving the situation: Your changes might break the code but unlike before, those breakages will be found and once they have been found, they will be fixed forever.

    That said, the next step depends on your experience and your credit with the team. If you want to play safe, use code like this (Java syntax):

    Mapper {
        public static Mapper INSTANCE = new Mapper(); // NEW code
    
        protected void doImpl() { // NEW CODE
            ... code copied from impl()... // OLD code, NEW PLACE
        }
    
        public static void impl() { // OLD code
            INSTANCE.doImpl(); // NEW code
        }
    
        // OLD code ...
    }
    

    This is a very simple change which allows you to overwrite INSTANCE from your tests. For production code, you don’t do anything and the default will make the code behave exactly like before.

    That way, you can replace one method at a time. You can stop following this path at any time – each change takes only a couple of minutes and it’s a refactoring: The code does exactly what it did before. Since each change is so small and can’t break anything, you can replace one method, write all the unit tests that you couldn’t write before, rinse, repeat. Lastly, if you don’t want/need to rework all static methods, this approach gives you all the leeway you could ask for.

    In a second step, you can introduce DI or any other technology which will make you happy. The advantage of this approach: When you come to the complex changes, you will already have unit tests that’ll protect you.

    If you started with DI, you’d have to change a lot of code in all kinds of places – without proper unit tests that could protect you.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.