JustMock

In elevated mode, you can use Telerik JustMock to mock non-public members and types. That is useful when you want to isolate calls to non-public members.

Note

This feature is available only in the commercial version of Telerik JustMock.

Refer to this topic to learn more about the differences between both the commercial and free versions of Telerik JustMock.

Important

To mock non-public members and types you first need to go to elevated mode by enabling Telerik JustMock from the menu. How to Enable/Disable Telerik JustMock?

In the further examples we will use the following sample class to test:

Step by step example

First, create an instance of the type you want to test. To mock a non-public member use the Mock.NonPublic modifier and then add the arrange statement. In the arrange statement, first pass the target object to test, then the member name that you want to test as a string and eventually arguments to be passed in case you test a method.

Here we setup that a call to the DoPrivate method of the Foo class should set a local variable called. Thus we override the original method behavior with a one that we specify.

Mock Private Call with Matcher

Arrange a call to a private method accepting an argument that matches any integer value.

Here we specify that PrivateEcho method called with any int argument will return 1. Acting is by calling it with 5 as argument. Finally, we verify that the return value is actually 1.

Mock Private Method With Overloads

Here we arrange a call to a private method with two overloads.

The following class will be used:

Important

To interact with non-public classes you will need to add the InternalVisibleTo property inside the AssemblyInfo.cs in your project, like this:

CopyC#
[assembly: InternalsVisibleTo("YourTestProject")]

We mock the pExecute method that accepts an integer value as argument. We arrange it to set a local boolean variable to true once it is called with 10 as argument. After that we act by calling foo.Execute(10) and verify that called is true.

Mock Private Interface Implementation Method

This example shows how we can mock an explicit (not public) interface implementation method from current or base class.

The following classes will be used:

We can now mock the IManager.Provider call in this way:

Mock Internal Virtual Method

Arrange a call to an internal virtual method.

Note that in the arrange statement we don't use a mock of Foo, but the actual instance.

Mock Internal Virtual Property Get And Set

Arrange a call to an internal virtual property.

Note that in the arrange statement we don't use a mock of Foo, but the actual instance.

Follows an example of mocking internal virtual property set. We override the actual implementation by arranging that the foo.Value must be called with certain value.

Mock Private Static Method

The following example shows how to mock a private static method.

We use the following class:

Important

To interact with non-public classes you will need to add the InternalVisibleTo property inside the AssemblyInfo.cs in your project, like this:

CopyC#
[assembly: InternalsVisibleTo("YourTestProject")]

The method we arrange is FooInternalStatic.EchoPrivate().

We call the Echo method, but its implementation calls the EchoPrivate method, so our assertion passes.

Mock Private Static Property

The following example shows how to mock the get function of a private static property.

We use the Foo class from above.

The property we arrange is Foo.PrivateStaticProperty. When called, it will return an expected integer value, different from the default:

To act, we call the GetMyPrivateStaticProperty() method, but its implementation returns the PrivateStaticProperty, so our assertion passes.

Mock Internal Class

Let's see an example of how to mock an internal class from .NET framework. Consider the System.Net.HttpRequestCreator class which is internal, but has a public interface System.Net.IWebRequestCreate. Here we mock its Create method.

Note the use of ArgExpr.IsAny<Uri>() - as we mock a non public call, we need to know the type of the argument to resolve the method. Thus, instead of using Arg, like we do in most of the other cases, we must use ArgExpr.

Important
To mock internal type, your assembly name must be fully qualified according to the framework design rules, i.e. assembly name = namespace. Note that you can't mock types from mscorlib in this way. We do a hierarchical search to find the proper qualified name as in the above example. System.Net.HttpRequestCreator is found in the System assembly, not in System.Net.

Mock Protected Member

To show how the mocking of protected members is being done, we will use the following class:

To mock protected members in JustMock, we use the same method and logic, as for the rest non-public types, shown above:

First we will arrange that our IntValue() method should never occur, like this:

The second test will test if our protected Load() method is actually been called, when Init() is initiated.

Important
To mock protected type, your assembly name must be fully qualified according to the framework design rules, i.e. assembly name = namespace. Note that you can't mock types from mscorlib in this way.

See Also