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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:45:18+00:00 2026-05-24T00:45:18+00:00

After reading a blog post mentioning how it seems wrong to expose a public

  • 0

After reading a blog post mentioning how it seems wrong to expose a public getter just to facilitate testing, I couldn’t find any concrete examples of better practices.

Suppose we have this simple example:

public class ProductNameList
{
    private IList<string> _products;
    public void AddProductName(string productName)
    {
        _products.Add(productName);
    }
}

Let’s say for object-oriented design reasons, I have no need to publicly expose the list of products. How then can I test whether AddProductName() did its job? (Maybe it added the product twice, or not at all.)

One solution is to expose a read-only version of _products where I can test whether it has only one product name — the one I passed to AddProductName().

The blog post I mentioned earlier says it’s more about interaction (i.e., did the product name get added) rather than state. However, state is exactly what I’m checking. I already know AddProductName() has been called — I want to test whether the object’s state is correct once that method has done its work.

Disclaimer: Although this question is similar to
Balancing Design Principles: Unit Testing, (1) the language is different (C# instead of Java), (2) this question has sample code, and (3) I don’t feel the question was adequately answered (i.e., code would have helped demonstrate the concept).

  • 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-24T00:45:18+00:00Added an answer on May 24, 2026 at 12:45 am

    You could make your accessors protected so you can mock or you could use internal so that you can access the property in a test but that IMO would be wrong as you have suggested.

    I think sometimes we get so caught up in wanting to make sure that every little thing in our code is tested. Sometime we need to take a step back and ask why are we storing this value, and what is its purpose? Then instead of testing that the value gets set we can then start testing that the behaviour of the component is correct.

    EDIT

    One thing you can do in your scenario is to have a bastard constructor where you inject an IList and then you test that you have added a product:

    public class ProductNameList
    {
        private IList<string> _products;
    
        internal ProductNameList(IList<string> products)
        {
            _products = products;
        }
        ...
    }
    

    You would then test it like this:

    [Test]
    public void FooTest()
    {
        var productList = new List<string>();
    
        var productNameList = new ProductNameList(productList);
    
        productNameList.AddProductName("Foo");
    
        Assert.IsTrue(productList[0] == "Foo");
    }
    

    You will need to remember to make internals visable to your test assembly.

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

Sidebar

Related Questions

Just signed up for a trial at fogcreek.com/Fogbugz after reading Joel's latest blog post
After reading Chris' answer to F# - public literal and the blog post at
Recently, I've got a dangerous idea into my head after reading this blog post.
After reading Jeff's blog post on Protecting Your Cookies: HttpOnly . I'd like to
After reading Jeff's blog post and being fairly new to GUI programming, I'm wondering
After reading a blog post, I got the idea to add safety code to
After reading an interesting article about unit testing behavior instead of state, I came
I was reading this post on the MySQL performance blog. Under application tuning it
After much googling, stackoverflowing (?) and blog-reading I still cant completely solve the issue.
After reading the Head First Design Patterns book and using a number of other

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.