Static objects

Using static variables, methods, and objects in code can be useful as these allow the objects to have the same value throughout all instances of the object, as only one copy of the object is created and put in memory. However, testing code containing statics, especially static methods, can create testing issues because you cannot override static methods in a subclass and using mocking frameworks to mock static methods is a very daunting task:

public static class LoanProcessor{    private static EligibilityChecker eligibilityChecker= new EligibilityChecker();        public static double CalculateCarLoanRate(Loan loan)    {        double rate=12.5F;        bool isEligible=eligibilityChecker.IsApplicantEligible(loan);        if(isEligible) rate=rate-loan.DiscountFactor; ...

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.