{"id":768,"date":"2011-12-08T10:22:00","date_gmt":"2011-12-08T10:22:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/why-you-should-write-unit-tests-testing-techniques-8.html"},"modified":"2012-10-21T20:53:54","modified_gmt":"2012-10-21T20:53:54","slug":"why-you-should-write-unit-tests-testing","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html","title":{"rendered":"Why You Should Write Unit Tests &#8211; Testing Techniques 8"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally dangerous for suggesting that certain classes may not need unit tests. Having dealt with <i>What<\/i> to test, today\u2019s blog deals with <i>Why<\/i> you should write unit tests, and today\u2019s example code is based upon a true story: only names, dates and facts have been changed.<\/p>\n<p>A client recently asked for a emergency release of some code to display a message on the screen, for legal reasons, on appropriate pages of their web site.<\/p>\n<p>The scenario was that a piece of information should be displayed on the screen if it existed in the database &#8211; a very simple scenario, which can be covered by a few simple lines of code. However, in the rush to write the code, the developer didn\u2019t write any unit tests and the code contained a bug that wasn\u2019t spotted until the patch reached UAT. You may ask what the bug was, and it was something that we\u2019ve all done at some point in our careers: adding an unwanted semi-colon \u2018;\u2019 to the end of a line.<\/p>\n<p>I\u2019m going to demonstrate a re-written, stunt double, version of the code using my <span class=\"Apple-style-span\" style=\"font-family: 'Courier New', Courier, monospace\">AddressService<\/span> scenario that I\u2019ve used in my previous \u2018Testing Techniques\u2019 blogs as outlined by the UML diagram below:<\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a href=\"http:\/\/3.bp.blogspot.com\/-yH4AkqVLOAg\/TrvNnaMIzKI\/AAAAAAAAAXk\/6xOz3GWqj9U\/s320\/Screen+Shot+2011-11-10+at+12.58.52.png\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/3.bp.blogspot.com\/-yH4AkqVLOAg\/TrvNnaMIzKI\/AAAAAAAAAXk\/6xOz3GWqj9U\/s320\/Screen+Shot+2011-11-10+at+12.58.52.png\" \/><\/a><\/div>\n<p>In this demonstration the functionality has changed, but the logic and sample code structure has essentially remained the same. In the <span class=\"Apple-style-span\" style=\"font-family: 'Courier New', Courier, monospace\">AddressService <\/span>world, the logic runs like this:<\/p>\n<ol style=\"text-align: left\">\n<li>Get an address from the database.<\/li>\n<li>If the address exists then format it and return the resulting string.<\/li>\n<li>If the addres does not exist then return null.<\/li>\n<li>If the formatting fails, don\u2019t worry about it and return null.<\/li>\n<\/ol>\n<p>The re-written <span class=\"Apple-style-span\" style=\"font-family: 'Courier New', Courier, monospace\">AddressService.findAddress(&#8230;) <\/span>looks something like this:<\/p>\n<pre class=\"brush:java\">@Component\r\npublic class AddressService {\r\n\r\n  private static final Logger logger = LoggerFactory\r\n      .getLogger(AddressService.class);\r\n\r\n  private AddressDao addressDao;\r\n\r\n  public String findAddressText(int id) {\r\n\r\n    logger.info(\"In Address Service with id: \" + id);\r\n    Address address = addressDao.findAddress(id);\r\n\r\n    String formattedAddress = null;\r\n\r\n    if (address != null);\r\n    try {\r\n      formattedAddress = address.format();\r\n    } catch (AddressFormatException e) {\r\n      \/\/ That's okay in this business case so ignore it\r\n    }\r\n\r\n    logger.info(\"Leaving Address Service with id: \" + id);\r\n    return formattedAddress;\r\n  }\r\n\r\n  @Autowired\r\n  @Qualifier(\"addressDao\")\r\n  void setAddressDao(AddressDao addressDao) {\r\n    this.addressDao = addressDao;\r\n  }\r\n}\r\n<\/pre>\n<p>Did you spot the bug? I didn\u2019t when I reviewed the code&#8230; Just in case, I\u2019ve annotated a screen shot below:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\">\n<\/div>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a href=\"http:\/\/1.bp.blogspot.com\/-9eJ904mCNp8\/Ts6BhBYgKwI\/AAAAAAAAAX4\/bULhMR2h77I\/s320\/Screen+Shot+2011-11-24+at+16.33.45.png\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/1.bp.blogspot.com\/-9eJ904mCNp8\/Ts6BhBYgKwI\/AAAAAAAAAX4\/bULhMR2h77I\/s320\/Screen+Shot+2011-11-24+at+16.33.45.png\" \/><\/a><\/div>\n<p>The point of demonstrating a trivial bug, a simple mistake that anyone can make, is to highlight the importance of writing a few unit tests because unit tests would have spotted the problem and saved a whole load of time and expense. Of course, each organisation is different, but releasing the above code caused the following sequence of events:<\/p>\n<ul style=\"text-align: left\">\n<li>The application is deployed to Dev, Test, and UAT.<\/li>\n<li>The test team checked that the modified screen works okay and passes the change.<\/li>\n<li>Other screens are regression tested and found to be incorrect. All failing screens are noted.<\/li>\n<li>An urgent bug report is raised.<\/li>\n<li>The report passes through various management levels.<\/li>\n<li>The report gets passed to me (and I miss lunch) to investigate the possible problem.<\/li>\n<li>The report gets sent to three other members of the team to investigate (four pairs of eyes are better than one)<\/li>\n<li>The offending semi-colon is found and fixed.<\/li>\n<li>The code is retested in dev and checked in to source control.<\/li>\n<li>The application is built and deployed to Dev, Test, and UAT.<\/li>\n<li>The test team checks that the modified screen works okay and passes the change.<\/li>\n<li>Other screens are regression tested and pass.<\/li>\n<li>The emergency fix is passed.<\/li>\n<\/ul>\n<p>The above chain of events obviously wastes a good number of man hours, costs a shed load of cash, unnecessarily raises peoples stress levels, and tarnishes our reputation with the customer: <span class=\"Apple-style-span\" style=\"background-color: orange\">all of which are very good reasons for writing unit tests.<\/span><\/p>\n<p>To prove the point, I\u2019ve written the three missing unit tests and it only took me an extra 15 minutes development time, which compared with the number of man hours wasted seems a good use of developer time.<\/p>\n<pre class=\"brush:java\">@RunWith(UnitilsJUnit4TestClassRunner.class)\r\npublic class WhyToTestAddressServiceTest {\r\n\r\n  private AddressService instance;\r\n\r\n  @Mock\r\n  private AddressDao mockDao;\r\n\r\n  @Mock\r\n  private Address mockAddress;\r\n\r\n  \/**\r\n   * @throws java.lang.Exception\r\n   *\/\r\n  @Before\r\n  public void setUp() throws Exception {\r\n\r\n    instance = new AddressService();\r\n    instance.setAddressDao(mockDao);\r\n  }\r\n\r\n  \/**\r\n   * This test passes with the bug in the code\r\n   * \r\n   * Scenario: The Address object is found in the database and can return a\r\n   * formatted address\r\n   *\/\r\n  @Test\r\n  public void testFindAddressText_Address_Found() throws AddressFormatException {\r\n\r\n    final int id = 1;\r\n    expect(mockDao.findAddress(id)).andReturn(mockAddress);\r\n    expect(mockAddress.format()).andReturn(\"This is an address\");\r\n\r\n    replay();\r\n    instance.findAddressText(id);\r\n    verify();\r\n  }\r\n\r\n  \/**\r\n   * This test fails with the bug in the code\r\n   * \r\n   * Scenario: The Address Object is not found and the method returns null\r\n   *\/\r\n  @Test\r\n  public void testFindAddressText_Address_Not_Found() throws AddressFormatException {\r\n\r\n    final int id = 1;\r\n    expect(mockDao.findAddress(id)).andReturn(null);\r\n\r\n    replay();\r\n    instance.findAddressText(id);\r\n    verify();\r\n  }\r\n\r\n  \/**\r\n   * This test passes with the bug in the code\r\n   * \r\n   * Scenario: The Address Object is found but the data is incomplete and so a\r\n   * null is returned.\r\n   *\/\r\n  @Test\r\n  public void testFindAddressText_Address_Found_But_Cant_Format() throws AddressFormatException {\r\n\r\n    final int id = 1;\r\n    expect(mockDao.findAddress(id)).andReturn(mockAddress);\r\n    expect(mockAddress.format()).andThrow(new AddressFormatException());\r\n\r\n    replay();\r\n    instance.findAddressText(id);\r\n    verify();\r\n  }\r\n}\r\n<\/pre>\n<p>And finally,at the risk of sounding smug I have to confess that although in this case, the bug wasn&#8217;t mine, I have released similar bugs into the wild in the past &#8211; before I learnt to write unit tests&#8230;<br \/>\nThe source code is available from GitHub at:<\/p>\n<p>git:\/\/github.com\/roghughe\/captaindebug.git<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/www.captaindebug.com\/2011\/12\/why-you-should-write-unit-tests-testing.html\">Why You Should Write Unit Tests &#8211; Testing Techniques 8<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a>&nbsp;&nbsp;Roger Hughes&nbsp;at the&nbsp;<a href=\"http:\/\/www.captaindebug.com\/\">Captain Debug&#8217;s Blog<\/a>.<\/p>\n<p><strong><i>Related Articles :<\/i><\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/testing-techniques-not-writing-tests.html\">Testing Techniques &#8211; Not Writing Tests<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/misuse-of-end-to-end-tests-testing.html\">The Misuse of End To End Tests &#8211; Testing Techniques 2<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/what-should-you-unit-test-testing.html\">What Should you Unit Test? &#8211; Testing Techniques 3<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/regular-unit-tests-and-stubs-testing.html\">Regular Unit Tests and Stubs &#8211; Testing Techniques 4<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/unit-testing-using-mocks-testing.html\">Unit Testing Using Mocks &#8211; Testing Techniques 5<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/11\/creating-stubs-for-legacy-code-testing.html\">Creating Stubs for Legacy Code &#8211; Testing Techniques 6<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/12\/more-on-creating-stubs-for-legacy-code.html#more\">More on Creating Stubs for Legacy Code &#8211; Testing Techniques 7<\/a><\/li>\n<li><a href=\"http:\/\/www.javacodegeeks.com\/2011\/12\/some-definitions-testing-techniques-9.html\">Some Definitions &#8211; Testing Techniques 9<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally dangerous for suggesting that certain classes may not need unit tests. Having dealt with What to test, today\u2019s blog deals with Why you should write unit tests, and &hellip;<\/p>\n","protected":false},"author":65,"featured_media":176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[274,273],"class_list":["post-768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-junit","tag-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Why You Should Write Unit Tests - Testing Techniques 8 - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Why You Should Write Unit Tests - Testing Techniques 8 - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-12-08T10:22:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-21T20:53:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Roger Hughes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roger Hughes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html\"},\"author\":{\"name\":\"Roger Hughes\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c9feacaf8e783104a69621cd65bf1f07\"},\"headline\":\"Why You Should Write Unit Tests &#8211; Testing Techniques 8\",\"datePublished\":\"2011-12-08T10:22:00+00:00\",\"dateModified\":\"2012-10-21T20:53:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html\"},\"wordCount\":771,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"keywords\":[\"JUnit\",\"Testing\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html\",\"name\":\"Why You Should Write Unit Tests - Testing Techniques 8 - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"datePublished\":\"2011-12-08T10:22:00+00:00\",\"dateModified\":\"2012-10-21T20:53:54+00:00\",\"description\":\"I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/junit-logo-e1426444701180.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/12\\\/why-you-should-write-unit-tests-testing.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Why You Should Write Unit Tests &#8211; Testing Techniques 8\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c9feacaf8e783104a69621cd65bf1f07\",\"name\":\"Roger Hughes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g\",\"caption\":\"Roger Hughes\"},\"sameAs\":[\"http:\\\/\\\/www.captaindebug.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Roger-Hughes\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Why You Should Write Unit Tests - Testing Techniques 8 - Java Code Geeks","description":"I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally","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:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html","og_locale":"en_US","og_type":"article","og_title":"Why You Should Write Unit Tests - Testing Techniques 8 - Java Code Geeks","og_description":"I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally","og_url":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-12-08T10:22:00+00:00","article_modified_time":"2012-10-21T20:53:54+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","type":"image\/jpeg"}],"author":"Roger Hughes","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Roger Hughes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html"},"author":{"name":"Roger Hughes","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c9feacaf8e783104a69621cd65bf1f07"},"headline":"Why You Should Write Unit Tests &#8211; Testing Techniques 8","datePublished":"2011-12-08T10:22:00+00:00","dateModified":"2012-10-21T20:53:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html"},"wordCount":771,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","keywords":["JUnit","Testing"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html","url":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html","name":"Why You Should Write Unit Tests - Testing Techniques 8 - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","datePublished":"2011-12-08T10:22:00+00:00","dateModified":"2012-10-21T20:53:54+00:00","description":"I\u2019ve had lots of reaction to my recent blog on \u2018What you Should Test\u2019, some agreeing with me for varying reasons and others thinking that I\u2019m totally","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/junit-logo-e1426444701180.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/12\/why-you-should-write-unit-tests-testing.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Why You Should Write Unit Tests &#8211; Testing Techniques 8"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c9feacaf8e783104a69621cd65bf1f07","name":"Roger Hughes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/db9d1e5362dbc3f8007b383b852473b59fb8c5282a6066a13ab1cef761a9d5d6?s=96&d=mm&r=g","caption":"Roger Hughes"},"sameAs":["http:\/\/www.captaindebug.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/Roger-Hughes"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=768"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/768\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/176"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}