{"id":8162,"date":"2013-02-06T16:00:42","date_gmt":"2013-02-06T14:00:42","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=8162"},"modified":"2013-02-06T06:31:01","modified_gmt":"2013-02-06T04:31:01","slug":"synchronising-multithreaded-integration-tests","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html","title":{"rendered":"Synchronising Multithreaded Integration Tests"},"content":{"rendered":"<p>Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test&#8230; hard. This is because in JUnit there&#8217;s no built in synchronisation between the test code, the object under test and any threads. This means that problems usually arise when you have to write a test for a method that creates and runs a thread. One of the most common scenarios in this domain is in making a call to a method under test, which starts a new thread running before returning. At some point in the future when the thread&#8217;s job is done you need assert that everything went well. Examples of this scenario could include asynchronously reading data from a socket or carrying out a long and complex set of operations on a database.<br \/>\n<a name=\"more\"><\/a><br \/>\n&nbsp;<br \/>\nFor example, the<code> ThreadWrapper<\/code> class below contains a single public method: <code>doWork()<\/code>. Calling<code> doWork()<\/code> sets the ball rolling and at some point in the future, at the discretion of the JVM, a thread runs adding data to a database.<\/p>\n<pre class=\" brush:java\">public class ThreadWrapper {\r\n\r\n  \/**\r\n   * Start the thread running so that it does some work.\r\n   *\/\r\n  public void doWork() {\r\n\r\n    Thread thread = new Thread() {\r\n\r\n      \/**\r\n       * Run method adding data to a fictitious database\r\n       *\/\r\n      @Override\r\n      public void run() {\r\n\r\n        System.out.println(\"Start of the thread\");\r\n        addDataToDB();\r\n        System.out.println(\"End of the thread method\");\r\n      }\r\n\r\n      private void addDataToDB() {\r\n        \/\/ Dummy Code...\r\n        try {\r\n          Thread.sleep(4000);\r\n        } catch (InterruptedException e) {\r\n          e.printStackTrace();\r\n        }\r\n      }\r\n\r\n    };\r\n\r\n    thread.start();\r\n    System.out.println(\"Off and running...\");\r\n  }\r\n\r\n}<\/pre>\n<p>A straightforward test for this code would be to call the<code> doWork()<\/code> method and then check the database for the result. The problem is that, owing to the use of a thread, there&#8217;s no co-ordination between the object under test, the test and the thread. A common way of achieving some co-ordination when writing this kind of test is to put some kind of delay in between the call to the method under test and checking the results in the database as demonstrated below:<\/p>\n<pre class=\" brush:java\">public class ThreadWrapperTest {\r\n\r\n  @Test\r\n  public void testDoWork() throws InterruptedException {\r\n\r\n    ThreadWrapper instance = new ThreadWrapper();\r\n\r\n    instance.doWork();\r\n\r\n    Thread.sleep(10000);\r\n\r\n    boolean result = getResultFromDatabase();\r\n    assertTrue(result);\r\n  }\r\n\r\n  \/**\r\n   * Dummy database method - just return true\r\n   *\/\r\n  private boolean getResultFromDatabase() {\r\n    return true;\r\n  }\r\n}<\/pre>\n<p>In the code above there is a simple<code> Thread.sleep(10000)<\/code> between two method calls. This technique has the benefit of being incredabile simple; however it&#8217;s also very risky. This is because it introduces a race condition between the test and the worker thread as the JVM makes no guarantees about when threads will run. Often it&#8217;ll work on a developer&#8217;s machine only to fail consistently on the build machine. Even if it does work on the build machine it atificially lengthens the duration of the test; remember that quick builds are important. The only sure way of getting this right is to synchronise the two different threads and one technique for doing this is to inject a simple<code> CountDownLatch<\/code> into the instance under test. In the example below I&#8217;ve modified the<code> ThreadWrapper<\/code> class&#8217;s doWork() method adding the<code> CountDownLatch<\/code> as an argument.<\/p>\n<pre class=\" brush:java\">public class ThreadWrapper {\r\n\r\n  \/**\r\n   * Start the thread running so that it does some work.\r\n   *\/\r\n  public void doWork(final CountDownLatch latch) {\r\n\r\n    Thread thread = new Thread() {\r\n\r\n      \/**\r\n       * Run method adding data to a fictitious database\r\n       *\/\r\n      @Override\r\n      public void run() {\r\n\r\n        System.out.println(\"Start of the thread\");\r\n        addDataToDB();\r\n        System.out.println(\"End of the thread method\");\r\n        countDown();\r\n      }\r\n\r\n      private void addDataToDB() {\r\n\r\n        try {\r\n          Thread.sleep(4000);\r\n        } catch (InterruptedException e) {\r\n          e.printStackTrace();\r\n        }\r\n      }\r\n\r\n      private void countDown() {\r\n        if (isNotNull(latch)) {\r\n          latch.countDown();\r\n        }\r\n      }\r\n\r\n      private boolean isNotNull(Object obj) {\r\n        return latch != null;\r\n      }\r\n\r\n    };\r\n\r\n    thread.start();\r\n    System.out.println(\"Off and running...\");\r\n  }\r\n}<\/pre>\n<p>The<a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/util\/concurrent\/CountDownLatch.html\" target=\"new\"> Javadoc API <\/a>describes a count down latch as: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately. This is a one-shot phenomenon &#8212; the count cannot be reset. If you need a version that resets the count, consider using a CyclicBarrier.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>A CountDownLatch is a versatile synchronization tool and can be used for a number of purposes. A CountDownLatch initialized with a count of one serves as a simple on\/off latch, or gate: all threads invoking await wait at the gate until it is opened by a thread invoking countDown(). A CountDownLatchinitialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times. A useful property of a CountDownLatch is that it doesn&#8217;t require that threads calling countDown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.<\/p>\n<p>The idea here is that the test code will never check the database for the results until the<code> run()<\/code> method of the worker thread has called<code> latch.countdown()<\/code>. This is because the test code thread is blocking at the call to<code> latch.await()<\/code>.<code> latch.countdown()<\/code> decrements latch&#8217;s count and once this is zero the blocking call the<code> latch.await()<\/code> returns and the test code continues executing, safe in the knowledge that any results which should be in the database, are in the database. The test can then retrieve these results and make a valid assertion. Obviously, the code above merely fakes the database connection and operations. The thing is you may not want to, or need to, inject a<code> CountDownLatch<\/code> directly into your code; after all it&#8217;s not used in production and it doesn&#8217;t look particularly clean or elegant. One quick way around this is to simply make the<code> doWork(CountDownLatch latch)<\/code> method package private and expose it through a public<code> doWork()<\/code> method.<\/p>\n<pre class=\" brush:java\">public class ThreadWrapper {\r\n\r\n  \/**\r\n   * Start the thread running so that it does some work.\r\n   *\/\r\n  public void doWork() {\r\n    doWork(null);\r\n  }\r\n\r\n  @VisibleForTesting\r\n  void doWork(final CountDownLatch latch) {\r\n\r\n    Thread thread = new Thread() {\r\n\r\n      \/**\r\n       * Run method adding data to a fictitious database\r\n       *\/\r\n      @Override\r\n      public void run() {\r\n\r\n        System.out.println(\"Start of the thread\");\r\n        addDataToDB();\r\n        System.out.println(\"End of the thread method\");\r\n        countDown();\r\n      }\r\n\r\n      private void addDataToDB() {\r\n\r\n        try {\r\n          Thread.sleep(4000);\r\n        } catch (InterruptedException e) {\r\n          e.printStackTrace();\r\n        }\r\n      }\r\n\r\n      private void countDown() {\r\n        if (isNotNull(latch)) {\r\n          latch.countDown();\r\n        }\r\n      }\r\n\r\n      private boolean isNotNull(Object obj) {\r\n        return latch != null;\r\n      }\r\n\r\n    };\r\n\r\n    thread.start();\r\n    System.out.println(\"Off and running...\");\r\n  }\r\n}<\/pre>\n<p>The code above uses Google&#8217;s Guava<code> @VisibleForTesting<\/code> annotation to tell us that the<code> doWork(CountDownLatch latch)<\/code> method visibility has been relaxed slightly for testing purposes.<\/p>\n<p>Now I realise that making a method call package private for testing purposes in highly controversial; some people hate the idea, whilst others include it everywhere. I could write a whole blog on this subject (and may do one day), but for me it should be used judiciously, when there&#8217;s no other choice, for example when you&#8217;re writing characterisation tests for legacy code. If possible it should be avoided, but never ruled out. After all tested code is better than untested code.<\/p>\n<p>With this in mind the next iteration of<code> ThreadWrapper<\/code> designs out the need for a method marked as <code>@VisibleForTesting<\/code> together with the need to inject a<code> CountDownLatch<\/code> into your production code. The idea here is to use the Strategy Pattern and separate the<code> Runnable<\/code> implementation from the<code> Thread<\/code>. Hence, we have a very simple<code> ThreadWrapper<\/code><\/p>\n<pre class=\" brush:java\">public class ThreadWrapper {\r\n\r\n  \/**\r\n   * Start the thread running so that it does some work.\r\n   *\/\r\n  public void doWork(Runnable job) {\r\n\r\n    Thread thread = new Thread(job);\r\n    thread.start();\r\n    System.out.println(\"Off and running...\");\r\n  }\r\n}<\/pre>\n<p>and a separate job:<\/p>\n<pre class=\" brush:java\">public class DatabaseJob implements Runnable {\r\n\r\n  \/**\r\n   * Run method adding data to a fictitious database\r\n   *\/\r\n  @Override\r\n  public void run() {\r\n\r\n    System.out.println(\"Start of the thread\");\r\n    addDataToDB();\r\n    System.out.println(\"End of the thread method\");\r\n  }\r\n\r\n  private void addDataToDB() {\r\n\r\n    try {\r\n      Thread.sleep(4000);\r\n    } catch (InterruptedException e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}<\/pre>\n<p>You&#8217;ll notice that the<code> DatabaseJob<\/code> class doesn&#8217;t use a<code> CountDownLatch<\/code>. How is it synchronised? The answer lies in the test code below&#8230;<\/p>\n<pre class=\" brush:java\">public class ThreadWrapperTest {\r\n\r\n  @Test\r\n  public void testDoWork() throws InterruptedException {\r\n\r\n    ThreadWrapper instance = new ThreadWrapper();\r\n\r\n    CountDownLatch latch = new CountDownLatch(1);\r\n\r\n    DatabaseJobTester tester = new DatabaseJobTester(latch);\r\n    instance.doWork(tester);\r\n    latch.await();\r\n\r\n    boolean result = getResultFromDatabase();\r\n    assertTrue(result);\r\n  }\r\n\r\n  \/**\r\n   * Dummy database method - just return true\r\n   *\/\r\n  private boolean getResultFromDatabase() {\r\n    return true;\r\n  }\r\n\r\n  private class DatabaseJobTester extends DatabaseJob {\r\n\r\n    private final CountDownLatch latch;\r\n\r\n    public DatabaseJobTester(CountDownLatch latch) {\r\n      super();\r\n      this.latch = latch;\r\n    }\r\n\r\n    @Override\r\n    public void run() {\r\n      super.run();\r\n      latch.countDown();\r\n    }\r\n  }\r\n}<\/pre>\n<p>The test code above contains an inner class<code> DatabaseJobTester<\/code>, which extends<code> DatabaseJob<\/code>. In this class the<code> run()<\/code> method has been overridden to include a call to<code> latch.countDown()<\/code> after our fake database has been updated via the call to<code> super.run()<\/code>. This works because the test passes a<code> DatabaseJobTester<\/code> instance to the<code> doWork(Runnable job)<\/code> method adding in the required thread testing capability. The idea of sub-classing objects under test is something I&#8217;ve mentioned before in one of my<a href=\"http:\/\/www.captaindebug.com\/p\/blogs-on-testing-techniques.html?m=0#.UQI90icgGK0\" target=\"new\"> blogs on testing techniques<\/a> and is a really powerful technique.<\/p>\n<p>So, to conclude:<\/p>\n<ul>\n<li>Testing threads is hard.<\/li>\n<li>Testing anonymous inner classes is almost impossible.<\/li>\n<li>Using <code>Thead.sleep(...)<\/code> is a risky idea and should be avoided.<\/li>\n<li>You can refactor out these problems using the Strategy Pattern.<\/li>\n<li><a href=\"http:\/\/www.captaindebug.com\/2012\/11\/is-programming-art-of-making-right.html#.UQUEr6Ux-lI\">Programming is the Art of Making the Right Decision<\/a><\/li>\n<\/ul>\n<p>&#8230;and that relaxing a method&#8217;s visibility for testing may or may not be a good idea, but more on that later&#8230;<\/p>\n<p>The code above is available on Github in the captain debug repository (git:\/\/github.com\/roghughe\/captaindebug.git) under the unit-testing-threads project.<br \/>\n&nbsp;<\/p>\n<p><strong><em>Reference: <\/em><\/strong><a href=\"http:\/\/www.captaindebug.com\/2013\/02\/synchronising-multithreaded-integration.html#.URHZSGdYWSo\">Synchronising Multithreaded Integration Tests<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Roger Hughes at the <a href=\"http:\/\/www.captaindebug.com\/\">Captain Debug&#8217;s Blog <\/a> blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test&#8230; hard. This is because in JUnit there&#8217;s no built in synchronisation between the test code, the object under test and any threads. This means that problems usually arise when you have to write a test for a &hellip;<\/p>\n","protected":false},"author":65,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[88,273],"class_list":["post-8162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-concurrency","tag-testing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Synchronising Multithreaded Integration Tests<\/title>\n<meta name=\"description\" content=\"Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test... hard. This is because in JUnit\" \/>\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\/2013\/02\/synchronising-multithreaded-integration-tests.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Synchronising Multithreaded Integration Tests\" \/>\n<meta property=\"og:description\" content=\"Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test... hard. This is because in JUnit\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.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=\"2013-02-06T14:00:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.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=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html\"},\"author\":{\"name\":\"Roger Hughes\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c9feacaf8e783104a69621cd65bf1f07\"},\"headline\":\"Synchronising Multithreaded Integration Tests\",\"datePublished\":\"2013-02-06T14:00:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html\"},\"wordCount\":1130,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Concurrency\",\"Testing\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html\",\"name\":\"Synchronising Multithreaded Integration Tests\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2013-02-06T14:00:42+00:00\",\"description\":\"Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test... hard. This is because in JUnit\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/02\\\/synchronising-multithreaded-integration-tests.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\":\"Synchronising Multithreaded Integration Tests\"}]},{\"@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":"Synchronising Multithreaded Integration Tests","description":"Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test... hard. This is because in JUnit","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\/2013\/02\/synchronising-multithreaded-integration-tests.html","og_locale":"en_US","og_type":"article","og_title":"Synchronising Multithreaded Integration Tests","og_description":"Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test... hard. This is because in JUnit","og_url":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-02-06T14:00:42+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.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":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html"},"author":{"name":"Roger Hughes","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c9feacaf8e783104a69621cd65bf1f07"},"headline":"Synchronising Multithreaded Integration Tests","datePublished":"2013-02-06T14:00:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html"},"wordCount":1130,"commentCount":1,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Concurrency","Testing"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html","url":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html","name":"Synchronising Multithreaded Integration Tests","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2013-02-06T14:00:42+00:00","description":"Testing threads is hard, very hard and this makes writing good integration tests for multithreaded systems under test... hard. This is because in JUnit","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/02\/synchronising-multithreaded-integration-tests.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":"Synchronising Multithreaded Integration Tests"}]},{"@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\/8162","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=8162"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/8162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=8162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=8162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=8162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}