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 7943039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:09:16+00:00 2026-06-04T00:09:16+00:00

Should a static class/method/property be used in a unit test development environment, given that

  • 0

Should a static class/method/property be used in a unit test development environment, given that there is no way to test it without introducing a wrapper that is again not testable?

Another scenario is that when the static members are used within the unit tested target, the static member cannot be mocked. Thus, you have to test the static members when the unit tested target is tested. You want to isolate it when the static member performs calculation.

  • 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-06-04T00:09:17+00:00Added an answer on June 4, 2026 at 12:09 am

    Testing static method is no different than testing any other method. Having static method as a dependency inside another tested module raises problems (as it’s been mentioned – you can’t mock/stub it with free tools). But if the static method itself is unit tested you can simply treat it as working, reliable component.

    Overall, there’s nothing wrong (as in, it doesn’t disrupt unit testing/TDD) with static methods when:

    • it is simple, input-output method (all kinds of “calculate this given that”)
    • it is reliable, by what we mean it’s either unit tested by you or comes from 3rd party source you consider reliable (eg. Math.Floor might be considered reliable – using it shouldn’t raise “Look out, it’s static!” warning; one might assume Microsoft does its job)

    When static methods will cause problems and should be avoided? Basically only when they interact with/do something you cannot control (or mock):

    • all kind of file system, database, network dependencies
    • other (possibly more complex) static methods called from inside
    • pretty much anything your mocking framework can’t deal with on regular terms

    Edit: two examples on when static method will make unit testing hard

    1

    public int ExtractSumFromReport(string reportPath)
    {
         var reportFile = File.ReadAllText(reportPath);
         // ...
    }
    

    How do you deal with File.ReadAllText? This will obviously go to file system to retrieve file content, which is major no-no when unit testing. This is example of static method with external dependency. To avoid that, you usually create wrapper around file system api or simply inject it as dependency/delegate.

    2

    public void SaveUser(User user)
    {
        var session = SessionFactory.CreateSession();
        // ...
    }
    

    What about this? Session is non-trivial dependency. Sure, it might come as ISession, but how do force SessionFactory to return mock? We can’t. And we can’t create easy to detemine session object either.

    In cases like above, it’s best to avoid static methods altogether.

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

Sidebar

Related Questions

When creating a repository class, eg. CustomerRepository, should my methods be static? Or should
What are the key uses of a Static Generic Class in C#? When should
I heard that static methods should use only static variables in java. But, main
In Java, static final variables are constants and the convention is that they should
How can I get the current Type in a static method that is defined
I have the following static method in an class called Article : public static
i am trying to create one dependency property in a class called "FocusBehaviour" that
How often should I use static methods generally? If I have like: Class1 _class34
I have a very simple yes no question: should static methods have same result
I need to mock a GrailsControllerClass interface. Instance should have a static variable defined.

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.