Even an AJAX endpoint should be tested! Not at the controller level because the controller is the consumer, the JSON&#x2F;XML output is your HTML template (just a converted structure) so if the contents are correct then it&#x27;s reasonable to expect the JSON&#x2F;XML is correct if you&#x27;re using a standardized way of rendering it. And you could test your client-side JS to ensure it works too, starting with the &quot;most degenerate&quot; cases such as a 500 or 404.<p>&gt; Forcing myself to refactor rather than &quot;making progress&quot; is going to be a mindshift.<p>Indeed. It&#x27;s like being an ex-heroin addict or something... but when you get to a point where a project is mature, you appreciate your prior restraint every single day. Think about it more like a tank rolling forward, powerful and hard to stop. Not so much like a bullet train that might derail at the next bend.<p>I have the tightest testing requirements on the most internal components; business logic at the center. Leaf nodes have the least testing applied, so for example:
- (Web&#x2F;HTML) Templates are dumb, they get hit @ manual, browser-based V&amp;V but nowhere else; nothing is leafier than this
- Controllers basically just inject values into templates, my controllers are so simple that I actually only test the number of variables I am binding. This sounds so lazy it doesn&#x27;t seem like it could provide anything but this has worked <i>shockingly</i> well for me (yes, even alerted me to oversights) because there is absolutely minimal logic in the controller, often boiling down to decisions about what to bind based upon a result. This is the node connecting the leaf and the branch, it&#x27;s a tiny little thing. I usually test their display method and make sure they inject the correct number of variables. If I can do that and my factory (which is effectively the integration test for this unit) can create the object then I consider the controller working. If there&#x27;s anything else which could fail, it doesn&#x27;t belong in the controller.<p>At the other end of the spectrum are things at the core of your system which are obviously wanting of stringent testing like:
- Business logic
- Billing<p>This is a balance you have to strike but be consistent with whatever you choose.<p>Regarding CRUD and also applicable to APIs, separate your use of those things from the logic which operates on their results. So if you&#x27;re writing something which uses the filesystem for CRUD you want to get ONLY the CRUD isolated in one layer. If you look at that CRUD class you won&#x27;t be able to determine anything about the larger system or its purpose; the CRUD class will only deal with file creation&#x2F;reading&#x2F;update&#x2F;delete and nothing else. It doesn&#x27;t consider, manipulate, iterate, process or do <i>anything</i> with the contents. If you truly isolate that level then you can test your ENTIRE system (each action in isolation) and ensure the CRUD calls are made as expected, without touching disk.<p>As soon as you isolate the statefulness of the filesystem or database or remote service, testing is easy because (assuming you inject dependencies so they aren&#x27;t hardwired) you can replace the CRUD service with a mock which will affirmatively pass or fail with a specified result at your command. If you can &quot;play God&quot; with return values like that then you can test the logic of that unit without restriction, hindrance or interference. What happens to this component if the CRUD class can&#x27;t open the file? Just write a new test for it and mock CRUD&#x27;s state to the same it is when it can&#x27;t find the file. Assert what you <i>want</i> to happen. If it doesn&#x27;t work as expected (the test fails) then you can fix the problem and prove that you didn&#x27;t break anything else while resolving that issue by running against all of the previously-passing tests. If the test passed, you can keep the test and ensure the behavior remains unchanged through future edits.<p>APIs are difficult because they can change and their natures can be very different. Integrating with a local or regional company doesn&#x27;t often resemble the experience of integrating with a giant like Google for example.<p>I would not test Google&#x27;s API, period. When dealing with smaller companies however, it&#x27;s a sticky kind of thing. One particular company I&#x27;ve integrated with breaks their API almost quarterly. If you often find yourself in a situation where the API breaks, even 1-2 times a year... I actually might recommend testing the API against an internalized test double. The internal mock gives you fast tests but importantly also becomes a check against which you can run the API. For APIs, testing is only worthwhile if you&#x27;re interested in that second benefit. That said, it has a really nice benefit of making sure your mocked service comports with the interface of the service itself. It&#x27;s a judgement call. As you mentioned, tracking down bugs in a 3rd party API is an utter nightmare.<p>Unfortunately I don&#x27;t know a better way to mock an API than providing dummy results. These are the least fun tests to write but important to ensure stable interoperability. You should be using information-hiding to limit complexity at each level and hopefully after an abstraction or two you no longer need the API directly anyhow. If you don&#x27;t have a test environment for the API at all I&#x27;d talk to the provider and&#x2F;or consider moving to something else. At that point I&#x27;d even consider doing something in-house if necessary.