1

I am new to JustMock mocking framework and I am trying to mock a private method. I am fully aware that unit testing a private method is a commonly bad practice, but in this case, I have no choice. I have been looking at the official documentation(http://www.telerik.com/help/justmock/advanced-usage-mocking-non-public-members-and-types.html) but nothing seem to work.

here is my example code:

using System;
using Telerik.JustMock;

public class Program
{
    public class Foo
    {
        public void useA()
        {
            a();
        }

        private void a()
        {
            Console.WriteLine("AAAAAAAAAAAAAAA");
        }   
    }

    public static void Main()
    {
        var mockeClass = Mock.Create<Foo>(Behavior.CallOriginal);
        Mock.NonPublic.Arrange(mockeClass, "a").DoInstead(() => Console.WriteLine("Mock success!!!"));      

        mockeClass.useA();
    }
}

And here is the error log:

Run-time exception (line 21): The type initializer for 'Telerik.JustMock.Core.Context.MockingContext' threw an exception.

Stack Trace:

[System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]

[System.TypeInitializationException: The type initializer for 'Telerik.JustMock.Core.Context.MockingContext' threw an exception.]
  at Program.Main(): line 21

I am just confused why it does not work.

Angelo Charl
  • 347
  • 4
  • 15
  • 2
    if you have no choice but testing explicitly a private method, then the method should probably not be private. Consider extracting the method out into its own class that can be tested directly, rather than via testing its callers. – Kritner Nov 02 '16 at 13:06
  • The thing is that the code that I am to be testing is really badly structured. I can't do anything about it. Another scenario I have is that the private method returns a value that affects the logic of the Method that I am testing. That Is why I needed to mock it. @Kritner – Angelo Charl Nov 02 '16 at 13:11

2 Answers2

0

Sometimes tests are helpful to let us know how to improve our code, or how it should be structured, in my opinion, like Kritner said maybe the function should be public instead of private.

In other hand, if you want to keep it as private, instead of testing the call to the private method you can test in your public method (where the private one is being called) that the code inside the private method is being called correctly in the public method like the private method doesn't exists and the code was directly write inside the public method.

Edited: you can even create a simple function which calls the private method (and add some logic related with the call if possible) and test this function.

Iskariote
  • 11
  • 4
  • With an actual unit testing, I can't edit the class. Put it this way: The main program I have in the example is the equivalent of a unit testing method in a separate project. while the class is in a separate project as well. – Angelo Charl Nov 02 '16 at 13:20
  • I think you can try to test that method useA() is calling method writeLine in class Console with args "AAAAAAAAAAAAAAA". – Iskariote Nov 02 '16 at 13:24
  • Yeah that will work on this example but what if you have to have certain parameter value to put in the method since it will have a complex algorithm that eventaully returns an "AAAAAAAAAAAAAAA" string if exact. – Angelo Charl Nov 02 '16 at 13:38
  • Then you create separate test functions or use [TestCase] in nunit to modify the assertions and expected results so that all the paths in the private method are tested. – Fran Nov 02 '16 at 15:07
  • I did not know about [TestCase](thanks for that).yes I was going to make a different test method for the private method. But sticking to the main issue, I want to make sure that the private method changes the values that will be used in the public method where it is called. – Angelo Charl Nov 02 '16 at 15:59
  • @AngeloCharl the changed values the private method performs (on a local in the public?) should have some testable behavior itself. Having this many branches in between your public and private methods means having to add additional test cases to cover all of your code... as you can probably tell this can get pretty complicated. That's why refactoring is such an important part of unit testing... since you can't refactor, you're gonna have a tougher time unfortunately. – Kritner Nov 02 '16 at 16:19
  • Yeah, you are right. I spoke to a senior developer about this and told me that the code should be refactored since it was not following the solid principles. A part of my problem is that the project is not yet ordered in the company so I can't suggest any refactoring in the code base. @Kritner – Angelo Charl Nov 03 '16 at 11:42
0

I've tried running your code in a clean .NET 4.5 console project and it works as expected - a() indeed prints out the mocked value. FileIOPermission is a very unusual exception to get from JustMock, probably the first to date. And permission errors in general are something even more unusual for console applications, because they usually run in full trust.

It would be very helpful if you paste into the ticket the InnerException of the System.TypeInitializationException with its call stack. Just copy it from the debugger.

Stefan Dragnev
  • 14,143
  • 6
  • 48
  • 52