background

Telerik JustMock

Mock DLL Imports

  • JustMock allows you to easily mock methods marked with the DLLImport attribute and test your code in isolation from such dependencies.
  • 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 DLL Imports Overview

    The DLLImport is a storage class attribute that represents a Microsoft-specific extension to the C and C++ languages. With such extensions, you can import C and C++ DLLs and use them in C# or VB code. This inevitably makes your code dependent on calls to the C or C++ API.

    JustMock allows you to easily mock methods marked with the DLLImport attribute and test your code in isolation from such dependencies.

    public class Foo
    {
        [DllImport("Kernel32.dll")]
        public static extern int GetCurrentProcessId();
    }
     
    ...
     
    [TestMethod]
    public void FormatCurrentProcessId_OnExecute_ShouldReturnExpected()
    {
        var expected = 3500;
      
        // Arrange
        Mock.Arrange(() => Foo.GetCurrentProcessId()).Returns(expected);
      
        // Act
        var myFoo = new Foo();
        var actual = myFoo.FormatCurrentProcessId();
      
        // Assert
        Assert.AreEqual(string.Format("The current process ID is {0}", expected), actual);
    }

    Mock DLL Imports documentation
Next Steps