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

  • Home
  • SEARCH
  • 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 219077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:48:40+00:00 2026-05-11T18:48:40+00:00

It is common to have classes with methods with string parameters that must be

  • 0

It is common to have classes with methods with string parameters that must be validated agains null or empty, such as this example:

public class MyClass {

    public void MyMethod(string param){

        if(string.IsNullOrEmpty(param)){
            throw new ArgumentNullException(...);
        }
        //...
    }
}

It’s clear that the behavior of the method is the same for both (invalid) values. This is a very common situation, and when it comes to testing these methods, I always doubt about how to do it. I always create two separate tests for these cases:

[TestClass]
public class Tests {

    [TestMethod]
    public void MyMethod_should_fail_if_param_is_null(){
        //...
        myclass.MyMethod(null);
        //...
    }

    [TestMethod]
    public void MyMethod_should_fail_if_param_is_empty(){
        //...
        myclass.MyMethod("");
        //...
    }

}

But I see too much redundancy. Those tests are exactly the same, with the only difference being the parameter passed to the method. That bothers me very much, since I have to create two tests for each string parameter. A method with 3 parameters would have 6 tests only to test the parameters.

I think this is the right way of testing those parameters, but if I know that 99% of string parameters will be validated the same way, wouldn’t it be better just test them for null (or empty) and assume that the behavior in the other case will be the same?

I would like to know what you think about this. I know what I’m asking is more a technical opinion than a technical question, but I think the testing community may have something interesting to say about this situation.

Thank you!

  • 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-11T18:48:40+00:00Added an answer on May 11, 2026 at 6:48 pm

    Personally I’d consider using a single test for all of the parameters. That doesn’t follow the normal dogma of unit testing, but it increases the readability of the tests (by minimizing the amount of test code which is dedicated to a pretty repetitive case) and doesn’t have much in the way of downsides. Yes, if the test fails you don’t know whether all of the checks after the first failing one will also fail – but is that really a problem in practice?

    The important point is to make sure that you’ve got a short cut for testing the case. For instance, you might write something like this (if your unit test framework doesn’t have it already):

    public static void ExpectException<T>(Action action) where T : Exception
    {
        try
        {
            action();
            Assert.Fail("Expected exception " + typeof(T).Name);
        }
        catch (T exception)
        {
            // Expected
        }
    }
    

    Then you can write:

    [Test]
    public void MyMethodFailsWithInvalidArguments()
    {
        ExpectException<ArgumentNullException>(() => myClass.MyMethod(null));
        ExpectException<ArgumentException>(() => myClass.MyMethod(""));
    }
    

    Much more concise than doing each one with an individual try/catch block, or even using an ExpectedException attribute and multiple tests.

    You might want overloads for cases where you also want to verify that in each case, no mocked objects have been touched (to check that side-effects are avoided) or possibly overloads for common exceptions like ArgumentNullException.

    For single-parameter methods you could even write a method to encapsulate exactly what you need:

    public void ExpectExceptionForNullAndEmptyStrings(Action<string> action)
    {
        ExpectException<ArgumentNullException>(() => action(null));
        ExpectException<ArgumentException>(() => action(""));
    }
    

    then call it with:

    [Test]
    public void MyMethodFailsWithInvalidArguments()
    {
        // This *might* work without the 
        ExpectExceptionForNullAndEmptyStrings(myClass.MyMethod);
    }
    

    … and maybe another one for methods with a single parameter but a non-void return type.

    That’s possibly going a bit far though 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 166k
  • Answers 166k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Specify the full path to time (the parentheses are unnecessary):… May 12, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer You don't need the @Results table. Directly inserting and selecting… May 12, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer I would bet that it's going throught the while (!foundField.eof())… May 12, 2026 at 1:24 pm

Related Questions

I have a very common situation here. And for years I haven't found if
I'm currently in University and they're pretty particular about following their standards. They've told
It is common knowledge that built-in enums in C++ are not typesafe. I was
I have a situation where I have an interface that defines how a certain

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.