locked
How to handle exceptions with PrivateType .InvokeStatic? RRS feed

  • Question

  • I am trying to test private static method from a class, in this way:

    PrivateType privateTypeObject = new PrivateType(typeof(MyType));
    
    try
    {
        privateTypeObject.InvokeStatic("myPrivateMethod", 0);
    }
    catch (ArgumentException) {}
    catch {throw; }

    The method that I am trying to test it has to throw an argument exception, is the correct, so this is the reason that the catch of the argument exception is empty, because I don't have to do anything.

    However, I get an error that says that the exception is not handle. I have try to in the method to throw an Exception, a generic exception, but the problem is the same.

    With no private methods in which I can call the method directly, without the PrivateType object, it works as I expect, but in this case I get the error.

    How could I catch the exception that I get from the method?

    Thanks.

    Thursday, August 11, 2016 2:04 PM

Answers

  • Hi ComptonAlvaro,

    According to your description, I have set a breakpoint in catch statement of test method and debugged it. I have got the following screenshot:

    In our method, when we add throw the exception code to “catch” statement and add breakpoint to this line, which will display Exception was unhandled by user code. This is because the error will be captured in TestMethod.

    Did you get the same result as above screenshot? If you don’t want this message shows. We could disable Exception settings in above screenshot, then this message would not be shown again.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by ComptonAlvaro Monday, August 15, 2016 8:40 AM
    Monday, August 15, 2016 6:31 AM

All replies

  • Hi ComptonAlvaro,

    In my side, based on your codes I test a private static method with try catch statement without problems. Could you please share some code snippets about your test method MyType that could help me to repro your issue? You could refer to the following test code in my side:

    Method:

        public class UnitTestForPrivateStaticMethod
        {
            private static bool IsTrue(string input1, int input3, bool input2 = false)
            {
                try
                {
                    int a = 2 / input3;
                }
                catch { throw; }
    
                if (input1.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
    
            }
        }

    Unit Test Method:

     [TestMethod]
            public void UnitTestForPrivateStaticMethod()
            {
               try
                {
                    string input1 = "something";
                    bool expected = true;
                    PrivateType privateType = new PrivateType(typeof(UnitTestForPrivateStaticMethod));
                    object[] parameters = new object[1];
                    bool actual = (bool)privateType.InvokeStatic("IsTrue", input1, 0,false);
                    Assert.AreEqual(expected, actual);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }  
            }

    In your method MyType, if the argument exception occurs before your try-catch. In test method we will can’t capture the exception thrown by try-catch statement.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, August 12, 2016 9:35 AM
  • Hello, thanks for your help.

    In my case I have tried that if I run the test, I don't have problems, but if I try to debug the test, if I set a breakpoint in the catch in the test method, I get an error in the catch of the method that I am testing that says that the exception is not handle.

    Could you try to debug the test method?

    Thank you.

    Friday, August 12, 2016 9:47 AM
  • Hi ComptonAlvaro,

    According to your description, I have set a breakpoint in catch statement of test method and debugged it. I have got the following screenshot:

    In our method, when we add throw the exception code to “catch” statement and add breakpoint to this line, which will display Exception was unhandled by user code. This is because the error will be captured in TestMethod.

    Did you get the same result as above screenshot? If you don’t want this message shows. We could disable Exception settings in above screenshot, then this message would not be shown again.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by ComptonAlvaro Monday, August 15, 2016 8:40 AM
    Monday, August 15, 2016 6:31 AM
  • Yes, I have the same behavior, so how you advise me, I will disable the exception.

    Thank you so much.

    Monday, August 15, 2016 8:39 AM