Telerik® JustMock™ by Progress

In elevated mode, you can use Telerik JustMock to mock imported functions (decorated with the [DLLImport()] attribute).


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.


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


Telerik JustMock will not instrument assemblies, containing only DLL import functions.

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

Mocking the standard DLL Import

We will start by testing the FormatCurrentProcessId function. As we can see, this method is getting the current process ID, formatting it in a way and finally returning the formatted string. To write the necessary tests for these units should not be hard. You will need:

  1. To pass certain parameters.
  2. To execute the function.
  3. To check if the results are the same as the expected.
The interesting part comes right with the first point.

How can you control the current process ID?

In the past this was achieved by wrapping the static GetCurrentProcessId method and arranging against the wrapper. However, this can become a tiresome operation.

With Telerik JustMock you are now able to directly arrange against the imported method just as you are arranging static functions (check Static Mocking for more details). Next is an example, demonstrating how easy is to arrange functions, decorated with the [DLLImport()] attribute:

Here we setup that Foo.GetCurrentProcessId() should return expected value when called. Thus we override the original method behavior with a one that we specify. Then we act against our method under test. And finally we assert the expectations.

Isolating the System Under Test

Let`s assume we need to isolate certain function which is depending on an extern imported method. Such is the Start method from our example (Foo) class. In it, we aim to execute some process, but warn with message before that. To test the execution logic (which in this case is insignificant), we will need to prevent the showing of the MessageBox. Again, using the commercial version of Telerik JustMock you are able to arrange directly against it, like so:

Here we specify, when Foo.MessageBox is called, it should do nothing. Note that we are using the same matchers as in any other JustMock arrangement. Having this, we can continue with the rest of our test.

Mock Private DLL Import

In many cases, you will find that the imported method/member needs to be set to non-public (private, protected, internal, etc.). Such scenarios is applied in the Is64BitProcessMessage() method from our example (Foo) class. To test this function we will directly arrange against the imported method, but using the Mocking LINQ Queries feature of JustMock. Next are the complete tests:

Note that, in this case we are mocking a non-public, static, imported function. This clearly shows what can be achieved and on what cost, with Telerik JustMock.

See Also