Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:42:07+00:00 2026-06-03T22:42:07+00:00

I am unit-testing private methods in C# using NUnit. For example, my method (if

  • 0

I am unit-testing private methods in C# using NUnit.

For example, my method (if public) is expected to throw an ArgumentNullException. I can assert that the method throws an ArgumentNullException like so: Assert.Throws<ArgumentNullException>(() => method.Call());

However, since I am invoking a private method using reflection, I would assert a TargetInvocationException for the method that throws an ArgumentNullException like so: Assert.Throws<TargetInvocationException>(() => methodInfo.Invoke(obj, new object[] { params }));

I would like to assert an ArgumentNullException instead of a TargetInvocationException for that private method so I can scan over its code and know what its expected to do rather than to debug to find out.

How would I assert for the actual exception, instead of a TargetInvocationException?

NOTE:
This question is not addressing the theory behind unit-testing public vs. private methods. My team and I have made the decision to unit-test the private methods, and whether or not that is the way to unit-test is irrelevant to this question. See the most upvoted answer on this question to understand our rationale.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:42:10+00:00Added an answer on June 3, 2026 at 10:42 pm

    Found my answer:

    var exception = Assert.Throws<TargetInvocationException>(() => methodInfo.Invoke(obj, new object[] { params }));
    Assert.IsInstanceOf<Exception>(exception.InnerException);
    

    UPDATE

    Assert.IsNotNull(exception.InnerException) lets me know that an inner exception exists. Assert.IsInstanceOf<Exception>(exception.InnerException); will assert any type of Exception thrown. I agree that both ways tell us that there is an inner exception.

    However…..what if I want to assert for a specific type of inner exception?

    For example, if my method throws an ArgumentNullException, then I cannot assert for that by doing Assert.IsInstanceOf<FileNotFoundException>(exception.InnerException); Using Assert.IsNotNull lets me know that an inner exception exists, but it does not reveal the type of the inner exception. Therefore, this is why I prefer using IsInstanceOf in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For unit testing you shouldn't test private methods, yes, but for integration tests (using
I'm having trouble unit testing a method that changes some properties of a reference
I'm trying to unit test, using VS unit testing facility following method. void Get(string
I'm new to unit testing using nunit (and Java development in general). When creating
In unit testing, the setup method is used to create the objects needed for
I am refactoring a class so that the code is testable (using NUnit and
I'm trying to write a method that will get a private field in a
Im using the Prism/Composite Application Library and trying to unit test some code that
I am using Rhino Mocks as a mocking framework for unit testing. I have
I am trying to do unit testing (using the Boost unit testing framework) on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.