Issue with Static Mocking of Utility Class

6 posts, 1 answers
  1. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 31 May 2011 Link to this post

    I have a Utility class that contains several static methods.  Originally I was setting up my static mock within an individual unit test but that was causing it to fail when run as a group of tests (I found the reason for that).  So to fix that issue I have moved the static setup to the ClassInitialize method. 

    Here is the code....
    Mock.SetupStatic<EagleUtility>();
               Mock.Arrange(() => EagleUtility.ValidateMessage(Arg.IsAny<byte[]>(), Arg.AnyInt, TowerTypes.Unified)).
                 DoNothing().Returns(true).MustBeCalled();
    
    

    This does not seem to be working.  When I call the test method that uses the mock and asserts that this and other methods are called it fails. I try debugging and see that the method in question is not being skipped as I expect it to.

    Any suggestions?
  2. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 01 Jun 2011 Link to this post

    Still working the issue on this end with no avail as of yet.  I did see that all the methods in the class are being "semi mocked" as I had forgotten to put calloriginal for behavior and expected some other methods to run normally that weren't.  I fixed that but this still doesn't seem to be working.
  3. DevCraft R3 2016 release webinar banner
  4. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 02 Jun 2011 Link to this post

    Well still no luck nor any responses perhaps it will help if I show the calling code etc. so here we go (with minor edits where appropriate to protect comapny IP).


    ClassInitialize Method
    [ClassInitialize()]
           public static void MyClassInitialize(TestContext testContext)
           {
               Mock.SetupStatic<EagleUtility>(Behavior.CallOriginal);
               Mock.Arrange(() => EagleUtility.ValidateMessage(Arg.IsAny<byte[]>(), Arg.AnyInt, TowerTypes.Unified)).
                 DoNothing().Returns(true).MustBeCalled();
           }

    TestMethod
    public void RecieveIncomingMessageTest()
            {
                var expectedTower = TestContext.DataRow["Tower"].ToString();
                var expectedEventDescription = TestContext.DataRow["EventDescription"].ToString().TrimStart('\r', '\n', ' ');
                expectedEventDescription = expectedEventDescription.TrimEnd(' ', '\n', '\r', '\0');
                var rawDataToUse = Convert.FromBase64String(TestContext.DataRow["RawData"].ToString());
                var called = false;
                var target = new UnifiedProtocolTranslator();
                ;
                int byteCount = rawDataToUse.Length;
                EagleIncomingMessageStatus expected = EagleIncomingMessageStatus.Complete;
                EagleIncomingMessageStatus actual;
     
                Mock.NonPublic.Arrange<bool>(target, "ProcessIncomingMessage", rawDataToUse, rawDataToUse.Length).DoNothing().Returns(true).MustBeCalled();
     
                Mock.NonPublic.Arrange(target, "CompileIncomingMessage").DoNothing().MustBeCalled();
                actual = target.RecieveIncomingMessage(rawDataToUse, byteCount);
     
                Mock.Assert(target);
      
     
                Assert.AreEqual(expected, actual);
            }


    Method Being Tested.

    public EagleIncomingMessageStatus RecieveIncomingMessage(byte[] message, int byteCount)
           {
               var messageType = (UnifiedIncomingMessageTypes) message[0];
               EagleIncomingMessageStatus returnValue = EagleIncomingMessageStatus.Continue;
               if (EagleUtility.ValidateMessage(message, byteCount, TowerTypes.Unified))
               {
                   if (ProcessIncomingMessage(message, byteCount))
                   {
                       CompileIncomingMessage();
                       returnValue = EagleIncomingMessageStatus.Complete;
                   }
               }
     
               return returnValue;
           }


    The test is failing on all runs for the test.  This is a data driven unit test pulling from an XML data source. So I believe I have to use Arg.IsAny<byte[]> in the Class Initialize method.

    Failure Message is: "Test method UnifiedProtocolTest.UnifiedProtocolTranslatorTest.RecieveIncomingMessageTest threw exception: 
    Telerik.JustMock.MockAssertionException: Setup contains calls that are marked as "MustBeCalled" but actually never called."

    Which makes sense since I don't believe the ValidateMessage method is being mocked properly.

    Please help I am going bald from ripping out my hair over this.

  5. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 03 Jun 2011 Link to this post

    Hi Chris,

    Basically to avoid this kind of static method issues, you have to move the initialization to ClassInitalize not the setup itself. Here is one example that shows how you can initialize in this regard:

    http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

    Please let us know if you are still having problems.



    Kind Regards
    Mehfuz
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Chris
    Chris avatar
    21 posts
    Member since:
    Oct 2012

    Posted 03 Jun 2011 Link to this post

    I saw that post and thought I was doing it like described (but then realized I wasn't) so that part is fixed. I have another issue I am fighting similar to this one dealing with private methods and partial mocking.

    Basically it is the same situation where I have a test method that mocks a nonpublic member.  When that test method runs by itself the method is mocked correctly, however when run as part of a larger test run it fails.

    I can't do the Mock.Partial<t>().For since it is a non public.  any suggestions?
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 08 Jun 2011 Link to this post

    Hi Chris,

    Thanks again for sending the issue. Yes you right that Mock.Partial should have options for NonPublic members as well. I am logging this as a bug to be fixed in some of the upcoming builds



    Kind Regards,
    Ricky
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft R3 2016 release webinar banner