Progress Telerik JustMock

Mock DLL Imports

Nasa, Visa, Microsoft Fox, Samsung, IBM World Bank Group, Volvo
  • 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
Background-NextSteps

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.