background

Telerik JustMock

Mock Static Classes, Methods and Properties

  • The powerful capabilities of the feature-rich JustMock framework allow you to mock static classes and calls to static members like methods and properties, set expectations and verify results.
  • Part of the fastest, most flexible and complete mocking tool for crafting unit tests.
  • Our award-winning support team is available to assist you with any issues.
Mock everything
  • Mocking Static Classes, Methods, and Properties Overview

    A static class is very similar in its nature to a non-static class. The difference between the two classes is that a static class cannot be instantiated. The new operator cannot create a variable of the class type. Because there is no instance variable, the class name itself should be used to access the members of a static class.

    The powerful capabilities of the feature-rich JustMock framework allow you to mock static classes and calls to static members like methods and properties, set expectations and verify results.

    To mock a static type in JustMock, you need to first implement the following setup:
    Mock.SetupStatic(typeof(MyStaticType));
  • Mocking Static Constructor

    The static constructor is used to initialize any static data or to perform specific actions only once. With JustMock, you have the control to decide whether to mock the static constructor  or execute the original static constructor.

    Mock.SetupStatic(typeof(MyStaticType), StaticConstructor.Mocked);
    The behavior of the static constructor to be executed only once remains.

    Mock static constructor documentation
  • Mocking Static Method

    To mock a static method, you must first setup the owner class for mocking.

    [TestMethod]
    public void ShouldMockStaticClassWithStrictBehavior()
    {
        var expected = 10;
     
        // ARRANGE
        // Getting the static instance(Foo) ready for mocking disregarding the constructor.
        // If we don't mock the constructor, a NotImplementedException will be thrown.
        Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
     
        // Arranging: When the Foo.Execute() method is called with 10, it should return expected.
        Mock.Arrange(() => Foo.Execute(10)).Returns(expected);
     
        // ACT
        var actual = Foo.Execute(10);
     
        // ASSERT
        Assert.AreEqual(expected, actual);
    }

    Mock a static method documentation
  • Mocking Static Property Getter

    Mocking a static property getter is as straightforward as mocking a static method. The following example shows how to mock the get function of a static property:

    public class Foo
    {
        static Foo()
        {
            throw new NotImplementedException();
        }
     
        public static int FooProperty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
     
    ...
     
    [TestMethod]
    public void ShouldArrangeStaticProperty()
    {
        var expected = 0;
     
        // ARRANGE
        // Getting the static instance(Foo) ready for mocking disregarding the constructor.
        // If we don't mock the constructor, a NotImplementedException will be thrown.
        Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
     
        // Arranging: When the static(Foo.FooProp_GET) property is called, it should return expected.
        Mock.Arrange(() => Foo.FooProperty).Returns(expected);
     
        // Assert
        Assert.AreEqual(expected, Foo.FooProp);
    }

    Mock a static property getter documentation
  • Mocking Static Property Set

    Mocking a static property setter works differently from mocking a static property getter. While the lambda function for a getter can be narrowed down to an expression, the function for a setter can be done only though an action and with a different API:

    public class Foo
    {
        static Foo()
        {
            throw new NotImplementedException();
        }
      
        public static int FooProperty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
     
    ...
     
    [TestMethod]
    public void ShouldFakeStaticPropertySet()
    {
        bool isCalled = false;
     
        // ARRANGE
        // Getting the static instance(Foo) ready for mocking disregarding the constructor.
        // If we don't mock the constructor, a NotImplementedException will be thrown.
        Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
     
        // Arranging: When the static(Foo.FooProp_SET) property is set to 10, it should assign true to isCalled instead.
        Mock.ArrangeSet(() => { Foo.FooProperty = 10; }).DoInstead(() => { isCalled = true; });
     
        // ACT
        Foo.FooProp = 10;
     
        // ASSERT
        Assert.IsTrue(isCalled);
    }

    Mock a static property setter documentation
  • Mocking Extension Methods

    Extension methods are a special kind of static methods. They are called as if they were instance methods on the extended type. Mocking extension methods is similar to mocking any instance method. The only difference is that we don’t need a Mock.Create<T>() call to initialize the class for mocking as extension mocking is by default partial.

    public static class FooExtensions
    {
        public static string Echo(this Foo foo, string value)
        {
            return value;
        }
    }
     
    ...
     
    [TestMethod]
    public void ShouldAssertExtensionMethodMockingWithArguments()
    {
        string expected = "bar";
     
        // ARRANGE
        var foo = new Foo();
     
        // Arranging: When the extension method foo.Echo() is called with any string argument,
        //            it should return expected.
        Mock.Arrange(() => foo.Echo(Arg.IsAny<string>())).Returns(expected);
     
        // ACT
        string actual = foo.Echo("hello");
     
        // ASSERT
        Assert.AreEqual(expected, actual);
    }

    Mock extension methods documentation 
Next Steps