JustMock-hero
Support .NET 5, .NET 6 Official & .NET Core

Telerik JustMock

The fastest, most flexible and complete mocking tool
for crafting unit tests.

Download Free Trial

Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Telerik is proud to hold the following industry awards.

big_wave_dblue_hero

Explore Some of Our Amazing Features

ninja-peeking
[TestMethod]
public void ShouldMockGenericClass()
{
    int expectedValue = 1;

    // Arrange
    // Creating a mock instance of the "FooGeneric<T>" class.
    var foo = Mock.Create<FooGeneric<int>>();

    // When the virtual foo.Get() is called with any integer as an argument, it should return expectedValue.
    Mock.Arrange(() => foo.Get(Arg.IsAny<int>())).Returns(expectedValue);

    // Act
    int actualValue = foo.Get(0);

    // Assert
    Assert.AreEqual(expectedValue, actualValue);
}
                        
<TestMethod> _
Public Sub ShouldMockGenericClass()
    Dim expectedValue As Integer = 1

    ' Arrange
    ' Creating a mock instance of the "FooGeneric<T>" class.
    Dim foo = Mock.Create(Of FooGeneric(Of Integer))()

    ' When the virtual foo.Get() is called with any integer as an argument, it should return expectedValue.
    Mock.Arrange(Function() foo.[Get](Arg.IsAny(Of Integer)())).Returns(expectedValue)

    ' Act
    Dim actualValue As Integer = foo.[Get](0)

    ' Assert
    Assert.AreEqual(expectedValue, actualValue)
End Sub
                        
[TestMethod]
public void ShouldAssertWithCustomSelect()
{
    var simpleDataInstance = new SimpleData();

    // Arrange - When simpleDataInstance.Products_GET is called, it should return expected collection.
    Mock.Arrange(() => simpleDataInstance.Products).ReturnsCollection(ReturnExpextedCollectionOfProducts());

    // Act - Applying a LINQ query for simpleDataMock.Products.
    var actual = (from p in simpleDataInstance.Products
                    where p.UnitsInStock == 50
                    select p.ProductID).SingleOrDefault();

    // Assert
    Assert.AreEqual(2, actual);
}
                        
<TestMethod> _
Public Sub ShouldAssertWithCustomSelect()
    Dim simpleDataInstance = New SimpleData()

    ' Arrange - When simpleDataInstance.Products_GET is called, it should return expected collection.
    Mock.Arrange(Function() simpleDataInstance.Products).ReturnsCollection(ReturnExpextedCollectionOfProducts())

    ' Act - Applying a LINQ query for simpleDataMock.Products.
    Dim actual = (From p In simpleDataInstance.Products _
                    Where p.UnitsInStock = 50 _
                    Select p.ProductID).SingleOrDefault()

    ' Assert
    Assert.AreEqual(2, actual)
End Sub
                        
[TestMethod]
public void DoPublic_OnExecute_ShouldCallDoPrivate()
{
    var isCalled = false;
    
    Foo foo = new Foo();
    
    // Arrange - When the non-public method DoPrivate() is called from the foo instance, 
    // it should set isCalled to true instead of executing its original logic.
    Mock.NonPublic.Arrange(foo, "DoPrivate").DoInstead(() => isCalled = true);
    
    // Act
    foo.DoPublic(); // DoPublic() should call DoPrivate().
    
    // Assert
    Assert.IsTrue(isCalled);
}
                        
<TestMethod>
Public Sub DoPublic_OnExecute_ShouldCallDoPrivate()
    Dim isCalled = False

    Dim foo As New Foo()

    ' Arrange - When the non-public method DoPrivate() is called from the foo instance, 
    ' it should set isCalled to true instead of executing its original logic.
    Mock.NonPublic.Arrange(foo, "DoPrivate").DoInstead(Function() InlineAssignHelper(isCalled, True))

    ' Act - DoPublic() should call DoPrivate().
    foo.DoPublic()

    ' Assert
    Assert.IsTrue(isCalled)
End Sub
                        
