{"id":280,"date":"2008-09-19T08:30:04","date_gmt":"2008-09-19T07:30:04","guid":{"rendered":"http:\/\/thekua.com\/atwork\/2008\/09\/19\/running-tests-on-a-specific-os-under-junit4\/"},"modified":"2008-09-19T08:45:56","modified_gmt":"2008-09-19T07:45:56","slug":"running-tests-on-a-specific-os-under-junit4","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/","title":{"rendered":"Running tests on a specific OS under JUnit4"},"content":{"rendered":"<p>Our current development team is split, some working on windows machines, others on macs, with continuous integration tests running against both windows and linux environments. We&#8217;ve had a need to run operating specific JUnit4 tests and I got a little tired putting guard clauses into different <strong>@Before<\/strong>, <strong>@After<\/strong> and <strong>@Test<\/strong> methods to prevent a particular block of code running. <\/p>\n<p>Using the new <strong>org.junit.runner.Runner<\/strong> annotation, and <strong>SystemUtils<\/strong> from <strong>commons-lang<\/strong>, here is the result:<\/p>\n<pre>\r\npackage com.thekua.java.junit4.examples;\r\n\r\nimport org.apache.commons.lang.SystemUtils;\r\nimport org.junit.internal.runners.InitializationError;\r\nimport org.junit.internal.runners.JUnit4ClassRunner;\r\nimport org.junit.runner.notification.RunNotifier;\r\n\r\npublic class RunOnlyOnWindows extends JUnit4ClassRunner {\r\n\r\n    public RunOnlyOnWindows(Class<?> klass) throws InitializationError {\r\n        super(klass);\r\n    }\r\n\r\n    @Override\r\n    public void run(RunNotifier notifier) {\r\n        if (<strong>SystemUtils.IS_OS_WINDOWS<\/strong>) {\r\n            super.run(notifier);            \r\n        }\r\n    }\r\n}<\/pre>\n<p>Our test classes, then look like this: <\/p>\n<pre>...\r\n@RunWith(value=RunOnlyOnWindows.class)\r\npublic class WindowsOnlySpecificFooBarUnitTest {\r\n    ...\r\n}\r\n<\/pre>\n<p>Of course, you can use any other particular variations like <strong>SystemUtils.IS_OS_UNIX<\/strong>, or <strong>SystemUtils.IS_OS_MAC<\/strong> but we haven&#8217;t needed to yet. <\/p>\n<p>Of course, this is easily turned into some sort of conditional runner abstract class but at least you get the basic idea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our current development team is split, some working on windows machines, others on macs, with continuous integration tests running against both windows and linux environments. We&#8217;ve had a need to run operating specific JUnit4 tests and I got a little tired putting guard clauses into different @Before, @After and @Test methods to prevent a particular [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,35],"tags":[],"class_list":["post-280","post","type-post","status-publish","format-standard","hentry","category-development","category-java","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Running tests on a specific OS under JUnit4 - patkua@work<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Running tests on a specific OS under JUnit4 - patkua@work\" \/>\n<meta property=\"og:description\" content=\"Our current development team is split, some working on windows machines, others on macs, with continuous integration tests running against both windows and linux environments. We&#8217;ve had a need to run operating specific JUnit4 tests and I got a little tired putting guard clauses into different @Before, @After and @Test methods to prevent a particular [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2008-09-19T07:30:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2008-09-19T07:45:56+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"Running tests on a specific OS under JUnit4\",\"datePublished\":\"2008-09-19T07:30:04+00:00\",\"dateModified\":\"2008-09-19T07:45:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/\"},\"wordCount\":139,\"commentCount\":2,\"articleSection\":[\"Development\",\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/\",\"name\":\"Running tests on a specific OS under JUnit4 - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2008-09-19T07:30:04+00:00\",\"dateModified\":\"2008-09-19T07:45:56+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2008\\\/09\\\/running-tests-on-a-specific-os-under-junit4\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Running tests on a specific OS under JUnit4\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Running tests on a specific OS under JUnit4 - patkua@work","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:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/","og_locale":"en_US","og_type":"article","og_title":"Running tests on a specific OS under JUnit4 - patkua@work","og_description":"Our current development team is split, some working on windows machines, others on macs, with continuous integration tests running against both windows and linux environments. We&#8217;ve had a need to run operating specific JUnit4 tests and I got a little tired putting guard clauses into different @Before, @After and @Test methods to prevent a particular [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/","og_site_name":"patkua@work","article_published_time":"2008-09-19T07:30:04+00:00","article_modified_time":"2008-09-19T07:45:56+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"Running tests on a specific OS under JUnit4","datePublished":"2008-09-19T07:30:04+00:00","dateModified":"2008-09-19T07:45:56+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/"},"wordCount":139,"commentCount":2,"articleSection":["Development","Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/","url":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/","name":"Running tests on a specific OS under JUnit4 - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2008-09-19T07:30:04+00:00","dateModified":"2008-09-19T07:45:56+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2008\/09\/running-tests-on-a-specific-os-under-junit4\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"Running tests on a specific OS under JUnit4"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":0,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}