{"id":1195,"date":"2010-12-12T14:47:08","date_gmt":"2010-12-12T21:47:08","guid":{"rendered":"https:\/\/simpleprogrammer.com\/2010\/12\/12\/back-to-basics-why-unit-testing-is-hard\/"},"modified":"2018-01-09T17:24:34","modified_gmt":"2018-01-09T22:24:34","slug":"back-to-basics-why-unit-testing-is-hard","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/","title":{"rendered":"Back to Basics: Why Unit Testing is Hard"},"content":{"rendered":"<p>More and more lately, I\u2019ve been beginning to question the value of unit testing.\u00a0 I\u2019ve really been starting to wonder if all the work we put into being able to actually test at the unit level and the extra scaffolding we put into our applications to support it is worth the cost.\\n\\nI\u2019m not going to talk about that subject yet.\u00a0 Instead, I want to look at some of the costs of unit testing and ask the question \u201cwhy is unit testing hard?\u201d\\n\\nAfter all, if unit testing weren\u2019t hard, we wouldn\u2019t have to question whether or not it was worth it.\u00a0 It makes sense then to look at first why it is hard and what makes it hard.\\n\\n<em>Before you start learning up any new skill or concept, I suggest you take a look at my course <a href=\"https:\/\/simpleprogrammer.com\/store\/products\/learn-anything-quickly\/\">&#8220;10 Steps to Learn Anything Quickly&#8221;.<\/a><\/em>\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/ar119675077156567.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-left: 0; padding-right: 0; display: inline; padding-top: 0; border: 0;\" title=\"ar119675077156567\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/ar119675077156567_thumb.jpg\" alt=\"ar119675077156567\" width=\"485\" height=\"364\" border=\"0\" \/><\/a>\\n\\n<\/p>\n<h2><\/h2>\n<p>\\n\\n<\/p>\n<h2>The ideal scenario<\/h2>\n<p>\\n\\nUnit testing itself is rather easy once you understand how to do it.\u00a0 Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario.\\n\\nWhat is the ideal scenario then?\\n\\n<strong>It is a unit test where the class under test has no external dependencies.<\/strong>\\n\\nWhen a class we are writing unit tests for doesn\u2019t have any external dependencies, we don\u2019t need mocks or stubs or anything else.\u00a0 We can just write code that tests our code.\\n\\nLet\u2019s look at an example of this.\u00a0 Suppose, I had a class called <em>Calculator.\u00a0 <\/em>This <em>Calculator<\/em> class has some very simple methods.\u00a0 Specifically, let us talk about testing a method <em>Add.\u00a0 Add <\/em>takes two single digit integers and returns the result.\u00a0 If either integer passed in is more than a single digit, it throws an exception.\\n\\nIt is a pretty stupid method, with little use, but it will serve the point well here.\\n\\nWe can TDD or BDD this baby with minimal effort.\\n\\nLet\u2019s start thinking of test cases:\\n\\n<\/p>\n<ul>\\n    <\/p>\n<li>When I add 0 and a single digit number it should return the single digit number<\/li>\n<p>\\n    <\/p>\n<li>When I add 0 and 0 it should return 0<\/li>\n<p>\\n    <\/p>\n<li>When I add two single digit numbers it should return the sum of those numbers<\/li>\n<p>\\n    <\/p>\n<li>When I add one two digit number it should throw an exception<\/li>\n<p>\\n<\/ul>\n<p>\\n\\nPretty easy to come up with test cases, just as easy to implement them:\\n\\n<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:76f84a40-5e84-4388-adcf-805db05ac012\" class=\"wlWriterEditableSmartContent\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/6b8c275939dc17939cad399b21f5fea6.js\"><\/script>\\n\\n<\/div>\n<p>\\n\\nWe can then implement the code that will make this test pass pretty easily.\u00a0 I won\u2019t show it here since it is so trivial.\\n\\nThis is the ideal scenario, or what I will call <strong>Level 1 Unit Testing<\/strong>.\\n\\n<strong>Level 1 Unit Testing<\/strong> is where we have a single class with no external dependencies and no state.\u00a0 We are just testing an algorithm.\\n\\n<\/p>\n<h2>Taking it up a notch<\/h2>\n<p>\\n\\nThe next level of unit testing is reached if we add state to the class under test.\\n\\n<strong>Level 2 Unit Testing<\/strong> is where we have a single class with no external dependencies but it does have state.\u00a0 We are setting up an object and testing it as a whole.\\n\\nIf we take our existing example, and now we want to add a new method called <em>GetHistory, <\/em>it is still not difficult to implement the tests, but it gets harder, because we have to make sure we are setting up some state for our object as part of the test.\\n\\nLet\u2019s look at one of the test cases we might implement for this functionality:\\n\\n<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:80309f0a-86ba-4b8a-a695-8124833de562\" class=\"wlWriterEditableSmartContent\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/544c5b0d0e624df4605c892aed3d15ba.js\"><\/script>\\n\\n<\/div>\n<p>\\n\\nAgain, not too hard.\u00a0 But, state does make this a bit more difficult.\u00a0 Here the value of a Behavior Driven Development (BDD) style of unit testing can be seen as it helps us to clearly divide the test up into the different parts we now have.\\n\\nThe real complexity we have added here is that we now have to deal with a setup step before we can execute our test.\u00a0 BDD deals with this by having a special step for defining the context in which the actual test takes place.\u00a0 It is called a few different things in different BDD circles, but let\u2019s stick with AAA for this post, since it is easy to remember.\\n\\nThe major difference between<strong> Level 1 Unit Testing<\/strong> and <strong>Level 2 Unit Testing<\/strong> is that in Level 1, we were really testing only one method.\u00a0 In Level 2 we are testing at the class level.\u00a0 Really we could call <strong>Level 1 Unit Testing<\/strong> method testing, since the unit we are testing is the method.\u00a0 The class that method existed in didn\u2019t matter.\\n\\n<\/p>\n<h2>Enter dependencies<\/h2>\n<p>\\n\\nLet\u2019s see what happens when we throw dependencies into the <em>Calculator<\/em> class.\\n\\nImagine that our <em>Calculator<\/em> class has to keep an audit trail of our calculations.\u00a0 We have a service that we can use to put calculations from the <em>Add<\/em> method into a storage location, like a database and our <em>GetHistory<\/em> method can query the storage location for the history.\\n\\nAs I was thinking about this, an important point occurred to me.\u00a0 Were this an integration test, our example test method above wouldn\u2019t change at all.\\n\\nBut, as it turns out we are talking about unit tests here, we need to isolate the testing down to the class level.\\n\\nSo let\u2019s think about what our test should do now.\u00a0 Here are some possible tests we might have.\\n\\n<\/p>\n<ul>\\n    <\/p>\n<li>When I add two number the result is returned and the <em>Store<\/em> method is called on the <em>StorageService<\/em> with that result.<\/li>\n<p>\\n    <\/p>\n<li>When I get the history, the <em>RetrieveHistory<\/em> method is called on the <em>StorageService <\/em>and it\u2019s results are returned back.<\/li>\n<p>\\n<\/ul>\n<p>\\n\\nLet\u2019s see what one of these tests might look like:\\n\\n<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:fde6810a-6429-43fe-895e-ef0728aa9cea\" class=\"wlWriterEditableSmartContent\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/f6d5c8af9b81783099c31946727be3f7.js\"><\/script>\\n\\n<\/div>\n<p>\\n\\nI call this <strong>Level 3 Unit Testing.<\/strong>\\n\\n<strong>Level 3 Unit Testing <\/strong>is when we have a single class with at least one external dependency, but it does not depend on its own internal state.\\n\\nThings really start to get complicated here, because we have to start thinking not just about inputs and outputs and sequences, but now have to think about interactions.\\n\\nIt really starts to get blurry here about what the expectations of our unit tests should be.\u00a0 In the example code above, do we need to check to make sure <em>IsServiceOnline<\/em> is called on the <em>StorageService <\/em>or do we only check that <em>Store<\/em> was called?\\n\\nYou\u2019ll also notice here that we had to use a mock and pass our dependency into our class so that we could change its behavior.\u00a0 Along with that came the burden of creating an interface, so that we could have a mock implementation.\\n\\nIf you\u2019re paying attention right now, you may be thinking to yourself that the example is bad.\u00a0 You may be thinking that the <em>Calculator<\/em> class now has two responsibilities.\\n\\n<\/p>\n<ul>\\n    <\/p>\n<li>It calculates things and return the result<\/li>\n<p>\\n    <\/p>\n<li>It stores calculation results<\/li>\n<p>\\n<\/ul>\n<p>\\n\\nRight you are, but we can\u2019t wish away this problem.\u00a0 Let\u2019s suppose we refactor and move the StorageService dependency out of the <em>Calculator<\/em> class.\u00a0 We have several options.\u00a0 We could make a decorator and use it like this:\\n\\n<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b5bac265-9e13-4b85-b93b-e2c7cd42ed3f\" class=\"wlWriterEditableSmartContent\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/7253b10e601ffc53551871ce0e062ac7.js\"><\/script>\\n\\n<\/div>\n<p>\\n\\nOr we could do something more like a mediator pattern like so:\\n\\n<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:f4ab745d-91cf-4bbe-a1de-f0dc1fb88cb5\" class=\"wlWriterEditableSmartContent\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/ef7520d5c0fb2e3449e640dcb8689a36.js\"><\/script>\\n\\n<\/div>\n<p>\\n\\nHowever we attempt to solve this problem, we are still going to have to have some class that will have to have a mock in its unit test.\\n\\nThere is a simple fact that we cannot get around. <strong>If we are going to use the <em>StorageService <\/em>to store calculations, either <em>Calculator<\/em> will depend on it, or something else will depend on calculator and it.\u00a0 <\/strong>There is no alternative to those two options.\\n\\nThere is another simple fact we can\u2019t get around also.\u00a0 <strong>If we are going to depend on another class in our unit test, we either need an interface that we can use for the mock class, or we need a mocking framework that will support mocking concrete classes.<\/strong>\\n\\nSo with <strong>Level 3 Unit Testing <\/strong>we are stuck with needing to mock at least one dependency and either creating a bogus interface, or using a mocking library that will let us mock concrete classes.\\n\\n<\/p>\n<h2><\/h2>\n<p>\\n\\n<\/p>\n<h2><strong>It gets worse<\/strong><\/h2>\n<p>\\n\\nIt only gets worse from here.\u00a0 At Level 3 we didn\u2019t worry about state inside our calculator class, we worried about an external dependency that pretty much handled state for us.\u00a0 In many cases though we will have to worry about state and dependencies.\\n\\n<strong>Level 4 Unit Testing<\/strong> is when we have a single class with at least one external dependency and depends on its own internal state.\\n\\nIn our calculator example, we can simply add the requirement that we only want to get the history for a particular session of calculations.\u00a0 We need to keep track of the calculations so that we can ask the <em>StorageService<\/em> for the history for our session.\\n\\nLet\u2019s look at a possible test for that scenario:\\n\\n<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:ea71e8d8-ccff-4b83-af1b-21f20fc59421\" class=\"wlWriterEditableSmartContent\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/77aaef90308a64d431672e2272c7057e.js\"><\/script>\\n\\n<\/div>\n<p>\\n\\nConsider for a moment how fragile and complex this unit testing code is.\u00a0 Consider how simple the functionality of our class is.\\n\\nWe have a major problem here.\u00a0 Our unit testing code is more complex than the code it is testing!\u00a0 It\u2019s ok, if the unit testing code is more lines of code than the code it is testing, that is usually the case.\u00a0 But, I consider it a big problem when our unit testing code is more complex, because you have to ask yourself the very real question.\\n\\nWhere is there more likely to be a bug?\\n\\n<\/p>\n<h2>I\u2019m not saying anything yet<\/h2>\n<p>\\n\\nMy point is not to make a point, at least not yet.\u00a0 My real goal here is to help us to change the way we think about unit testing.\\n\\nWe need to stop asking the general question of whether not unit testing is worth the cost and instead ask the more specific question of what level of unit testing is worth the cost.\\n\\nLevel 3+ has a very steep cost as mocking is unavoidable and adds considerable complexity to even the most trivial of implementations.\\n\\nFrom that we can draw a bit of wisdom.\u00a0 If we are going to unit test we should strive to encapsulate as much of our pure logic into classes without dependencies and if possible without state.\\n\\nThe other thing to consider is that as the difficulty and complexity of the unit tests are increasing each level, the goal of the test and value starts to become lost also.\\n\\nWhat I mean by this, is that when we start testing that our class properly calls another class with certain parameters, we are crossing over into testing the implementation details of the class.\\n\\nIf I say a class should be able to add 2 numbers and return the result.\u00a0 I am not talking about how it has to do it.\u00a0 As long as the result is correct, how doesn\u2019t matter.\\n\\nWhen I add a mock and say a class needs to add 2 numbers and store the result using a <em>StorageService<\/em> by calling the method <em>Store<\/em> on it, I have now tied how into the test.\u00a0 Changing how breaks the test.\\n\\nThat\u2019s all we are going to look at for now.\u00a0 If you\u2019ve read some of my <a href=\"https:\/\/simpleprogrammer.com\/back-to-basics-series\/\">other back to basics posts<\/a>, you can see the progression up to this point.\u00a0 I\u2019ve been discounting using interfaces and dependency injection for the sake of unit testing, but I have yet to offer an alternative.\u00a0 I still don\u2019t.\u00a0 To be honest, I don\u2019t have one yet.\u00a0 But, I do believe by breaking down this problem to its roots we can evaluate what we are doing and determine what our true problems are.\\n\\nBy the end of this series I hope to have a solution and a recommendation for tackling these kinds of problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>More and more lately, I\u2019ve been beginning to question the value of unit testing.\u00a0 I\u2019ve really been starting to wonder if all the work we put into being able to actually test at the unit level and the extra scaffolding we put into our applications to support it is worth the cost.\\n\\nI\u2019m not going to&#8230;<\/p>\n","protected":false},"author":2,"featured_media":24881,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[2185,148,12,3882],"tags":[],"class_list":["post-1195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-practices","category-design","category-testing","category-unit-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Back to Basics: Why Unit Testing is Hard - Simple Programmer<\/title>\n<meta name=\"description\" content=\"Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Back to Basics: Why Unit Testing is Hard - Simple Programmer\" \/>\n<meta property=\"og:description\" content=\"Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-12-12T21:47:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T22:24:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Back to Basics: Why Unit Testing is Hard\",\"datePublished\":\"2010-12-12T21:47:08+00:00\",\"dateModified\":\"2018-01-09T22:24:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/\"},\"wordCount\":2060,\"commentCount\":19,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/12\\\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png\",\"articleSection\":[\"Best Practices\",\"Design\",\"Testing\",\"Unit Testing\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/\",\"name\":\"Back to Basics: Why Unit Testing is Hard - Simple Programmer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/12\\\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png\",\"datePublished\":\"2010-12-12T21:47:08+00:00\",\"dateModified\":\"2018-01-09T22:24:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/12\\\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/12\\\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/back-to-basics-why-unit-testing-is-hard\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Back to Basics: Why Unit Testing is Hard\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Back to Basics: Why Unit Testing is Hard - Simple Programmer","description":"Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/","og_locale":"en_US","og_type":"article","og_title":"Back to Basics: Why Unit Testing is Hard - Simple Programmer","og_description":"Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.","og_url":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/","og_site_name":"Simple Programmer","article_published_time":"2010-12-12T21:47:08+00:00","article_modified_time":"2018-01-09T22:24:34+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png","type":"image\/png"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Back to Basics: Why Unit Testing is Hard","datePublished":"2010-12-12T21:47:08+00:00","dateModified":"2018-01-09T22:24:34+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/"},"wordCount":2060,"commentCount":19,"image":{"@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png","articleSection":["Best Practices","Design","Testing","Unit Testing"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/","url":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/","name":"Back to Basics: Why Unit Testing is Hard - Simple Programmer","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png","datePublished":"2010-12-12T21:47:08+00:00","dateModified":"2018-01-09T22:24:34+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered\u2026 at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/12\/Back-to-Basics-Why-Unit-Testing-Is-Hard.png","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/back-to-basics-why-unit-testing-is-hard\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Back to Basics: Why Unit Testing is Hard"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/1195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=1195"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/1195\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/24881"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}