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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:54:51+00:00 2026-06-10T23:54:51+00:00

Example: public interface IFoo { bool DoSomething(); } public class Foo:IFoo { public bool

  • 0

Example:

public interface IFoo
{
    bool DoSomething();
}

public class Foo:IFoo
{
    public bool DoSomething()
    {
        var result = DoOtherThing();
        ...
        return result;
    }

    public bool DoOtherThing()
    {
        ...
    }
}

My normal TDD approach would be to write unit tests on both the DoSomething() and DoOtherThing() methods. But this would be very hard to do if DoOtherThing was a private method. I have also read that testing private methods is a no-no.

Is it considered acceptable to have public methods on a class in order to facilitate code coverage and testing, even if the purpose of the class is only to be accessed through its (IFoo) interface? Normally I would put methods that are outside of the scope of the interface as private methods, but this does not allow you to efficiently test all of your code. Making the method public allows you to right tests on the Foo class, but it doesn’t seem correct, at least to me, to have public methods that aren’t called from outside the class. Is this way considered best for TDD or is there a better way?

  • 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-10T23:54:53+00:00Added an answer on June 10, 2026 at 11:54 pm

    Public methods are public because they should be accessible. Make it private if it’s not intended to be invoked from the outside.

    If you fail to get code coverage you might want to break up your class into multiple classes and use composition instead.

    Things that are hard to test usually indicates a design flaw.

    Update

    ok, so lets say you have a method for sending an email. Step 1 is generating a MailMessage class and populating it. Step 2 is sending the email

    Thats two responsibilites (SRP). Composing an email and actually send it. I would not do that in the same class. It will also be code duplication if all your email classes compose their messages and then send them. How do you handle network failure? Do you duplicate those checks too?

    Do something like:

    public class SendWelcomeEmailComposer
    {
        MailMessage Compose(User user)
    }
    
    public class EmailSender
    {
        void SendEmail(MailMessage);
    }
    
    public class EmailService
    {
        void SendWelcomeEmail(User user)
        {
            // compose email
            // and send using the classes above.
        }
    }
    

    Update 2

    The reason to why you should not test private methods is as far as I concern a quality measure. It’s likely that you are breaking some fundamental principles (SOLID) if you get low test coverage.

    So it’s better to take time and reflect over the class design instead of trying to test private methods.

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

Sidebar

Related Questions

For example public interface X{ public void foo(X i); } public class Y implements
I have the following example code using MEF: public interface IFoo<T> {} public class
class Foo1: public IFoo { public: template <class T> std::vector<T> foo() { return std::vector<T>();
Consider the following code. public interface IFoo { } public class Bar { public
for example public interface IWMPSettings [DispId(101)] bool autoStart { get; set; } [DispId(102)] int
Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Consider this example: public interface IAccount { string GetAccountName(string id); } public class BasicAccount
import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event>
The code is also at https://github.com/timp21337/java-generics-example package a; import java.util.List; public interface Container<T> {
Example: public bool Save(MyObj instance) { if (instance.IsNew) { this.repository.Create(instance); } else { this.repository.Update(instance);

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.