[TestMethod]
public void ShouldFakeStaticPropertyGet()
{
    bool isCalled = false;
    var expected = 1;

    // Arrange
    // Getting the static instance(Foo) ready for mocking disregarding the constructor and applying strict behavior.
    // If we don't mock the constructor, a NotImplementedException will be thrown.
    Mock.SetupStatic(typeof(Foo), Behavior.Strict, StaticConstructor.Mocked);

    // When the static(Foo.FooProp_GET) property is called, 
    // it should assign true to isCalled and return expected instead.
    Mock.Arrange(() => Foo.FooProp).DoInstead(() => { isCalled = true; }).Returns(expected);

    // Act
    var actual = Foo.FooProp;

    // Assert
    Assert.AreEqual(expected, actual);
    Assert.IsTrue(isCalled);
}

                        
<TestMethod>
Public Sub ShouldFakeStaticPropertyGet()
    Dim isCalled As Boolean = False
    Dim expected = 1

    ' Arrange
    ' Getting the static instance(Foo) ready for mocking disregarding the constructor and applying strict behavior.
    ' If we don't mock the constructor, a NotImplementedException will be thrown.
    Mock.SetupStatic(GetType(Foo), Behavior.[Strict], StaticConstructor.Mocked)

    ' When the static(Foo.FooProp_GET) property is called, 
    ' it should assign true to isCalled And return expected instead.
    Mock.Arrange(Function() Foo.FooProp).DoInstead(Sub() isCalled = True).Returns(expected)

    ' Act
    Dim actual = Foo.FooProp

    ' Assert
    Assert.AreEqual(expected, actual)
    Assert.IsTrue(isCalled)
End Sub

                        
[TestMethod]
public void ShouldApplyIgnoreInstanceToVirtual()
{
    var expected = 10;

    // Arrange
    // Creating a mocked instance of the "Calculus" class.
    // This mock will also be used as a fake of a given type for applying a future instance expectation to that type.
    var calculus = Mock.Create<Calculus>();

    // When calculus.Sum() is called, it should return expected.
    // Also, this expectations will apply for all newly created instances of the Calculus class 
    // during the test.
    Mock.Arrange(() => calculus.Sum()).IgnoreInstance().Returns(expected);

    // Act
    var valueFromExistingInstance = calculus.Sum();
    var valueFromNewInstance = new Calculus().Sum();

    // Assert
    Assert.AreEqual(expected, valueFromExistingInstance); // Verifying the value from an already existing instance.
    Assert.AreEqual(expected, valueFromNewInstance); // Verifying the value from a newly created instance.
}

                        
<TestMethod>
Public Sub ShouldApplyIgnoreInstanceToVirtual()
    Dim expected = 10

    ' Arrange
    ' Creating a mocked instance of the "Calculus" class.
    ' This mock will also be used as a fake of a given type for applying a future instance expectation to that type.
    Dim calculus = Mock.Create(Of Calculus)()

    ' When calculus.Sum() is called, it should return expected.
    ' Also, this expectations will apply for all newly created instances of the Calculus class 
    ' during the test.
    Mock.Arrange(Function() calculus.Sum()).IgnoreInstance().Returns(expected)

    ' Act
    Dim valueFromExistingInstance = calculus.Sum()
    Dim valueFromNewInstance = New Calculus().Sum()

    ' Assert
    Assert.AreEqual(expected, valueFromExistingInstance) ' Verifying the value from an already existing instance.
    Assert.AreEqual(expected, valueFromNewInstance) ' Verifying the value from a newly created instance.
End Sub

                        
[TestMethod]
public void ShouldAssertCustomValueForDateTimeNow()
{
    var expected = new DateTime(1900, 4, 12);

    // Arrange - Here we arrange, when DateTime.Now is called it should return expected DateTime.
    Mock.Arrange(() => DateTime.Now).Returns(expected);

    // Act
    var now = DateTime.Now;

    // Assert - the date is indeed what was arranged.
    Assert.AreEqual(expected.Year, now.Year);
    Assert.AreEqual(expected.Month, now.Month);
    Assert.AreEqual(expected.Day, now.Day);
}

                        
<TestMethod>
Public Sub ShouldAssertCustomValueForDateTimeNow()
    Dim expected = New DateTime(1900, 4, 12)

    ' Arrange - Here we arrange, when DateTime.Now is called it should return expected DateTime.
    Mock.Arrange(Function() DateTime.Now).Returns(expected)

    ' Act
    Dim now = DateTime.Now

    ' Assert - the date is indeed what was arranged.
    Assert.AreEqual(expected.Year, now.Year)
    Assert.AreEqual(expected.Month, now.Month)
    Assert.AreEqual(expected.Day, now.Day)
