Progress Telerik JustMock

Mock Private and Other Non-Public Members and Types

Nasa, Visa, Microsoft Fox, Samsung, IBM World Bank Group, Volvo
  • Mocking Non-Public Members and Types Overview

    In the most complex dependency cases, JustMock’s powerful features enable you to mock private methods containing important logic that creates a critical code dependency. It also supports mocking members with any access modifier like: public, private, internal, protectedprotected internalprivate protected.
    The supported mocking operations include: mock of non-public property getter and setter, mock of any non-public method overload, mock non-public method with argument matcher, mock non-public static members, etc.

    Mock non-public members and types documentation
  • Mocking Private Method

    With JustMock, mocking of private methods is straightforward task:
    public class Foo
    {
        private void DoPrivate()
        {
            throw new NotImplementedException();
        }
    }
     
    ...
     
    [TestMethod]
    public void ShouldInvokeNonPublicMemberWithMatcher()
    {
        Foo foo = new Foo();
      
        int expected = 1;
      
        // Arrange
        Mock.NonPublic.Arrange<int>(foo, "PrivateEcho", Arg.Expr.IsAny<int>()).Returns(expected);
      
        // Act
        int actual = foo.Echo(5);
      
        // Assert
        Assert.AreEqual(expected, actual);
    }

    Mock non-public member step by step documentation example
  • Mocking Private Method with Matcher

    Along the capability of mocking private methods JustMock supports argument matchers that allow you to more easily and precise narrow down for which exact arguments the created mock should execute the expected logic:
    public class Foo
    {
        private int PrivateEcho(int arg)
        {
            return arg;
        }
     
        public int Echo(int arg)
        {
            return PrivateEcho(arg);
        }
    }
    ...
     
    [TestMethod]
    public void Echo_OnExecute_ShouldReturnTheExpectationsForPrivateEcho()
    {
        var expected = 1;
     
        Foo foo = new Foo();
     
        // ARRANGE - When the non-public function PrivateEcho() is called from the foo instance, with any integer argument,
        //  it should return expected integer.
        Mock.NonPublic.Arrange<int>(foo, "PrivateEcho", ArgExpr.IsAny<int>()).Returns(expected);
     
        // ACT
        int actual = foo.Echo(5); // Echo calls the private method PrivateEcho
     
        // ASSERT
        Assert.AreEqual(expected, actual);
    }

    Mock private call with matcher documentation
  • Mocking Static Private Property Getter

    Mocking of static private property getters is as easy as mocking private property:

    public class Foo
    {
        private static int PrivateStaticProperty { get; set; }
    }
    ...
     
    [TestMethod]
    public void ShouldMockPrivateStaticProperty()
    {
        var expected = 10;
      
        // Arrange
        Foo foo = new Foo();
      
        Mock.NonPublic.Arrange<int>(typeof(Foo), "PrivateStaticProperty").Returns(expected);
      
        // Act
        int actual = foo.GetMyPrivateStaticProperty();
      
        // Assert
        Assert.AreEqual(expected, actual);
    }

    For more examples visit the JustMock documentation
Background-NextSteps

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.