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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:12:04+00:00 2026-06-01T05:12:04+00:00

In a lot of TDD tutorials I see code like this: public class MyClass

  • 0

In a lot of TDD tutorials I see code like this:

public class MyClass
{
    public void DoSomething(string Data)
    {
      if (String.IsNullOrWhiteSpace(Data))
        throw new NullReferenceException();
    }
}


[Test]
public DoSomething_NullParameter_ThrowsException()
{
   var logic = MyClass();

   Assert.Throws<NullReferenceException>(() => logic.DoSomething(null));
}

This all makes sense but at some point you are going to get to the class that actually uses MyClass and you want to test that the exception is handled:

public class EntryPointClass
{
  public void DoIt(string Data)
  {
     var logicClass = new MyClass();

     try
     {
        logicClass.DoSomething(Data);
     }
     catch(NullReferenceException ex)
     {

     }
  }
}

[Test]
public DoIt_NullParameter_IsHandled()
{
  var logic = new EntryPointClass()

  try
  {
    logic.DoIt(null);
  }
  catch
  {
    Assert.Fail();
  }
}

So why not put the try/catch in MyClass in the first place rather than throwing the exception and have the test that test’s for null in the MyClass unit test class and not in the EntryPointClass unit test class?

  • 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-01T05:12:05+00:00Added an answer on June 1, 2026 at 5:12 am

    Typically, your exception handling will look like this:

    public class EntryPointClass
    {
      Logger _logfile;
      // ...
      public void DoIt(string Data)
      {
         var logicClass = new MyClass();
    
         try
         {
            logicClass.DoSomething(Data);
         }
         catch(NullReferenceException ex)
         {
             _logfile.WriteLine("null reference exception occured in method 'DoIt'");
         }
      }
    }
    

    (The Logger is just an example for a resource you need for proper exception handling not available at the place where MyClass lives. You could also add the display of a message box here or something like that.)

    At the level of MyClass, you typically don’t have the appropriate tools available for proper exception handling (and you don’t want to add them there to keep the class decoupled from, for example, a specific logging mechanism).

    Note that this design decision is independent from doing TDD. If you want your class MyClass actually catching the exceptions by itself, you have to write your tests differently, that is right. But that is only a good idea if catching the exceptions does not block your automatic tests. Try, for example, to write a good unit test for MyClass when MyClass shows a hardcoded warning dialog by itself.

    Of course, the example above shows that unit testing EntryPointClass may get harder in reality when you first need something like a logger to get things working. In general, you can attack this problem by providing the logger at constrcution time utilizing an ILogger interface which allows the logger to be replaced by mock. Or in this simple case, it may be enough not to initialize the logger for your unit tests at all and code it like this:

     public class EntryPointClass 
     {
          // ....
          catch(NullReferenceException ex)
          {
              if(_logfile!=null)
                  _logfile.WriteLine("null reference exception occured in method 'DoIt'");
          }    
          // ....
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A lot of times when reading source code I see something like this: public
I know that TDD helps a lot and I like this method of development
I am aware of this question: https://stackoverflow.com/questions/428691/how-to-encourage-implementation-of-tdd In my team, we write a lot
I use Assert.Fail a lot when doing TDD. I'm usually working on one test
I know there's a lot of stuff on TDD and i'm trying to pick
lot__no is a public string Dim myDelims As String() = New String() {<beginning of
A lot of contact management programs do this - you type in a name
I do a lot of TDD and am thinking of installing a Continuous Integration
I've been reading a lot on TDD over the past few months and decided
I've read a lot about Test-Driven Development (TDD) and I find the principles very

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.