Unit Testing Is Overrated
Well thought-out article with good points and deductions! I have a few counterpoints and I think the example is not the best way to argue against unit testing as there are better ways to implement the same features and write better unit tests without bloating the code.<p>Looking at the <i>SolarCalculator</i> class, I would go another way first and refactor it like so:<p><pre><code>    public class SolarCalculator
    {
        public static SolarTimes GetSolarTimes(Location location, DateTimeOffset date) { &#x2F;* ... *&#x2F; }
    }
</code></pre>
1. Made the method static (make it a free function in other languages)
2. Take an explicit Location parameter
3. Return a SolarTimes object directly, not async, not a Task&lt;solarTimes&gt;, and remove Async from the name
4. Drop the now unnecessary class LocationProvider member<p>This becomes more easily unit testable without any excess Arranges steps at the beginning.<p><pre><code>    public class SolarCalculatorTests
    {
        [Fact]
        public Task GetSolarTimes_ForKyiv_ReturnsCorrectSolarTimes()
        {
            &#x2F;&#x2F; Arrange
            var location = new Location(50.45, 30.52);
            var date = new DateTimeOffset(2019, 11, 04, 00, 00, 00, TimeSpan.FromHours(+2));

            var expectedSolarTimes = new SolarTimes(
                new TimeSpan(06, 55, 00),
                new TimeSpan(16, 29, 00)
            );

            &#x2F;&#x2F; Act
            var solarTimes = solarCalculator.GetSolarTimes(location, date);

            &#x2F;&#x2F; Assert
            solarTimes.Should().BeEquivalentTo(expectedSolarTimes);
        }
    }
</code></pre>
The GetSolarTimes function is now purely computational and has no plumbing at all (stealing terms from chimprich). I think the original author would also agree that unit testing <i>SolarTimes GetSolarTimes(Location location, DateTimeOffset date)</i> has none of the problems that unit testing <i>async Task&lt;SolarTimes&gt; GetSolarTimesAsync(DateTimeOffset date)</i> had.<p>(Added benefit: the interface is more flexible, it can be reused in more use cases without modification but that is not the point.)<p>I find that such a refactoring often solves all of the problem entirely. It might seem like we just swapped the problem under the rug and forced the calling code to add the complexity (and the tests, interfaces, mocks etc.) that we discarded, but in practice this is often not the case.<p><pre><code>    &#x2F;&#x2F; Uses original async implicit-location interface
    &#x2F;&#x2F; (assumes an existing solarCalculator instance)
    var solarTimes = await solarCalculator.GetSolarTimesAsync(date);

    &#x2F;&#x2F; Uses proposed non-async explicit-location interface
    &#x2F;&#x2F; (assumes an existing locationProvider instance)
    var solarTimes = SolarCalculator.GetSolarTimes(await locationProvider.GetLocationAsync(), date);
</code></pre>
The reason we can often get away with this in practice is that the complexity increase in the caller is small. We did <i>not add additional state</i> to the caller, we did not push more testing&#x2F;mocking complexity to the caller. The assumed <i>locationProvider</i> instance in the caller <i>replaces</i> the <i>solarCalculator</i> instance in the caller. If testing&#x2F;mocking <i>locationProvider</i> is required, testing&#x2F;mocking <i>solarCalculator</i> ought to have been tested too. We require the caller to test&#x2F;mock something else, not something new.<p>If the original <i>async Task&lt;SolarTimes&gt; GetSolarTimesAsync(DateTimeOffset date)</i> interface is required nonetheless, it can be implemented as a pure &quot;plumbing&quot; function. As such I would agree that unit testing it would provide less value than integration testing. A simple pattern that can be applied here instead of an <i>ILocationProvider</i> interface and all the baggage that comes with it is using a <i>Func&lt;Task&lt;Location&gt;&gt;</i> or lambda instead. This allows both testing the instance with custom location providers and unhindered usage of the <i>SolarCalculator</i> class without always needing to inject a dependency.<p><pre><code>    public class SolarCalculator
    {
        private readonly Func&lt;Task&lt;Location&gt;&gt; _locationProvider;

        &#x2F;&#x2F; default constructor for normal usage
        public SolarCalculator() {
            internalReaLLocationProvider = LocationProvider();
            _locationProvider = async () =&gt; internalReaLLocationProvider.GetLocationAsync();
        }

        &#x2F;&#x2F; constructor for custom locations and testing
        public SolarCalculator(Func&lt;Task&lt;Location&gt;&gt; locationProvider) {
            _locationProvider = locationProvider;
        }

        &#x2F;&#x2F; Gets solar times for current location and specified date
        public async Task&lt;SolarTimes&gt; GetSolarTimesAsync(DateTimeOffset date) { 
            return GetSolarTimes(_locationProvider(), date);
        }
        
        public static SolarTimes GetSolarTimes(Location location, DateTimeOffset date) { &#x2F;* ... *&#x2F; }
    }
</code></pre>
(Sorry, I haven&#x27;t implemented the IDisposable pattern for internalReaLLocationProvider and I might have misplaced an async keyword or two because C# is not my most recent language.)<p>To support the &quot;pyramid-driven&quot; paradigm I argue that the most complicated part of this feature is the solar time calculation and it would well deserve a large test suite containing many test cases like <i>GetSolarTimes_ForKyiv_ReturnsCorrectSolarTimes</i> above (edge cases, diverse locations, etc.). Conversely, the higher-level functions don&#x27;t need this level of testing. Since they contain no complex logic, I usually assume that if they work for one input, they will work for any other. Testing the async, automatic-location version with the simple Kyiv-based input is enough, there is no need to test it with midnight sun and all the same edge cases as the base function.<p>The point I&#x27;m making is that unit testing is not as overrated as the original example suggests. The code can be modularized better (<i>not</i> by making everything an interface), with a well-unit-testable &quot;computational&quot; part and a part which is mostly &quot;plumbing&quot;. I agree with those saying that the second part benefits more from integration testing than unit testing, and I can agree with keeping them &quot;as highly integrated as possible, while keeping their speed and complexity reasonable&quot;. But I insist that unit testing the 1st part and writing it in a way that it is unit testable is important.