93

I have found that there are only 3 ways to unit test (mock/stub) dependencies that are static in C#.NET:

Given that two of these are not free and one has not hit release 1.0, mocking static stuff is not too easy.

Does that make static methods and such "evil" (in the unit testing sense)? And if so, why does resharper want me to make anything that can be static, static? (Assuming resharper is not also "evil".)

Clarification: I am talking about the scenario when you want to unit test a method and that method calls a static method in a different unit/class. By most definitions of unit testing, if you just let the method under test call the static method in the other unit/class then you are not unit testing, you are integration testing. (Useful, but not a unit test.)

Vaccano
  • 4,028
  • 5
  • 31
  • 37
  • 1
    You can't call static methods in C#? Thats, wow. – TheLQ Sep 21 '10 at 01:20
  • 3
    TheLQ: You can. I believe he is talking about not being able to test static methods because much of the time it touches static variables. Thus changing the state after and between test. –  Sep 21 '10 at 02:44
  • 26
    Personally I think you are taking definition of "unit" too far. "Unit" should be "smallest unit that makes sense to test in isolation". That may be a method, it may be more than that. If the static method has no state and well tested then having a second unit test call it is (IMO) not an issue. – mlk Nov 10 '10 at 11:46
  • 10
    "Personally I think you are taking definition of "unit" too far." No, its just that he's going with standard usage and you're making up your own definition. –  Aug 15 '11 at 14:26
  • 7
    "why does resharper want me to make anything that can be static, static?" Resharper doesn't _want_ you to do anything. It is merely making you aware that the modification is possible and _maybe_ desirable from a code analysis POV. Resharper is not a replacement for your own judgement! – Adam Naylor Feb 01 '12 at 12:39
  • Moles has been replaced with Fakes msdn.microsoft.com/en-us/library/hh549175(v=vs.110).aspx and available in visual studio 2012 – M – Michael Freidgeim Sep 08 '12 at 12:24
  • @acidzombie24. By that logic non-static methods are even worse as they can modify *both* static and non-static external state. – mike30 May 21 '13 at 18:43
  • 1
    possible duplicate of [So Singletons are bad, then what?](http://programmers.stackexchange.com/questions/40373/so-singletons-are-bad-then-what) – Jim G. May 21 '13 at 23:32
  • 1
    @mike30: Touching non-static is fine because the object should go away at the end of the test. –  May 22 '13 at 09:26
  • 4
    @acidzombie24. Regular methods can modify static state too so they would be just as "bad" as static methods. The fact they can also modify state with a shorter life cycle makes them even more dangerous yet. (I'm not in favor of static methods, but the point about state modification is a blow to regular methods too, even more so) – mike30 May 22 '13 at 13:04
  • @mike30: I didn't notice the post didnt talk about static **variables** wtf. This question is just silly now –  May 22 '13 at 18:37
  • 1
    @acidzombie24 - Why is it silly? Despite what you say, you still can not easily mock them. And you can do most anything in them (db access, write to file...). – Vaccano May 22 '13 at 20:34
  • @JimG. - Well, since that question was asked in 2011 and I asked this one in 2010, wouldn't it be a duplicate of this one (if they are in fact duplicates)? – Vaccano May 22 '13 at 20:35
  • What is a "unit"? A "unit" is completely arbitrary measurement for a piece of code. - Something we humans like to do. But anything can be considered a "unit", just depending on your point of view: your objects. Or a group of objects - or even your whole application can be considered a unit. Remember objects are just an abstraction of an organization of the code; they don't have to follow the same pattern as the "units". IE if design demands that "X" is a unit I make tests for X. However I don't care how X is implemented, if I implement if using objects A and B it still means X is a single unit – paul23 Mar 28 '18 at 18:17

11 Answers11

118

Looking at the other answers here, I think there might be some confusion between static methods that hold static state or cause side-effects (which sounds to me like a really bad idea), and static methods that merely return a value.

Static methods which hold no state and cause no side effects should be easily unit testable. In fact, I consider such methods a "poor-man's" form of functional programming; you hand the method an object or value, and it returns an object or value. Nothing more. I don't see how such methods would negatively affect unit testing at all.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 49
    The static method is unit testable, but what about the methods that call the static method? If the callers are in another class then they have a dependency that needs to be decoupled to do a unit test. – Vaccano Sep 21 '10 at 15:51
  • 24
    @Vaccano: But if you write static methods that do not hold state and have no side effects, they are more or less functionally equivalent to a stub anyway. – Robert Harvey Sep 21 '10 at 18:41
  • 22
    Simple ones maybe. But more complex ones can start throwing exceptions and having outputs that are not expected (and should be caught in the unit test for the static method, not in the unit test for the caller of the static method), or at least that is what I have been lead to believe in the literature I have read. – Vaccano Sep 21 '10 at 19:53
  • 23
    @Vaccano: A static method that has outputs or random exceptions has side-effects. – Tikhon Jelvis Dec 11 '11 at 03:55
  • 11
    @TikhonJelvis Robert *was* talking about outputs; and "random" exceptions shouldn't be a side effect, they are essentially a form of output. The point is, whenever you test a method that calls the static method, you are wrapping that method and all permutations of its potential output, and cannot test your method in isolation. – Nicole Aug 16 '12 at 23:04
  • @Vaccano you don't need an object for DI, you can use a delegate to inject a static method easily enough, an object may be prefereable in some circumstances though – jk. Nov 29 '12 at 17:55
  • 10
    As a fan of using static methods I previously lost this argument to a TDD-oriented developer. Let's say you have a static helper method that does a simple task (in a completely stateless mannner). That method is used in 15 different places throughout other methods, it also has a bug causing every other method that uses it to fail their tests. The primary goal of TDD is to isolate code for testing without side-effects. A static method that gets called inside another method will always be a side effect. – Evan Plaice May 21 '13 at 22:59
  • 6
    @EvanPlaice: Your argument is not compelling; non-static methods can also fail in multiple places. The difference being, non-static methods can often modify local object state, an undesirable quality which is not true of static methods. – Robert Harvey May 21 '13 at 23:01
  • 1
    (Cont) Of course, you could always test static methods before instance methods to fix the issue, but what about static methods that call other static methods. Forcing a constraint that tests must be run in a specific order is a serious code smell in the world of TDD. Theoretically, static methods are terrible for testing but in practice people very rarely ever provide 100% test coverage anyway so YMMV. – Evan Plaice May 21 '13 at 23:02
  • 2
    @EvanPlaice The ordering of tests is not necessary when testing static methods, even if they call other static methods. – Robert Harvey May 21 '13 at 23:05
  • 1
    @RobertHarvey The point is, non-static methods can be written in a way where they're completely isolated and free of side-effects. The benefit of static methods is that they're globally accessible. I'm not saying that's a bad thing but from a testing standpoint, it's a definite weakness that needs to be taken into consideration. – Evan Plaice May 21 '13 at 23:05
  • 6
    @EvanPlaice What weakness? The visibility of static methods has nothing to do with their safety. In practice, static methods are sequestered behind a namespace or static class anyway. But even if they weren't, they don't hold any state (if they are written properly), so it doesn't matter anyway. A static method's influence disappears completely once it returns. – Robert Harvey May 21 '13 at 23:06
  • let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/8871/discussion-between-evan-plaice-and-robert-harvey) – Evan Plaice May 21 '13 at 23:08
  • @Nicole Exactly. Robert, a method with a dependency on a static method cannot be unit-tested (easily anyway), and even if a static method merely returns a value without updating state or causing side-effects, not mocking it means you are performing an integration test. It is the same as copying/pasting all the code inside the static method into the method being tested, which I presume is what Nicole is referring to by "wrapping". Doing so also breaches the single responsibility principle. – Ash Jul 16 '19 at 03:16
  • @Ash: Calling it an integration test is making a distinction without a difference. A method is a method. Who is Nicole? – Robert Harvey Jul 16 '19 at 05:10
  • 1
    Nicole is the 5th person to comment on this answer. If a distinction can be made between two tests such that one is unit and one is integration, then there _is_ a difference and it's not just superficial. With a unit test, I can fix state and behaviour, and I can verify actions on my test-doubles. That last point is where the difference lies with mocking vs not mocking static methods (or any method not under test for that matter). Let me explain further (cont...) – Ash Jul 16 '19 at 07:29
  • 1
    @RobertHarvey If you don't mock a static method that simply returns a value, then the test on a method that consumes it might pass; but would it pass for the _right_ reasons? Potentially not. Consider a static method that's not bijective. You could have a situation where the method under test calls it with the wrong input and yet receive a returned value that makes the test pass. Now if I could mock the static method (a _unit_ test as opposed to an integration one), I could verify that this method was called with the right inputs and fail otherwise. – Ash Jul 16 '19 at 07:30
  • @Ash: I'm sorry, but I'm having difficulty understanding any of this. Why does a mock make any difference? Consider the function `int product(int x, int y) { return x * y; }` How does having mocks make any difference testing this method? What difference does it make if it is a static method or a non-static one? If you have methods that call other methods, *you have to unit test all of them,* whether they're static or non-static. If you want to call that an integration test, fine; we need those also. – Robert Harvey Jul 16 '19 at 14:11
  • 1
    @RobertHarvey An integration test is not a substitute for a unit test. Simply not mocking a static method and just calling it an integration test is not acceptable. Suppose I'm testing a method (MUT, Method Under Test) that calls the static method `product`, that you've provided. Let's say I verify the MUT by checking the value it returns is 6. It does return a value of 6 so my test passes. Now it's my expectation that the MUT returns the value returned by `product` and I expect that in this particular test, it would've invoked `product` with inputs x=2 and y=3. Did it though? – Ash Jul 17 '19 at 01:06
  • @Ash: OK. Now imagine doing the same thing with non-static methods. How do you make sure it works with those? – Robert Harvey Jul 17 '19 at 04:09
  • @RobertHarvey You verify the mocked `product` method to make sure it was called with x=2 and y=3; that would (among other checks) determine success of your test. – Ash Jul 17 '19 at 05:21
  • If I understand @Ash correctly, the point is most salient when we consider code may change, or may have outputs so complex we can't foresee all the permutations. In this case, bugs in a static method (bugs = producing unexpected output OR exceptions) may cascade failures to *unit* tests of its consumers. That's misleading because the consumer may actually be functioning correctly. Mocking complex or often-changing dependencies prevents such cascading red herrings and gives us more accurate information in unit tests of consumers. – Nicole Jul 19 '19 at 01:48
  • 1
    I last commented on this almost 7 years ago, but in that time more than once I've had to migrate a static method to a mockable member/DI method so that I could isolate expectations (and therefore failures) to the appropriate consumer unit tests. – Nicole Jul 19 '19 at 01:50
  • 1
    Oh, and worse, but rarer, than misleading cascading failures, is when your unit tests actually **succeed** only because of a faulty function of a dependency. (or worse: you didn't have thorough unit tests and your manual testing succeeded). Then, when someone fixes the dependency, appropriately, they unknowingly break things down the line. If the dependency had been mocked for unit tests (fixed input/expectations) instead of static, along with thorough unit tests, you would never have had this problem. – Nicole Jul 19 '19 at 01:53
30

You seem to be confusing static data and static methods. Resharper, if I remember correctly, recommends making private methods within a class static if they can be made so - I believe this yields a small performance benefit. It doesn't recommend making "anything that can be" static!

There is nothing wrong with static methods and they are easy to test (so long as they don't change any static data). For instance, think of a Maths library, which is good candidate for a static class with static methods. If you have a (contrived) method like this:

public static long Square(int x)
{
    return x * x;
}

then this is eminently testable and has no side effects. You just check that when you pass in, say, 20 you get back 400. No problem.

Rap
  • 273
  • 3
  • 9
Dan Diplo
  • 3,900
  • 1
  • 27
  • 30
  • 5
    What happens when you have another class that calls this static method? Seems simple in this case, but it is a dependency that cannot be isolated except with one of the three tools listed above. If you do not isolate it then you are not "unit testing" you are "integration testing" (because you are testing how well your different units "integrate" together. – Vaccano Sep 21 '10 at 15:54
  • 4
    Nothing happens. Why would it? The .NET framework is full of static methods. Are you saying you can't use these in any methods you want to unit test, either? – Dan Diplo Sep 21 '10 at 16:01
  • 3
    Well, if your code is at the production level/quality of the .NET Framework then sure, go ahead. But the whole point of unit testing is the test the unit, in isolation. If you are also calling methods in other units (be they static or otherwise) then you are now testing your unit plus its dependencies. I am not saying it is not a useful test, but not a "unit test" by most definitions. (Because you are now testing your unit-under-test and the unit that has the static method). – Vaccano Sep 21 '10 at 16:23
  • 10
    Presumably you (or others) have already tested the static method and shown that it works (at least as expected) before writing too much more code around it. If something breaks in the part you test next, that's where you should look first, not in the stuff you've already tested. – cHao Sep 21 '10 at 19:03
  • 7
    @Vaccano So how do Microsoft test the .NET Framework, then, eh? Many of the classes in the Framework reference static methods in other classes (such as `System.Math`) not to mention the abundance of static factory methods etc. Plus you'd never be able use any extension methods etc. The fact is, simple functions like this are fundamental to modern languages. You can test these in isolation (since they will generally be deterministic) and then use them in your classes without worry. It is not a problem! – Dan Diplo Sep 21 '10 at 19:06
  • 3
    @Dan: For reference, 20 squared is 400. :) – cHao Sep 21 '10 at 19:10
  • @cHao - Which just shows how import is to test your methods :) /blush – Dan Diplo Sep 21 '10 at 19:12
  • 2
    Well, this kind of discussion is why I asked the question. But while I don't know how MS tests the .NET framework, I do know that some of my static methods, while usually deterministic, are fairly complex. They take several parameters and have several output options. When I am unit testing other methods that rely on those complex static methods I would rather have them not called so that I can just test my method. – Vaccano Sep 21 '10 at 19:51
  • Makes me wish our programming languages had different keywords for `static` data and `static` methods. – dan04 Jan 29 '11 at 15:43
  • 1
    Making an instance method static may provide a performance benefit, but I think the real motivation is that it is confusing to have an instance method which does not depend on an instance. The classic example of where this would become confusing is `Thread.Sleep`. It also forces you to create an instance, though this is slightly less concerning for methods which are private. – Brian Nov 29 '12 at 18:26
  • 1
    @Vaccano Where exactly are those definitions? According to Martin Fowler and Uncle Bob this definition is nowhere as rigid as you'd have us believe. – Esben Skov Pedersen Mar 01 '15 at 20:49
20

If the real question here is "How do I test this code?":

public class MyClass
{
   public void MethodToTest()
   {
       //... do something
       MyStaticClass.StaticMethod();
       //...more
   }
}

Then, just refactor the code and inject as usual the call to the static class like this:

public class MyClass
{
   private readonly IExecutor _externalExecutor;
   public MyClass(_IExecutor executor)
   {
       _exeternalExecutor = executor;
   }

   public void MethodToTest()
   {
       //... do something
       _exetrnalExecutor.DoWork();
       //...more
   }
}

public class MyStaticClassExecutor : IExecutor
{
    public void DoWork()
    {
        MyStaticClass.StaticMethod();
    }
}
Sunny
  • 515
  • 3
  • 9
17

Statics aren't necessarily evil, but they can limit your options when it comes to unit-testing with fakes/mocks/stubs.

There are two general approaches to mocking.

The first one (traditional - implemented by RhinoMocks, Moq, NMock2; manual mocks and stubs are in this camp, too) relies on test seams and dependency injection. Suppose you're unit-testing some static code and it has dependencies. What happens often in the code designed this way is that statics create their own dependencies, inverting dependency inversion. You soon discover that you can't inject mocked interfaces into code under test that is designed this way.

The second one (mock anything - implemented by TypeMock, JustMock and Moles) relies on .NET's Profiling API. It can intercept any of your CIL instructions and replace a chunk of your code with a fake. This allows TypeMock and other products in this camp to mock anything: statics, sealed classes, private methods - things not designed to be testable.

There's an ongoing debate between two schools of thought. One says, follow SOLID principles and design for testability (that often includes going easy on statics). The other one says, buy TypeMock and don't worry.

azheglov
  • 7,177
  • 1
  • 27
  • 49
14

Check this out: "Static Methods are Death to Testability". Short summary of the argument:

To unit test you need to take a small piece of your code, rewire its dependencies and test it in isolation. This is hard with static methods, not only in the case they access global state but even if they just call other static methods.

Rafał Dowgird
  • 349
  • 1
  • 7
  • 35
    I don't keep global state or cause side-effects in my static methods, so this argument seems irrelevant to me. The article you linked makes a slippery slope argument that has no basis if static methods are confined to simple procedural code, acting in a "generic" way (like math functions). – Robert Harvey Sep 21 '10 at 14:53
  • 4
    @Robert Harvey - how do you unit test a method that uses a static method from another class (ie it has a dependency on a static method). If you just let it call it then you are not "unit testing" you are "integration testing" – Vaccano Sep 21 '10 at 15:48
  • 10
    Don't just read the blog article, read the many comments that disagree with it. A blog is just an opinion, not a fact. – Dan Diplo Sep 21 '10 at 16:10
  • 8
    @Vaccano: A static method with no side-effects or external state is functionally equivalent to a value. Knowing that, it's no different than unit testing a method that creates an integer. This is one of the key insights that functional programming gives us. – Steven Evers Nov 29 '12 at 17:59
  • 3
    Barring embedded systems work or an app whose bugs have potential to create international incidents, IMO, when "testability" drives architecture, you had it twisted before you adopted test-every-last-corner-methodologies in the first place. Also I'm tired of XP's version of everything dominating every conversation. XP is not an authority, it is an industry. Here's the original, sensible definition of unit testing: http://python.net/crew/tbryan/UnitTestTalk/slide2.html – Erik Reppen May 21 '13 at 20:35
  • The link http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/ is broken – Michael Freidgeim Feb 04 '21 at 00:25
  • Works for me as of 2021-02-23 – Rafał Dowgird Feb 23 '21 at 13:31
6

The simple truth rarely acknowledged is that if a class contains a compiler-visible dependency on another class, it cannot be tested in isolation from that class. You can fake up something that looks like a test, and will appear on a report as if it was a test.

But it will not have the key defining properties of a test; failing when things are wrong, passing when they are right.

This applies to any static calls, constructor calls, and any reference to methods or fields not inherited from a base class or interface. If the class name appears in the code, it is a compiler-visible dependency, and you can't validly test without it. Any smaller chunk simply is not a valid testable unit. Any attempt to treat it as if it were will have results no more meaningful than writing a little utility to emit the XML used by your test framework to say 'test passed'.

Given that, there are three options:

  1. define unit testing to be testing of the unit composed of a class and it's hard-coded dependencies. This works, providing you avoid circular dependencies.

  2. never create compile-time dependencies between classes you are responsible for testing. This works, providing you don't mind the resulting code style.

  3. don't unit test, integration test instead. Which works, providing it doesn't conflict with something else you need to use the term integration testing for.

soru
  • 3,625
  • 23
  • 15
  • 3
    There is nothing that says that a unit test is a test of a single class. It's a test of a single unit. The defining properties of unit tests are that they are fast, repeatable and independent. Referencing `Math.Pi` in a method does not make it an integration test by any reasonable definition. – sara Jun 02 '16 at 12:01
  • i.e. option 1. I agree that's the best approach, but it can be useful to know other people use the terms differently (reasonably or not). – soru Jun 02 '16 at 14:57
4

There's no two ways about it. ReSharper's suggestions and several useful features of C# would not be used as often if you were writing isolated atomic unit tests for all your code.

For instance, if you have a static method and you need to stub it out, you can't unless you use a profile based isolation framework. A call-compatible workaround is to change the top of the method to use lambda notation. For example:

BEFORE:

    public static DBConnection ConnectToDB( string dbName, string connectionInfo ) {
    }

AFTER:

    public static Func<string, string, DBConnection> ConnectToDB (dbName, connectionInfo ) {
    };

The two are call-compatible. Callers don't have to change. The body of the function remains the same.

Then in your Unit-Test code, you can stub this call like so (assuming it's in a class called Database) :

        Database.ConnectToDB = (dbName, connectionInfo) => { return null|whatever; }

Be careful to replace it with the original value after you're done. You can do that via a try/finally or, in your unit-test clean up, the one that gets called after every test, write code such as this:

    [TestCleanup]
    public void Cleanup()
    {
        typeof(Database).TypeInitializer.Invoke(null, null);
    }

which will re-invoke the static initializer of your class.

Lambda Funcs are not as rich in support as regular static methods, so this approach has the following undesirable side-effects:

  1. If the static method was an extension method, you have to change it to a non-extension method first. Resharper can do this for you automatically.
  2. If any of the data types of the static methods are an embedded-interop assembly, such as for Office, you have to wrap the method, wrap the type or change it to type 'object'.
  3. You can no longer use Resharper's change-signature refactoring tool.

But let's say you avoid statics altogether, and you convert this to an instance method. It's still not mockable unless the method is either virtual or implemented as part of an interface.

So in reality, anyone who suggests the remedy to stubbing static methods is to make them instance methods, they would also be against instance methods that are not virtual or part of an interface.

So why does C# have static methods? Why does it allow for non-virtual instance methods?

If you use either of these "Features", then you simply cannot create isolated methods.

So when do you use them?

Use them for any code that you don't expect anyone to ever want to stub out. Some examples: the Format() method of the String class the WriteLine() method of the Console class the Cosh() method of the Math class

And one more thing.. Most people won't care about this, but if you can about the performance of an indirect call, that's another reason to avoid instance methods. There are cases when it's a performance hit. That's why non-virtual methods exist in the first place.

zumalifeguard
  • 332
  • 1
  • 10
3

I see that after long time no one has yet stated a really simple fact. If resharper tells me that I can make a method static, it means a huge thing to me, I can hear his voice tell me: "hey, you, these pieces of logic are not current class's RESPONSIBILITY to handle, so it should stay out in some helper class or something".

Martijn Pieters
  • 14,499
  • 10
  • 57
  • 58
g1ga
  • 139
  • 2
  • 3
    I disagree. Most of the time when Resharper says I can make something static it's a little bit of code that was common to two or more methods in the class and thus I extracted it out to it's own procedure. Moving that to a helper would be meaningless complexity. – Loren Pechtel Nov 30 '12 at 03:21
  • 2
    I can see your point only if the domain is very simple and it is not suitable for future modification. Differently, what you call "meaningless complexity" it's good and human-readable design to me. Having an helper class with simple and clear reason to exists is in some way the "mantra" of SoC and Single Responsibility principles. In addition, considering that this new class becomes a dependency for the main one, it has to expose some public members, and as a natural consequence it becomes testable in isolation and easy to mock when acts as a dependency. – g1ga Nov 30 '12 at 10:30
  • 1
    Not relevant to the question. – Igby Largeman Mar 12 '14 at 22:28
3
  1. I believe it's partly because static methods are "faster" to call than instance methods. (In quotes because this smells of micro optimization) see http://dotnetperls.com/static-method
  2. It's telling you that it doesn't need state, therefore could be called from anywhere, removing the instatiation overhead if that's the only thing someone needs.
  3. If I want to mock it, then I think it's generally the practice that it's declared on an interface.
  4. If it's declared on an interface then R# won't suggest you make it static.
  5. If it's declared virtual, then R# won't suggest you make it static either.
  6. Holding state (fields) statically is something that should always be considered carefully. Static state and threads mix like lithium and water.

R# isn't the only tool that will make this suggestion. FxCop/MS Code Analysis will also do the same.

I would generally say that if the method is static, generally it should be testable as is as well. That brings some design consideration and probably more discussion than I have in my fingers right now, so patiently awaiting the down votes and comments... ;)

MIA
  • 5,264
  • 19
  • 29
  • Can you declare a static method/object on an interface? (I do not think so). Ether way, I am referring to when your static method is called by the method under test. If the caller is in a different unit then the static method needs to be isolated. This is very hard to do with out one of the three tools listed above. – Vaccano Sep 21 '10 at 15:56
  • 1
    No you cannot declare static on an interface. It would be meaningless. – MIA Sep 21 '10 at 17:24
2

If the static method is called from inside other method, it is not possible to prevent or substitute such a call. This means, these two methods make a single Unit; Unit test of any kind tests them both.

And if this static method talks to Internet, connects databases, shows GUI popups or otherwise converts the Unit test into complete mess, it just does without any easy work around. A method that calls such the static method is not testable without refactoring, even if it has lots of purely computational code that would highly benefit from being unit tested.

h22
  • 905
  • 1
  • 5
  • 15
0

I believe that Resharper is giving you guidence and apply the coding guidelines it has been setup with. When I have used Resharper and it has told me that a method should be static it is bound to be on a private method that does not act on any instance variables.

Now as for testablity this scenario shouldn't be an issue as you shouldn't test private methods anyway.

As for the testability of static methods that are public then unit testing does become hard when static methods touch static state. Personally I would keep this to a minimum and use static methods as pure functions as much as possible where any dependencies are passed into the method which can be controlled via a test fixture. However this is a design decision.

aqwert
  • 499
  • 2
  • 6
  • I am referring to when you have a method under test (not static) that calls a static method in another class. That dependency can only be isolated with one the three tools listed above. If you do not isolate it then you are integration testing, not unit testing. – Vaccano Sep 21 '10 at 15:52
  • Accessing instance variables inherently means it can't be static. The only state a static method can possibly have is class variables and the only reason that should happen is if you're dealing with a singleton and thus don't have a choice. – Loren Pechtel Nov 30 '12 at 03:25