Unable to mock Static method in the given example,

2 posts, 0 answers
  1. Nikhil
    Nikhil avatar
    1 posts
    Member since:
    Jun 2013

    Posted 02 Oct 2013 Link to this post

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Telerik.JustMock;

    namespace ClassLibrary1

        public class AsyncTests
            public static void ClassIntialization(TestContext t)
                Mock.SetupStatic(typeof(StaticClass), StaticConstructor.Mocked);
                Mock.Arrange(() => StaticClass.Get1()).Returns(1);

            public async Task SecondTest()
                var svc = new TestAsync();           
                var r = await svc.DoSomethingAsync();
                Assert.AreEqual(1, r);
                Assert.AreEqual(1, StaticClass.Get1());

        public class TestAsync
             public Task<int> DoSomethingAsync()
           // public int DoSomethingAsync()
                return Task<int>.Factory.StartNew(() => {
                    var i = Get();
                    var j = StaticClass.Get1();

                    return j;

            public int Get()
                return 2;


        public static class StaticClass

            public static int Get1()
                return 20;
  2. Kaloyan
    Kaloyan avatar
    880 posts

    Posted 02 Oct 2013 Link to this post

    Hello Nikhil,

    I've just replied to you in ticket with ID: 742864, as follows:
    • With the current version of JustMock it is not possible to keep expectations for static members/methods against multi-threaded scenarios.

      This is into our backlog for future release. Please, check this item from the JustMock Ideas and Feedback portal in order to vote for its faster implementation.

    Let me know if I can assist you further.

    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top