background

Telerik JustMock

Mock Non-Virtual Methods and Properties

  • JustMock allows you to create mock object from any type and test your code in complete isolation from its dependencies to achieve the best possible design for your software without making any sacrifices.
  • 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 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
Next Steps