Progress Telerik JustMock

Mock Non-Virtual Methods and Properties

Nasa, Visa, Microsoft Fox, Samsung, IBM World Bank Group, Volvo
  • Mocking Non-Virtual Methods and Properties Overview

    Mocking non-virtual methods and non-abstract classes means that you can mock a concreate instance of whichever class you like. JustMock allows you to create any mock object and test your code in complete isolation from its dependencies to achieve the best possible design for your software without making any sacrifices.

    public class Context
    {
        public IList<int> myList = new List<int>();
     
        public void PopulateList(int count)
        {
            for (int i = 0; i < count; i++)
            {
                this.myList.Add(i);
            }
        }
    }
    ...
     
    [TestMethod]
    public void MockConcreteInstance_And_ValidateOccurance()
    {
        var expectedOccurrences = 10;
     
        // ARRANGE
        // Creating a mock instance of the "Context" class with Behavior.CallOriginal.
        var foo = Mock.Create<Context>(Behavior.CallOriginal);
     
        // Arranging: foo.myList.Add() should be called expected number of times no matter the argument.
        Mock.Arrange(()=> foo.myList.Add(Arg.AnyInt)).Occurs(expectedOccurrences);
     
        // ACT
        foo.PopulateList(expectedOccurrences);
     
        // ASSERT
        Mock.Assert(foo);
    }

    Concrete mocking documentation
Background-NextSteps

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.