Progress Telerik JustMock

Mock Virtual and Abstract Methods and Properties

 

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

    Like interfaces, virtual and abstract methods are fundamental pillars of software design. Virtual methods typically have an implementation that can be overridden in a derived class. In comparison, an abstract method (or property) is simply a declaration of a method without an implementation, which must be provided in a derived class.

    With JustMock, you can arrange the behavior of a virtual or abstract method or property in the same way you would arrange any other method.
  • Mock of an Abstract Method

    Here is an example of how to mock an abstract method with JustMock:

    public abstract class Shape
    {
        public double X { get; set; }
        public double Y { get; set; }
     
        public abstract double CalculateArea();
    }
     
    ...
     
    [TestMethod]
    public void MockAbstractMethod()
    {
        // Arrange
        Shape shapeMock = Mock.Create<Shape>();
        Mock.Arrange(() => shapeMock.CalculateArea()).Returns(42);
     
        // Act
        double actualResult = shapeMock.CalculateArea();
     
        // Assert
        Assert.AreEqual(42, actualResult);
    }
  • Mock of Virtual Method

    Here is how to mock a virtual method with JustMock, using the same approach:
    public abstract class Shape
    {
        public double X { get; set; }
        public double Y { get; set; }
     
        public virtual double CalculateArea()
        {
            return X * Y;
        }
    }
     
    ...
     
    [TestMethod]
    public void MockAbstractMethod()
    {
        // Arrange
        Shape shapeMock = Mock.Create<Shape>();
        Mock.Arrange(() => shapeMock.CalculateArea()).Returns(42);
     
        // Act
        double actualResult = shapeMock.CalculateArea();
     
        // Assert
        Assert.AreEqual(42, actualResult);
    }
  • Mock a Property Getter

    Mocking properties is similar to mocking methods. Here’s how to mock a property getter with JustMock:
    public interface IFoo
    {
        int Value{get; set;}
    }
     
    ...
     
    [TestMethod]
    public void ShouldFakePropertyGet()
    {
        // Arrange
        var foo = Mock.Create<IFoo>();
      
        Mock.Arrange(() => foo.Value).Returns(25);
      
        // Act
        var actual = foo.Value;
      
        // Assert
        Assert.AreEqual(25, actual);
    }

    Mock a property getter documentation
  • Mock a Property Setter

    Mocking a property setter differs from mocking a property getter as it requires you to use another method due to a limitation in LAMBDA expressions to process the assignment operator:

    [TestMethod]
    public void ShouldAssertPropertySet()
    {
        // Arrange
        var foo = Mock.Create<IFoo>();
      
        Mock.ArrangeSet(() => foo.Value = 1);
      
        // Act
        foo.Value = 1;
      
        // Assert
        Mock.AssertSet(() => foo.Value = 1);
    }

    Mock a property setter documentation
Background-NextSteps

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.