Arranged static method is not arranged on different thread

4 posts, 0 answers
  1. Todd K
    Todd K avatar
    5 posts
    Member since:
    Jan 2010

    Posted 17 May 2012 Link to this post

    I am trying to test my application that uses Tasks. I have arranged a static method, if I call that method right after arranging it, I get the Mocked return value. But, if I call the method from a task, the method is not mocked.
    I am running on version 2011.2.713.2  as my subscription has run out, and since this is a static method, I cannot use the free edition.

    I do have JustMock enabled in the VS menu Telerik->JustMock->Enable JustMock
    I managed to reproduce the problem with a simple example:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Telerik.JustMock;
      
    namespace TestMockingStatic
    {
       public class ClassWithStatic
       {
          public static int NumGot = 0;
          public static int GetANumber(int foo, int bar)
          {
             throw new NotImplementedException();
          }
       }
         
       [TestClass]
       public class UnitTest1
       {
          public void GetValue()
          {
             Task.Factory.StartNew(() =>
             {
                ClassWithStatic.NumGot = ClassWithStatic.GetANumber(5, 10);
             });
          }
      
          [TestMethod]
          public void TestMethod1()
          {
             Mock.SetupStatic<ClassWithStatic>(Behavior.Strict);
             Mock.Arrange(() => ClassWithStatic.GetANumber(Arg.AnyInt, Arg.AnyInt)).Returns(42);
      
             Assert.AreEqual(42, ClassWithStatic.GetANumber(5, 10), "Mocked Method did not return expected result");
      
             GetValue();
             SpinWait.SpinUntil(() => ClassWithStatic.NumGot > 0, 20000);
      
             Assert.AreEqual(42, ClassWithStatic.NumGot, "Mocked method called via TASK, did not return correct value");
          }
       }
    }

    Any help would be great,
    Todd
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 22 May 2012 Link to this post

    Hi Todd,

    Thanks again for reporting the issue. 

    We have identified the problem and logged an issue for it into our system. However, should you need the fix urgently please open a support ticket where I will send you the fix asap.


    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft R3 2016 release webinar banner
  4. Mark
    Mark avatar
    6 posts
    Member since:
    Jun 2012

    Posted 19 Jul 2013 Link to this post

    Have you guys solved this?  I'm having the same problem.

  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 22 Jul 2013 Link to this post

    Hi Mark,

    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.

    To assist you further, I would suggest the using of a singleton class:
    public class Singleton
    {
        private static Singleton myInstance = new Singleton();
        private static object syncRoot = new Object();
     
        private Singleton() { }
     
        public static Singleton GetInstance
        {
            get
            {
                if (myInstance == null)
                {
                    lock (syncRoot)
                    {
                        if (myInstance == null)
                            myInstance = new Singleton();
                    }
                }
                return myInstance;
            }
        }
     
        public int NumGot = 0;
     
        public int GetANumber(int foo, int bar)
        {
            throw new NotImplementedException();
        }
    }
     
     
    [TestClass]
    public class UnitTest1
    {
        public void GetValueSingleton()
        {
            Task.Factory.StartNew(() =>
            {
                var mySingletonInstance = Singleton.GetInstance;
                mySingletonInstance.NumGot = mySingletonInstance.GetANumber(5, 10);
            });
        }
     
        [TestMethod]
        public void TestMethodSingleton()
        {
            var mySingleton = Singleton.GetInstance;
            Mock.Arrange(() => mySingleton.GetANumber(Arg.AnyInt, Arg.AnyInt)).Returns(42);
     
            Assert.AreEqual(42, mySingleton.GetANumber(5, 10), "Mocked Method did not return expected result");
     
            GetValueSingleton();
            SpinWait.SpinUntil(() => mySingleton.NumGot > 0, 20000);
     
            Assert.AreEqual(42, mySingleton.NumGot, "Mocked method called via TASK, did not return correct value");
        }
    }

    The above scenario works as expected.

    I hope this helps.

    Regards,
    Kaloyan
    Telerik
    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