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 6708387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:45:12+00:00 2026-05-26T07:45:12+00:00

I have the following classes: public class MyClass { public void MyMethod() { try

  • 0

I have the following classes:

public class MyClass
{
    public void MyMethod()
    {
        try
        {
            ...
            save data to file
            ...
        }
        catch (Exception exception)
        { 
            ErrorDisplay.ShowErrorMessage("MyMethod", exception);
        }
    }
}

public class ErrorDisplay
{
    public static ShowErrorMessage(string methodName, Exception exception)
    {
        if (exception is IOException)
            MessageBox.Show(methodName + " : " + GetIODisplayMessage());
        else if ... 
            ...
        else
            ...
   }

    public static string GetIODisplayMessage()
    {
        return "IO error";
    }

    ....
}

I need to write a unit test in witch i will mock the IOException and i need to check that
GetIODisplayMessage() method is being called. Is there a way to test if a method is called?
Or maybe another idea on how to make a unit test for my case?

Thanks.

  • 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-05-26T07:45:13+00:00Added an answer on May 26, 2026 at 7:45 am

    As mentioned above, using the static ErrorDisplay causes a number of problems with testing and injecting in an implementation of IErrorDisplay would solve some of them but not all (see MessageBox below). However…

    Static + Interface

    If you need to keep the static class ErrorDisplay and don’t want to shell out for TypeMock you can add a level of indirection

    public interface IDisplayErrorImplementation {
        void ShowErrorMessage(string message, Exception ex);
    }
    
    public class DefaultDisplayErrorImplementation : IDisplayErrorImplementation {
        public void ShowErrorMessage(string message, Exception ex) {
            //...
        }
    }
    
    public static class DisplayError {
        static DisplayError(){
            Implementation = new  DefaultDisplayErrorImplementation();
         }
    
        public static IDisplayErrorImplementation Implementation { get; set;}
    
        public static void ShowErrorMessage(string message, Exception ex) {
            Implementation.ShowErrorMessage(message, ex);
        }
    }
    

    You get to keep the existing calls to ErrorDisplay, but they are now more replaceable and testable.

    It is not a perfect solution but, if maintaining legacy code, it lets you add some testability without having to do major rework.

    MessageBox

    You have another problem with running unit tests on ErrorDisplay; you will have a MessageBox appearing. Not good if trying to run tests in a test harness (or as part of a build).

    Again indirection is your friend. You can replace the MessageBox.Show() call with a call to a MessageDisplayService.Show(). The default implementation can call message box, a dummy one can be used for testing (either a mock or simple do nothing implementation)

    Unit Testing

    What are your unit boundaries for testing?

    It looks from the question that you want to run a test against MyClass::MyMethod(), cause an IOException and see that GetIODisplayMessage() is invoked. If I mis-understood, please skip the next section 🙂

    Are you planning to do this for every class/method where an IOException can occur?
    Are you planning to do this for every class/method where each of the other exception types handled by ErrorDisplay can occur?

    This is a lot of work to which simply re-tests the ErrorDisplay code again and again.

    The boundaries I would see are

    MyClass::MyMethod

    If an Exception occurs, it calls ErrorDisplay.ShowErrorMessage() passing in “MyMethod” and the exception.

    Anything after that is out of its hands and should not be part of the unit tests.

    ErrorDisplay.ShowErrorMessage()

    If it is called with an IOException then it shows the message that it gets by calling GetIODisplayMessage().

    This is independent (as written above) of the calling code and can be unit tested separately.

    ErrorDisplay.GetIODisplayMessage()

    Returns the correct error message. OK, a bit of overkill if the value is hardcoded but shows the principle.

    When testing MyClass::MyMethod, we want to verify that when an exception occurs, the error display code is called passing in the correct method name and exception.

    When testing ErrorDisplay.ShowErrorMessage(), we verify we get the correct message for the exception type that we call the MessageDisplayService.Show() with
    methodname + “:” + <>
    we don’t need to test the text of the IODisplayMessage here.

    When testing ErrorDisplay.GetIODisplayMessage() we check that it returns the correct message.

    Hope this is useful,

    Alan.

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

Sidebar

Related Questions

As a simplified example, I have the following data classes: public class Employee {
I have following classes. class A { public: void fun(); } class B: public
I have the following constructor for my Class public MyClass(File f1, File f2, File
I have the following classes: public class Person { public String FirstName { set;
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following two classes: public class Address { public string AddressLine1 {
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following classes in my Model: public abstract class Entity : IEntity

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.