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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:00:13+00:00 2026-05-16T00:00:13+00:00

Suppose I have a function like this: public void AddEntry(Entry entry) { if (entry.Size

  • 0

Suppose I have a function like this:

public void AddEntry(Entry entry)
{
    if (entry.Size < 0)
        throw new ArgumentException("Entry size must be greater than zero");
    DB.Entries.Add(entry);
}

And a corresponding unit test:

[TestMethod]
[ExpectedException(typeof(ArgumentException), "Entry size must be greater than zero")]
public void AddEntry_TermSizeLessThanZero_ThrowException()
{
    Entry e = new Entry(-5);

    AddEntry(e);
}

And then I refactor the validation code out:

public void AddEntry(Entry entry)
{
    Validate(entry);
    DB.Entries.Add(entry);
}

public void Validate(Entry entry)
{
    if (entry.Size < 0)
        throw new ArgumentException("Entry size must be greater than zero");
}

The unit test no longer describes the validation code.

What’s the best thing to do in this case? Do I just leave Validate() to be tested through AddEntry?

Edit: to clarify, supposing I had a reason to make the refractored code public (a bit contrived in this situation), would I want to duplicate the test code to be thorough?

  • 1 1 Answer
  • 1 View
  • 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-16T00:00:13+00:00Added an answer on May 16, 2026 at 12:00 am

    At this point I wouldn’t add a test to Validate(), since that code is already hit by an existing test. I like that test since it corresponds closely to the class’s requirements.

    This changes when you start using Validate() in other functions in the class, or in other classes. Validate() may also be extended to validate different issues, you’ll want to address this.

    When Validate() has multiple callers, and Validate() starts testing multiple conditions, you’ll probably want:

    • 1 Validate() test per validation condition
    • 1 test per call to Validate
      • you might verify someone calls Validate by passing in one of the failure conditions
        • upside of this approach is less fake abstraction
        • downside is those tests could all become invalid if the Validate() criteria changes
        • consider putting the code generating the invalid input in a test utility method
      • you might also verify someone calls Validate by passing in a mock object for the validator
        • upside of this approach is decoupling of what is validated versus who is doing the validation, making tests more robust versus some changes
        • downside is yet another layer of abstraction making the code more complex

    It seems adding tests in this manner keeps good coverage while scaling linearly with the number of requirements tested.

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

Sidebar

Related Questions

Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose I have two functions which look like this: public static void myFunction1(int a,
Suppose I have a utility function like this - public static boolean isABlankSpace(char c)
Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have a free function called InitFoo . I'd like to protect this
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
I have something like that (simplified) class A { public: virtual void Function ()
I have a function that I use to add vectors, like this: public static

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.