Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose private method to unit test

I'm trying to expose my private method to unit testing. I saw this example in UnitTesting Succintly:

public class DoesSomething
{
#if TEST
    public
#else
    private
#endif
    void SomeComputation() {
    }
}

Here is my attempted at it but the code still appears private when trying to access via test.

#if TEST
            public
#else
        private
#endif
        Organization TransformToOrganization(OrgChartOrganizationUnitType org)
        {
            ...
        }
like image 917
Antarr Byrd Avatar asked Nov 19 '25 18:11

Antarr Byrd


1 Answers

You shouldn't need to expose private methods to your tests. The public methods of your class have a specification. Your unit tests should be testing that the public methods of your class adhere to that specification. How the public methods go about doing that is no one's business; you want to be free to change your private implementation however you like as long as you continue to adhere to the specification of your public methods.

If you write unit tests that depend on the private implementation, you end up with incredibly brittle unit tests that don't actually add any value because the only thing that you need to validate is that the publicly exposed members adhere to their specification. If there are little kittens running around hidden in the background making the whole thing work, it doesn't matter.

like image 67
jason Avatar answered Nov 22 '25 07:11

jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!