Mock Static or Extension Members
Working with .NET applications enables you to use static classes, methods and properties. When a method is declared as static, it does not require an instance of its parent class so that it can be invoked — you can directly call it on the class itself.
Testing a non-static method (at least one that has a simple implementation without external dependencies) is straightforward. However, testing a static method is not an easy task at all. This article explains how you can overcome this challenge and test static classes and their members using JustMock.
Mocking static implementations is an elevated feature. Refer to the Commercial vs Free Version topic to learn more about the differences between both versions of Telerik JustMock.
To better illustrate different cases, we will be using several examples classes representing an order. The order can contain different products and it might have a gift for the user if they purchased more items. Here is how the implementation looks like:
Sample setup
Mock Static Constructor
In the sample code above, you will notice that the constructor of GiftsDistributor
invokes the EnsureGifts
which in turn is used to contact the warehouses and ensure that they have enough amount of the products used as gifts for the future orders. The EnsureGifts
method might need a connection to a specific service or database to do his job. This, however, is not a focus on the test we would like to implement – namely, to ensure that gifts are properly distributed for the orders. Here the static constructor mocking of JustMock comes handy to eliminate the dependencies we are not interested in.
The API of JustMock provides you with the SetupStatic
method that prepares all the static members of a class and mocks its constructor.
Example 1: Mock a static object constructor
Mock Static Property
Once you set up the static type, you can mock its properties as you would do with instance properties.
Example 2: Mock a static property
Mock Static Method
Mocking static methods is also similar to how you mock instance methods. Just use the Mock.Arrange()
method to setup the behavior you need.
Example 3: Mock a static method
Mock Extension Method
Extension methods in .NET are also defined as static ones. To demonstrate how you can mock extension methods, we will use the following sample implementation extending the functionality of the Order
class.
Sample static class
PrintOrder
you would again need only to arrange your expectations with Mock.Arrange()
.
Example 4: Mock extension method
Next Steps
Take a look at the Mock Non-Public API article that shows how you can setup private and internal implementations in your tests.