End Sub

                        
[TestMethod]
public void ShouldReturnFakeDbSet_WhenDbContextPropertyIsCalled()
{
    // Arrange
    NerdDinners nerdDinners = new NerdDinners();

    // When the DBSet property nerdDinners.Dinners is called, it should return a predefined fake collection containing 3 items.
    Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());

    // Act - We call the nerdDinners.Dinners and search for a dinner with a certain DinnerID.
    var actualQuery = from d in nerdDinners.Dinners
                        where d.DinnerID == 1
                        select d;

    // Assert - We assert that the nerdDinners.Dinners collection will actually return a collection with 3 items.
    Assert.AreEqual(3, nerdDinners.Dinners.Count());
    // We assert that there is only one item in our collection that has DinnerID equal to one.
    Assert.AreEqual(1, actualQuery.Count());
    Assert.AreEqual(1, actualQuery.First().DinnerID);
}
                        
<TestMethod>
Public Sub ShouldReturnFakeDbSet_WhenDbContextPropertyIsCalled()
    ' Arrange
    Dim nerdDinners As New NerdDinners()

    ' When the DBSet property nerdDinners.Dinners is called, it should return a predefined fake collection containing 3 items.
    Mock.Arrange(Function() nerdDinners.Dinners).ReturnsCollection(FakeDinners())

    ' Act - We call the nerdDinners.Dinners and search for a dinner with a certain DinnerID.
    Dim actualQuery = From d In nerdDinners.Dinners Where d.DinnerID = 1D

    ' Assert - We assert that the nerdDinners.Dinners collection will actually return a collection with 3 items.
    Assert.AreEqual(3, nerdDinners.Dinners.Count())
    ' We assert that there is only one item in our collection that has DinnerID equal to one.
    Assert.AreEqual(1, actualQuery.Count())
    Assert.AreEqual(1, actualQuery.First().DinnerID)
End Sub

                        
Coppied!

Why Choose JustMock

Flexible Packaging Tailored to Your Needs

You can buy Telerik JustMock individually or as a part of the DevCraft bundle. If you want to be covered for all .NET and JavaScript technologies or need a reporting, mocking or testing solution, the DevCraft bundles offers the most value for money—and the most power.

ninja-175x175px-min

Buy Telerik JustMock

Buy the fastest, most flexible and complete mocking tool for crafting unit tests.

See Pricing

Starting at $399

Buy DevCraft Bundle

Get the complete bundle of .NET controls, JavaScript components, reporting, automated testing and productivity tools.

See Pricing

Starting at $1,299

“I recently dropped another leading mocking tool in favor of JustMock. I was never happy with the other tool’s syntax but I couldn’t find anything else on the market. I’ve found JustMock is a much more pleasant experience.”

Brad Irby

.NET Architect and author of Reengineering .NET, Reengineering .NET

“When isolating methods for testing gets difficult when trying to test against legacy code or databases, JustMock really starts to set itself apart. Its features such as MsCorlib, Sealed, Static, Private method and Entity Framework mocking make the seemingly untestable, testable.”

Bradley Braithwaite

Software Developer, Contented Coder

“I've used a series of mocking tools and I believe that the API for JustMock is the most mature that I've encountered so far. The highest praise that I can give a framework tool is to say that its semantics are readable enough that you don't think about them at all as you're reading the code, and this is true with JustMock.”

Erik Dietrich

Software Developer, DaedTech

What's New

Mock Interfaces with Default Implementation

Mock Interfaces with Default Implementation

Telerik JustMock now supports default implementation of interfaces.

Support for .NET 7 Preview

Compatibility with .NET 7 Preview 1

Telerik JustMock ships support for .NET 7 Preview 1.

Create a Mock for a Method from the Context Menu

Create a Mock for a Method from the Context Menu

Telerik JustMock now features the ability to create a mock from the quick action menu.

Industry-Leading Support

Expert and Timely Support

Get answers to your questions directly from the developers who build this UI suite, even during your trial period.

Contact support

Need Evaluation Help?

If you are not a developer or don't have time to evaluate our product, send us your project requirements. We will evaluate your required features and let you know how our products fit your needs.

Send us your project requirements
Background-NextSteps

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.